1

I'm playing around with TypeScript in a command-line NodeJS project, working through Advent of Code problems. For day 2, I started with:

day2.ts

export function* lineToNumbers(line: string): Iterable<number> {
    console.log(`line = "${line}"`);
    const parts = line.trim().split(/[Sa-b]\s+/);
    for (const part of parts) {
        yield Number(part);
    }
}

And a Mocha unit test for this:

day2-test.ts

import * as day2 from "./day2";

describe("day2.lineToNumbers()", () => {
    it("empty or whitespace string returns empty arrayS", () => {
        assert.equal(Array.from(day2.lineToNumbers("")), []);
        assert.equal(Array.from(day2.lineToNumbers("   ")), []);
    });
});

When I run the main program, the debugger will hit a breakpoint in the TS source just fine, but when I try to debug the (failing) unit test, breakpoints don't get hit. I'm guessing the unit test runner just executes the generated .js file directly and doesn't handle source maps correctly.

Is there a way to bludgeon Visual Studio into just letting me debug an all-TS project correctly?

millimoose
  • 39,073
  • 9
  • 82
  • 134

1 Answers1

3

To debug unit test and let the breakpoint be hit, we need choose Debug Selected Test: enter image description here

Or right-click the unit test file Set as Node.js Startup File: and debug it as normal(Press F5)

enter image description here

Update:

The latest VS version really has this issue, we have reported this issue to the product team:

https://developercommunity.visualstudio.com/content/problem/184931/vs2017-1554-debugging-typescript-unit-test-the-bre.html

If possible, you could add your comment and vote it in above report.

Fletcher
  • 422
  • 1
  • 6
  • 21
  • "Debug Selected Tests" is what I'm doing but no dice. – millimoose Jan 17 '18 at 10:47
  • I tried setting it as the startup file, but that just fails with "describe" not being defined – millimoose Jan 17 '18 at 10:52
  • @millimoose have you installed Mocha? https://stackoverflow.com/a/28400523/9125096 – Fletcher Jan 18 '18 at 08:00
  • Yes, the tests do run and give me a pass/fail. They also run with "npx mocha" from the command-line. Debugging even works in VS Code, amusingly enough, it's just the big VS that keeps getting confused. – millimoose Jan 18 '18 at 09:21
  • It also can't detect the Mocha tests as Mocha tests reliably - it would give me errors on `describe` and `it` even though I have `import "mocha";` above my code, then it wouldn't for about a day, now it does again. Also the editor thinks they're Jasmine tests but that might be ReSharper not having Mocha support mucking things up, I'll try disabling R# for a while. – millimoose Jan 18 '18 at 09:25
  • @millimoose It might be really a limitation of Visual Studio, I just update it to VS2017 15.5.4, and suffered the same issue as yours. I've already reported it to VS developing team: [link(]https://developercommunity.visualstudio.com/content/problem/184931/vs2017-1554-debugging-typescript-unit-test-the-bre.html) you may also go to vote and comment on it. – Fletcher Jan 19 '18 at 03:44
  • Thanks for going through the bother, I was afraid this would be left as a “my computer is cursed” mystery. – millimoose Jan 19 '18 at 10:38
  • 1
    Node.js 8 and later changed their debugger protocol. The change went in for debugging the actual project, but the support for debugging the tests didn't get in time. The fix is now merged in, but not yet shipped (https://github.com/Microsoft/nodejstools/pull/1712) – Bill Ticehurst Jan 20 '18 at 00:24