【问题标题】:getting nyc/istanbul coverage report to work with typescript让 nyc/istanbul 覆盖报告与打字稿一起使用
【发布时间】:2017-06-22 14:14:19
【问题描述】:

我正在努力为我的 typescript/mocha/gulp 项目获得 nyc/istanbul 的适当报道。我尝试了很多方法,其中一些似乎无法使用源映射,而其他一些由于ts-node/tsc 错误而失败。我目前的设置是:

nycpackage.json中的相关配置

"scripts": {
    "test:coverage": "nyc npm run test:unit",
    "test:unit": "gulp mocha"
}
"nyc": {
    "check-coverage": true,
    "all": true,
    "extension": [
      ".js",
      ".jsx",
      ".ts",
      ".tsx"
    ],
    "include": [
      "src/**/!(*.test.*).[tj]s?(x)"
    ],
    "reporter": [
      "html",
      "lcov",
      "text",
      "text-summary"
    ],
    "report-dir": "docs/reports/coverage"
  }

gulpfile.jsmocha相关部分

const SRC_DIR = path.join(__dirname, 'src');
const SRC_FILES = path.join(SRC_DIR, '**', '*.[jt]s?(x)');
const TEST_FILES = path.join(SRC_DIR, '**', '*.test.[jt]s?(x)');
const MOCHA_CONFIG = {
    src: [
        TEST_FILES
    ],
    watchSrc: [
        SRC_FILES,
        TEST_FILES
    ],
    mocha: {
        // compilers: [
        //     'ts:ts-node/register',
        //     'tsx:ts-node/register'
        // ],
        require: [
            './tests/setup.js',
            'ignore-styles',
            'source-map-support/register'
        ]
    }
};
gulp.task('mocha', mocha(MOCHA_CONFIG));

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "rootDir": "./src",
    "outDir": "./build",
    "allowJs": true,
    "module": "commonjs",
    "target": "es5",
    "lib": ["es5", "es6", "dom"],
    "sourceMap": true,
    "inlineSourceMap": false,
    "inlineSources": false,
    "experimentalDecorators": true,
    "noUnusedParameters": true,
    "noUnusedLocals": true,
    "jsx": "react",
    "moduleResolution": "node"
  },
  "exclude": [
    "docs",
    "tests",
    "**/*.test.js",
    "**/*.test.jsx",
    "**/*.test.ts",
    "**/*.test.tsx",
    "tools",
    "gulpfile.js",
    "node_modules",
    "build",
    "typings/main",
    "typings/main.d.ts"
  ],
  "awesomeTypescriptLoaderOptions": {
    "useCache": true,
    "useBabel": true
  }
}

使用上述设置覆盖会为所有文件生成结果,但它们对于 TS 文件不正确,很可能是由于未使用源映射(即报告显示没有覆盖作为 cmets 的行并且数字似乎也是错误的) )。

尝试了多种变体方法但均未成功,最常见的建议之一是将"require": ["ts-node/register"] 添加到nyc 配置中,然后我收到错误抱怨,即gulpfile.jsdocs/reports/coverage/lcov-report/prettify.js 和数量其他JS文件为not under 'rootDir',这是正确的,但尚不清楚为什么ts-node会尝试处理src中的所有文件,即使它们被排除在tsconfig.json中(仍然配置变得非常复杂)。

如果有任何建议可以得到正确的 TS 文件覆盖率报告,我将不胜感激。

【问题讨论】:

  • 设置正确的覆盖测试有时会很麻烦。我停止在 React-Typescript 项目中使用 Mocha/Istanbul,转而使用 Jest 和 ts-jest (github.com/kulshekhar/ts-jest),它需要伊斯坦布尔以及 React 测试所需的所有好东西。这对我来说是跨不同版本的稳定堆栈。
  • 感谢您的建议!虽然实际上这是我的考虑之一,但我是否正确它可能需要重写,因为 jestmocha 不同?
  • 摩卡测试很容易改写成笑话。删除描述部分并将 it() 更改为 test()。说法也不同。但是 before* 和 after* 部分是相同的。需要一些时间,但不长...
  • 以及它如何适合(或比较与 mocha/sinon/chai)为非反应的东西编写测试?
  • 就像摩卡一样简单!

标签: javascript typescript mocha.js istanbul tsconfig


【解决方案1】:

最近我在tsconfig.jsoncompilerOptions 中使用"target": "es6" 而不是es5 找到了一个令人满意的解决方案。虽然直接在tsconfig.json 中更改target 可能不是一个选项,因为它会影响构建,但另一个技巧是使用TS_NODE_COMPILER_OPTIONS='{"target":"es6"},它可以直接添加到package.json scripts 中,即:

"test:coverage": "TS_NODE_COMPILER_OPTIONS='{\"target\":\"es6\"}' nyc npm run test:unit",

test:unit 是用于运行实际测试的任何方式(在我的情况下只是 gulp mocha

注意:我还按照https://github.com/istanbuljs/nyc/issues/618 线程的建议将 nyc 更新为最新的 11.1.0 和 ts-node 更新为 3.3.0

【讨论】:

  • 当我得到“ES5/ES3 中的异步函数或方法需要 'Promise' 构造函数时,此方法解决了我的问题。确保您有 'Promise' 构造函数的声明或包含 'ES2015 ' 在您的 --lib 选项中“错误”
【解决方案2】:

我不确定这是不是同一个问题,但我会把它放在这里以防它对未来的开发人员有所帮助...

在我将exclude-after-remap=false 添加到我的package.jsonnyc 部分之前,我没有获得任何覆盖率数据。

这是在in the documentation 上列出的,但不是以非常突出的方式 (IMO)。

【讨论】:

    【解决方案3】:

    由于许多更改破坏了旧的工作设置,我创建了一个详细的示例项目,涵盖 typescript + mocha + nyc 支持对非调用文件(这通常不包括在示例中)以及一些单元测试示例和质量的适当覆盖使用最新版本进行检查。

    我在去 mocha 8+ nyc 15+ 时遇到了几个问题。也许它也有助于其他人偶然发现它。

    https://github.com/Flowkap/typescript-node-template

    如果您只对覆盖感兴趣,请检查 .ncyrc.yml 和 mocharc.yml 以及 package.json 中的调用配置。 VsCode 启动配置还包括:

    .nycrc.yml

    extends: "@istanbuljs/nyc-config-typescript"
    
    reporter:
      - html
      - lcovonly
      - clover
      # those 2 are for commandline outputs
      - text
      - text-summary
    report-dir: coverage
    

    .mocharc.yml

    require:
      - ts-node/register
      - source-map-support/register
    recursive: true
    color: true
    exit: true
    extension:
      - ts
      - test.ts
    

    package.json 中的测试作业

    "test": "npm run lint && nyc mocha src test",
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 2018-09-13
      • 1970-01-01
      • 2019-12-14
      • 2017-07-02
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      相关资源
      最近更新 更多