Node - 如何在使用 nyc 和 mocha 时使用 source-map?

13

所以nyc正在对我的文件进行以下更改:

  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)

我不确定如何使用源映射来还原这个代码。文档中提到:

使用源映射生成准确的堆栈跟踪。

当produce-source-map设置为true时,被检测过的源文件将包括用于检测转换的内联源映射。与source-map-support结合使用,被检测过的代码的堆栈跟踪将反映它们的原始行。

因此,我尝试了以下npm运行命令:

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

结合纽约市的背景:

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

但是这条线仍然缠绕在一起。

1个回答

13

它能够正常工作的基本前提是(如此处所述):

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

请确保 nyc 的版本为 ^10.3.210.3.0 版本存在问题)。

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

nyc 配置应该是 "sourceMap": true, "produce-source-map": true

而且文档解释了如何使用它们:

CLI Usage

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

编程使用

在编译文件的顶部添加以下行。

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

可以通过添加注释来定义映射文件名:

//# sourceMappingURL=filename.js.map

我正在尝试使用他们的示例 $ mocha --require source-map-support/register tests/,但这样做毫无意义,因为我可以在第一时间就得到正确的行错误。我只是想弄清楚如何将其与nyc结合使用。 - A. L
produce-source-map设置为true时,被检测到的源文件将包含内联源映射...其中“inline”表示它们的位置。 sourceMappingURL可用于不进行内联处理。问题跟踪器中有许多条目:https://github.com/istanbuljs/nyc/issues?utf8=%E2%9C%93&q=is%3Aopen+source-map - Martin Zeitler
发现另一个 nyc 配置,它使用 sourceMap 而不是 source-map - Martin Zeitler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接