Ts-jest: Source Map Inconsistency

Created on 30 Jan 2018  ยท  5Comments  ยท  Source: kulshekhar/ts-jest

Issue

I spent quite a bit of time trying to get source maps working with the Node (8.x) debugger and
VSCode. I finally got it working so I could set breakpoints in TypeScript code and have jest
stop at the breakpoints. (Sadly, this was no small feat to get working)

Unfortunately, after I got all that working I ran some jest tests that failed and the problem is
that now ts-jest cannot generate proper stack traces. In stead, it throws the following error
whenever my code throws an exception:

 โ— Test suite failed to run

    ENOENT: no such file or directory, open '/users/mtiller/source/opensource/jsonata/eval2.ts'

      at exports.formatStackTrace (node_modules/jest-message-util/build/index.js:232:20)
          at Array.map (<anonymous>)

This is, presumably, ts-jest trying to reconstruct a proper stack trace using the TypeScript
code.

Expected behavior

What I'd like to be able to do is configure jest, tsc, ts-jest and VSCode to all have a
common understanding about source maps so that I can simultaneously have working breakpoints,
working coverage metrics and working stack traces. But I cannot seem to achieve all three.

Output from your debug log

I was unable to get this to produce a debug.txt file. The command I ran (with my sample repo, see below) was:

$ TS_JEST_DEBUG="true" yarn jest

Link to a minimal repo that reproduces this issue

I don't have a minimal repo. But I do have a repo that demonstrates it. You can clone [email protected]:xogeny/jsonata.git and switch to the source-map-inconsistency branch
to access the code.

I'm using yarn and VSCode. You just need to run yarn install followed by yarn jest and you
should see the issue with the stack trace not working. I've created a launch.json file in the
.vscode directory to demonstrate setting breakpoints. Just set a break point at src/evaluate.ts:39 and run the Debug Tests debugging configuration. If you do that, VSCode
will stop at the correct line in the file (demonstrating that the source maps seem to be
sufficient for the Node debugger to be able to map ts<->js.

If you change the value of sourceRoot in tsconfig.json to be ./src, then the stack trace
works when running jest from the command line (kind of...file paths look odd) but then the
breakpoints don't work. They will actually stop at the right place, but they only show the
inline source code, not the actual file (so you see the yellow marker, but not the red one).

I realize that this isn't a minimal repository and I don't expect you to have to deal with that
complexity. But since I managed to isolate this issue down to a reproducible case that I can
share (and demonstrate which setting is impacting the behavior), I figured I'd at least share
it in case it is of some value.

Most helpful comment

In an attempt to create a minimal repo, I created a repository with a README.md that walks step-by-step through the process of setting up and configuring ts-jest. The funny thing is that doing it this way, I didn't have any issues.

I found that if I copied tsconfig.json, jest.config.js and .vscode/launch.json back to my other (complicated) project that everything worked. I'll try to go through and figure out which specific settings caused the problems if I get some time. But at least I've found a nice miminal configuration that works the way I want.

I also turned the README.md into a post for Medium called Debugging with TypeScript, Jest, ts-jest and Visual Studio Code.

All 5 comments

I should point out that this "fix" of setting sourceRoot to ./src addresses the issue if the
exception is thrown from code in my src directory. But if, instead, the test fails because of
an assertion in the jest test itself (i.e., code in the __tests__ directory), then the source
maps are wrong again.

Any thoughts on how this inconsistency could develop (i.e., debugger and ts-jest stack traces getting conflicting source map information)?

@xogeny I'd like to look into this but without a minimal repo, I'm hesitant because there have been several instances where looking at a larger project has resulted in false leads and wasted time.

If you can replicate this issue with just a couple of files in src and test folders, I'll be happy to take a look at it after/before work. Without that, I'll have to wait till I can set aside a longer window of time.

I'll give it a try.

In an attempt to create a minimal repo, I created a repository with a README.md that walks step-by-step through the process of setting up and configuring ts-jest. The funny thing is that doing it this way, I didn't have any issues.

I found that if I copied tsconfig.json, jest.config.js and .vscode/launch.json back to my other (complicated) project that everything worked. I'll try to go through and figure out which specific settings caused the problems if I get some time. But at least I've found a nice miminal configuration that works the way I want.

I also turned the README.md into a post for Medium called Debugging with TypeScript, Jest, ts-jest and Visual Studio Code.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bruk1977 picture bruk1977  ยท  3Comments

Vinnl picture Vinnl  ยท  3Comments

qm3ster picture qm3ster  ยท  3Comments

bySabi picture bySabi  ยท  4Comments

artola picture artola  ยท  3Comments