Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node - how to use source-map with nyc and mocha

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.

like image 459
A. L Avatar asked Oct 24 '18 01:10

A. L


1 Answers

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
like image 153
Martin Zeitler Avatar answered Nov 15 '22 01:11

Martin Zeitler