没有报道nyc摩卡

时间:2018-05-22 04:34:59

标签: node.js mocha istanbul

即使测试用例正在通过,我也无法弄清楚为什么测试覆盖率为0。我在package.json中有一个脚本:

"nyctest": "node --max_old_space_size=4096 node_modules/nyc/bin/nyc.js --reporter=text mocha"

当我运行npm run nyctest

我的测试通过,但覆盖率为0%。

enter image description here

以下是测试,文件正在测试:

test.js

var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
chai.should();
chai.use(sinonChai);
var application = require('../../../src/main/resources/static/js/components/app.js');

describe('sample return testing', function(){
    it('should return true', function(){
        application.sample.returnValue().should.equal(true);
    })
});

app.js

const sample = {
    returnValue: function () {
        return true;
    }
};

module.exports = {sample};

感谢任何帮助。

5 个答案:

答案 0 :(得分:3)

我有类似的问题。我认为您需要使用-all 标志修改 nyctest 命令。

我的命令如下:

“ nyctest”:“ nyc --reporter = lcov --reporter = text-lcov --all -x \” ./ node_modules / \“ -x \” ./ coverage / \“ check-coverage --lines 10 --function 90 npm run unittest”

因此,通过-all 标志,将提取所有文件:)

答案 1 :(得分:3)

到2020年8月:两件事:

  1. 您需要添加de --all参数。
  2. 最好使用.nycrc文件,以使package.json中的命令行不大,如下所示:

最小的.nycrc文件:

{
  "all": true,
  "include": [
    "test/**.js"
  ],
  "exclude": [
  ]
}

答案 2 :(得分:2)

就我而言,问题是.nyrc配置包含"instrument": false。删除此内容会带回覆盖率报告。

答案 3 :(得分:2)

TL;DR:在您的纽约配置中尝试 useSpawnWrap: true


问题的根本原因在于 nyc 检测文件以进行覆盖的方式。它通过观察它的子进程并跟踪运行的代码来做到这一点。这就是您将 mocha 传递给 nyc

的原因

您可以通过将 --show-process-tree 标志添加到 nyc, as described in the nyc docs 来测试是否是这种情况。如果您在输出的底部看到“nyc”,但在其下方没有任何内容,您就会知道这是您的问题。

现在,上面的注释解释了如何解决这个问题 (useSpawnWrap: true),但是是什么原因造成的?这 GitHub issue comment 部分解释了正在发生的事情,而 this comment 解释了 pnpm 如何破坏这种能力。在我的情况下,我使用 nvm 来解决这个问题,尽管我确定它是相同的根本原因,但某些东西正在影响路径或 process.env.NODE_OPTIONS 环境变量。

我希望这个更详细的解释可以帮助您在未来解决此类问题。

答案 4 :(得分:1)

我的情况有些不同,

我已经全局安装了mocha,并且nyc全局引用了摩卡时,它在覆盖率报告中没有显示任何内容。

运行时我得到了结果

nyc node_modules/.bin/mocha

这是一个已知问题,请参阅此处 https://github.com/istanbuljs/nyc/issues/1029#issuecomment-514174340