【问题标题】:nyc mocha is generating nothing despite providing nyc config to package.json尽管向 package.json 提供了 nyc 配置,但 nyc mocha 什么也没产生
【发布时间】:2022-11-01 19:28:51
【问题描述】:

我在使用 nyc + mocha 时遇到了一些麻烦。我的测试通过了,但我没有收到任何报告,尽管在我的包 json 中添加了一个 nyc 配置对象:

//package.json
{
  "name": "open-concepts",
  "version": "1.0.0",
  "description": "Simply matching idea makers and doers",
  "main": "app.js",
  "scripts": {
    "start": "nodemon --watch './**/*.ts' --exec 'ts-node-esm' app.ts",
    "test": "NODE_ENV='test' nyc mocha -r ts-node/register 'test/**/*.*.*.ts' --exit",
   },
"nyc": {
    "extends": "@istanbuljs/nyc-config-typescript",
    "check-coverage": true,
    "all": true,
    "include": [
      "test/**/*.*.[tj]s?(x)"
    ],
    "reporter": [
      "html",
      "lcov",
      "text",
      "text-summary",
      "cobertura"
    ],
    "report-dir": "coverage"
  }
}

当我运行测试时,我得到这个错误:

 91 passing (4s)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------

=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================
runner.once is not a function <-- I don't know where this comes from...

我的nyc 配置中是否缺少某些内容?

蒂亚!

【问题讨论】:

    标签: node.js mocha.js nyc


    【解决方案1】:

    我认为问题在于您的包含声明:

        "include": [ "test/**/*.*.[tj]s?(x)" ]
    

    这应该与您想要接收覆盖的 src 文件相对应,而不是测试文件本身。您可能希望从覆盖率报告中排除测试文件。

    我在很多地方使用的 nyc 配置是:

    "nyc": {
        "all": true,
        "extension": [
          ".ts",
          ".tsx"
        ],
        "exclude": [
          "**/*.d.ts",
          "**/*.js",
          "**/*.spec.ts"
        ],
        "reporter": [
          "text",
          "html",
          "cobertura"
        ],
        "require": ["ts-node/register"]
      }
    

    所有 .ts 或 .tsx 文件都通过扩展参数包含在内,然后 exclude 过滤我们的测试文件、一些生成的 ts 文件以及任何 JS。

    如果您只是在寻找 JS 文件覆盖率,请相应地切换扩展参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多