node - how to use source-map with nyc and mocha
Asked Answered
B

1

15

So nyc is mangling my files as follows:

  at _onCreate (src/post/admin.js:1:10453)
  at doQuery (src/db.js:59:216)
  at process._tickCallback (internal/process/next_tick.js:68:7)

I am unsure of how to use a source map to unmangle this. The docs state:

Accurate stack traces using source-maps.

When produce-source-map is set to true, then the instrumented source files will include inline source maps for the instrumenter transform. When combined with source-map-support, stack traces for instrumented code will reflect their original lines.

So I tried the following npm run command:

"NODE_ENV=test nyc mocha --require ./tests/setup.js --require source-map-support/register --produce-source-map true --bail ./tests/unit/$FILE"

combined with the nyc setting:

"nyc": {
    "include": [
        "src"
    ],
    "exclude": [
        "./tmp/**/*",
        "./tests"
    ],
    "instrument": true,
    "report-dir": "./tests/coverage",
    "temp-dir": "./tests/temp",
    "source-map": true,
    "produce-source-map": true
}

but the line is still mangled.

Broadbrim answered 24/10, 2018 at 1:39 Comment(0)
L
14

the basic pre-condition for it to work would be (as described here):

npm install --save-dev source-map-support

make sure nyc is ^10.3.2 (10.3.0 was broken).

"devDependencies": {
    ...
    "mocha": "^3.3.0",
    "nyc": "^10.3.2",
    "source-map-support": "^0.4.15",
}

the nyc config should be "sourceMap": true, "produce-source-map": true.

and the documentation explains how to use them:

CLI Usage

node -r source-map-support/register compiled.js

Programmatic Usage

Put the following line at the top of the compiled file.

require('source-map-support').install();

one can also define mapping file-names by adding comments:

//# sourceMappingURL=filename.js.map
Ledezma answered 26/10, 2018 at 3:46 Comment(3)
Well I'm trying to use in their example of $ mocha --require source-map-support/register tests/, except that's pretty pointless since I can get the right line error without it in the first place. I'm just trying to work out how to incorporate it with nyc.Broadbrim
@A.Lau when produce-source-map is set to true, then the instrumented source files will include inline source maps... where the word "inline" tells where to find them. sourceMappingURL can be used not to inline. the issue tracker has quite some entries: github.com/istanbuljs/nyc/…Ledezma
found another nyc config, which uses sourceMap instead of source-map.Ledezma

© 2022 - 2024 — McMap. All rights reserved.