【问题标题】:node - how to use source-map with nyc and mochanode - 如何在 nyc 和 mocha 中使用 source-map
【发布时间】:2018-10-24 01:39:55
【问题描述】:

所以 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)

我不确定如何使用源映射来解决这个问题。文档状态:

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

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

所以我尝试了以下 npm run 命令:

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

结合 nyc 设置:

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

但这条线仍然损坏。

【问题讨论】:

    标签: javascript node.js mocha.js istanbul nyc


    【解决方案1】:

    它工作的基本前提是(如here 所述):

    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

    documentation 解释了如何使用它们:

    CLI 用法

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

    程序化使用

    将以下行放在编译文件的顶部。

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

    还可以通过添加 cmets 来定义映射文件名:

    //# sourceMappingURL=filename.js.map
    

    【讨论】:

    • 好吧,我正在尝试在他们的$ mocha --require source-map-support/register tests/ 示例中使用,但那毫无意义,因为我一开始就可以在没有它的情况下得到正确的行错误。我只是想弄清楚如何将它与纽约市合并。
    • @A.Lau 当produce-source-map 设置为true 时,检测的源文件将包括内联源映射......其中“内联”一词告诉在哪里可以找到它们。 sourceMappingURL 可用于不内联。问题跟踪器有很多条目:github.com/istanbuljs/nyc/…
    • 找到另一个nyc 配置,它使用sourceMap 而不是source-map
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2013-09-06
    相关资源
    最近更新 更多