jest icon indicating copy to clipboard operation
jest copied to clipboard

Source map support

Open trusktr opened this issue 11 years ago • 22 comments

Inline and external source-map support is very much needed (f.e. when using preprocessors). Is there currently an official way to use source maps so that the line numbers of my errors (the very things that jest revolves around) are meaningful?

trusktr avatar Apr 22 '15 21:04 trusktr

Any way to do this yet? Following suggestions from:

http://marmelab.com/blog/2015/06/24/jest-in-practice.html https://medium.com/@dwerthen/speeding-up-react-testing-with-jest-5a300c1918b7#.bot61cbba

I took out the "scriptPreprocessor" so that I could manually watch for changes using babel-cli. Is it possible to use inlined source maps from babel yet in jest?

theengineear avatar Jan 14 '16 03:01 theengineear

I'm going to assume that this should probably work with babel. I'm going to close this as babel has a retainLines option that we are using in babel-jest and at FB we are currently unlikely to use another compile-to-JS language that needs this feature. Is there a reason why you'd want source maps if you know the exact line number of when something is failing?

I also appreciate contributions to babel-jest (see the packages/ folder) that improve support here.

cpojer avatar Mar 03 '16 08:03 cpojer

I'm using typescript & jest reports the JS line numbers. Is there a workaround? Thx!

kellerapps avatar Mar 20 '16 00:03 kellerapps

A feature to preserve line numbers in typescript was specifically rejected because they already produce source maps: https://github.com/Microsoft/TypeScript/issues/3142

Jessidhia avatar Nov 24 '16 07:11 Jessidhia

@cpojer There is a reason for adding source map support: codecov.io doesn't support coverage reports for files not checked into source control. I tried using remap-istanbul, but it couldn't resolve the original source paths correctly.

kripod avatar Jan 13 '17 21:01 kripod

