【问题标题】:mocha nyc and source maps摩卡纽约和源地图
【发布时间】:2019-06-13 14:14:54
【问题描述】:

所以。我试图弄清楚为什么这不起作用。

我正在 srctests 目录中的文件中编写源代码和测试 TypeScript 代码。这些是*.ts*.spec.ts 文件。

我使用npx tsc 将源文件和测试文件都转译到目录build。所以这个目录现在包含*.js.*.spec.js*.js.map*.spec.js.map 文件类型。

我现在如何设置 mocha 和 nyc 来使用它?

我有以下一套。

.nycrc.json

{
    "extends": "@istanbuljs/nyc-config-typescript",
    "all": true,
    "branches": 0,
    "lines": 0,
    "functions": 0,
    "statements": 0,
    "check-coverage": true,
    "exclude": [".ignore", "coverage"],
    "report-dir": "./coverage/",
    "cache": false,
    "source-map": true,
    "produce-source-map": true
}

tsconfig.json:

{
    "compilerOptions": {
        "allowJs": true,
        "noImplicitAny": false,
        "strictNullChecks": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "module": "commonjs",
        "target": "es2016",
        "lib": ["es2016"],
        "moduleResolution": "node",
        "types": ["mocha", "node"],
        "typeRoots": ["node_modules/@types"],
        "sourceMap": true,
        "outDir": "./build/",
        "skipLibCheck": true,
        "removeComments": false
    },
    "include": ["./**/*.ts", "./**/*.js"]
}

错误:

karl@karl-Dell-Precision-M3800:~/dev/escd$ NODE_ENV=test npx nyc --reporter=html --reporter=text mocha "./build/tests/**/*.js"
mappedCoverage.addStatement is not a function

但是,如果我在 .nycrc.json 文件中将 all 设置为 false,那么它不会抱怨。怎么了?

我不想使用ts-node 或类似的东西,我已经转译了这些文件。

【问题讨论】:

  • 我在这里给出了另一个帖子的详细示例,它可能是重复的:stackoverflow.com/a/64829161/2538750 这似乎是相同的原因(nyc 没有为 typescript 正确配置。设置可能很棘手。

标签: typescript mocha.js source-maps nyc


【解决方案1】:

https://github.com/istanbuljs/nyc#source-map-support-for-pre-instrumented-codebases

我必须删除 "extends": "@istanbuljs/nyc-config-typescript", 并添加 "exclude-after-remap": false,

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多