node-source-map-support icon indicating copy to clipboard operation
node-source-map-support copied to clipboard

Jest source maps not working

Open schw4rzlicht opened this issue 5 years ago • 10 comments

Hey there!

I'm pretty new to TypeScript/jest and I spent hours in figuring out why line numbering in failing tests are wrong so please bear with me.

I'm not sure if I missed something or it's an actual bug and I couldn't find anything close to my problem online. When running install(), line numbering breaks, when commenting it out, everything works fine (but of course stack traces don't work any more). It doesn't matter if install() is run in test code or in imported production code, test line numbering always breaks.

I have set up a demo repo over here.

Thanks for your help!

schw4rzlicht avatar May 24 '20 19:05 schw4rzlicht

Did it work with 0.5.18? 🤔

LinusU avatar May 25 '20 07:05 LinusU

I just tested 0.5.0 and 0.5.18 and they both have the same problem.

schw4rzlicht avatar May 25 '20 13:05 schw4rzlicht

Possibly related and probably not this Libs issue https://github.com/istanbuljs/nyc/issues/1323 as Jest uses Istanbul nyc.

Any suggestions?

Also this error is occurring with nyc that is strange

robot@raspberrypi:~/Sketches/JS/Classes/_/o $ ../node_modules/.bin/nyc  --require=../node_modules/source-map-support/register.js  --source-map=false --cache=false --compact=false  ../node_modules/.bin/_mocha Test/Spork.js
Cannot find module '../node_modules/source-map-support/register.js'
Require stack:
- /home/robot/Sketches/JS/Classes/_/node_modules/nyc/bin/nyc.js

Verified that register.js is where it is expected

My suspicion is mix and match CJS and ES module types are the problem?

Lib is working with just mocha though

andinoJamesV avatar May 30 '20 06:05 andinoJamesV

So I kept digging into the project and code coverage is built into V8

andinoJamesV avatar May 30 '20 16:05 andinoJamesV

@Heavy-Gray I'm not sure how I could actually help.

My suspicion is mix and match CJS and ES module types are the problem?

Where does that suspicion come from? Did you have a look at the sample I linked above? Do you mean b/c of

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

https://github.com/schw4rzlicht/node-sourcemap-jest-demo/blob/master/tsconfig.json

schw4rzlicht avatar Jun 02 '20 19:06 schw4rzlicht

@schw4rzlicht No for sure you can`t I was bouncing around broken libraries and by chance this lib and thread came up.

Just to verify node-source-map-support is working and the problem you are having is in nyc or Jest

andinoJamesV avatar Jun 02 '20 19:06 andinoJamesV

Just to verify node-source-map-support is working and the problem you are having is in nyc or Jest

Yes. Debugging and stack traces have the right line numbers outside of Jest tests.

schw4rzlicht avatar Jun 04 '20 20:06 schw4rzlicht

Related issue: https://github.com/facebook/jest/issues/10330

dmaretskyi avatar Aug 14 '20 08:08 dmaretskyi

as a workaround:

if (!process.env.TEST_MODE) {
  require('source-map-support').install()
}

until this issue is fixed.

also, https://github.com/evanw/esbuild has impact as well because of this issue. probably https://github.com/swc-project/swc also.

stavalfi avatar Feb 05 '21 21:02 stavalfi

Are you using ts-jest? I am seeing this issue when using ts-jest and I have opened a ticket with minimal repro here: https://github.com/kulshekhar/ts-jest/issues/2372

silasdavis avatar Feb 17 '21 13:02 silasdavis