I was finally able to solve this issue. See felipeochoa/jest-sourcemaps for the details. (It's not pretty, but it works!)

felipeochoa avatar Mar 20 '17 16:03 felipeochoa

@cpojer I was poking around the refactoring you did in #3376 and #3388 and was excited to see that source maps are cleanly handled when coverage is enabled! Is the plan to allow saving source maps independently of whether coverage is enabled? It seems to me that simply removing the else clause in line 227 and the instrument && mapCoverage on line 241 would be enough to let transformer authors specify sourcemaps.

A current limitation of the jest-sourcemaps approach is that I can't cache source maps, so having this built-in with jest would be amazing!

felipeochoa avatar Apr 29 '17 07:04 felipeochoa

@felipeochoa I have no smart thoughts on this to be honest, so feel free to send a pull request with the changes and with tests and we'll see where can take it.

cpojer avatar May 02 '17 12:05 cpojer

So, jest's support for source maps is currently limited to coverage only? If I write a custom preprocessor that extracts inline source maps for TypeScript I get accurate code coverage on my .ts files, but whenever I throw an exception the lines in the stack trace are completely wrong.

GAumala avatar Jun 04 '17 03:06 GAumala

I've managed to use remap-istanbul with TypeScript, this should work with other transpilers. I could get Codecov and Code Climate to work with this. Here the explanations: https://github.com/codecov/example-typescript/issues/7

Edit: ~~why not use ts-jest? Because you cannot debug (e.g set breakpoints) your tests with it, see https://github.com/kulshekhar/ts-jest/pull/118#issuecomment-283633957~~ ts-jest now fully works, including vscode breakpoints


Copy-paste for history:

What you need to change in your code for it to work: https://github.com/tkrotoff/react-form-with-constraints/commit/b5817829141ea6b28e77a8eeaa6bdb1571dc495c

  • Configure TypeScript to generate the source maps by adding "sourceMap": true to your tsconfig.json
  • Configure Jest so that it generates only coverage-final.json thanks to "coverageReporters": ["json"]. By default Jest generates everything including the HTML report (lcov-report) and lcov.info
  • Then use remap-istanbul to rewrite coverage-final.json using the source maps
  • And finally istanbul report to generate the right HTML report (lcov-report) and lcov.info from the correct coverage-final.json

This blog post: https://medium.com/@novemberborn/code-coverage-with-babel-istanbul-nyc-83b8c2f1093 allowed me to understand.

tkrotoff avatar Aug 29 '17 21:08 tkrotoff

@tkrotoff Thanks for your help, you definitely have moved me in the right direction but my istanbul coverage report still shows the incorrect umber of lines.

image

while the actual lines in the file are: image

Any help would be greatly appreciated.

sean-killeen avatar Feb 22 '18 16:02 sean-killeen

@sean-killeen you should use ts-jest, no need for remap-istanbul:

ts-jest now fully works, including vscode breakpoints

tkrotoff avatar Feb 22 '18 16:02 tkrotoff

I actually started with ts-jest originally and have been getting an error with istanbul-lib-coverage when using it.

at CoverageMap.fileCoverageFor (node_modules/istanbul-lib-coverage/lib/coverage-map.js:96:15) at Array.forEach ()

that is what spawned this process.

update:

getting the latest versions of jest and jest-cli have resolved the cli error, however, the initial issue still persists while using ts-jest

sean-killeen avatar Feb 22 '18 16:02 sean-killeen

Jest should fully support sourcemaps out of the box now, as long as they are either inline or the transformer returns it (meaning we don't look up external sourcemaps).

@sean-killeen could you provide a repro of your issue?

SimenB avatar Feb 23 '18 08:02 SimenB

@SimenB I think this is an open issue with React 16 and Node 8. I found this which I think is the same issue. What I am getting is coverage but with incorrect line numbers. Looks the the numbers are generated from the transpiled source code that isn't source-mapped.

sean-killeen avatar Feb 23 '18 11:02 sean-killeen

Is there a conversation somewhere about why jest does not support external source maps/are they considered in the future? Having just transitioned form mocha and with all that's baked in, I somewhat expected this :)

The workarounds to get support seem much heavier than other test runners (see https://github.com/facebook/jest/issues/2205#issuecomment-376222735).

patrickhulce avatar Mar 26 '18 16:03 patrickhulce

PR welcome! We support inline sourcemaps, should be possible to support external as well.

SimenB avatar Mar 26 '18 16:03 SimenB

greetings typescript friends

i've found two harmonious workflows for jest+typescript+vscode that have worked well for me regarding source maps

  1. use ts-jest on your ts files
    i've found ts-jest to "just work" excellently,
    it will hot-compile your ts files right before each test run,
    sourcemapping works like a charm, great dev experience

  2. use jest on your js files, plus tsc option --inlineSourceMap
    the sourcemapping starts working once inline source maps are used instead of external ones,
    using jest directly (instead of ts-jest) allows us to skip the compilation step and save a little time (in a build routine, compilation may already happen before the tests are run)

if you use inline source maps, try to only do so in a debug build that isn't published -- you probably wouldn't want to publish to npm the js files which contain the big fat inline sourcemaps, it'd be terrible if everyone did that

chase-moskal avatar Apr 04 '18 00:04 chase-moskal

Jest should fully support sourcemaps out of the box now, as long as they are either inline or the transformer returns it (meaning we don't look up external sourcemaps).

@SimenB How does Jest support sourcemaps? (I was expecting to see in babel-jest something like in felipeochoa/jest-sourcemaps, ie calling babel.transform with {sourceMap: true}, and passing these outputed sourcemaps to node-source-map-support).

Edit: I'm looking at script_transformer.js in jest-runtime, should be ok for me to understand how jest support sourcemaps.

dfroger avatar Apr 10 '18 14:04 dfroger

Did you answer your own question? We might want to document how to utilise sourcemaps in Jest, but I'm not sure where to put it. It works out of the box, but custom transforms have to support it explicitly (by returning code and map properties)

SimenB avatar Apr 10 '18 20:04 SimenB