nyc (istanbul) 从覆盖率报告中排除测试代码

Sha*_*ing 6 code-coverage mocha.js typescript istanbul nyc

我正在尝试将覆盖率报告生成添加到打字稿库项目中。

布局包括以下目录:

project
?? src
?  ?? lib      ## project code (typescript), *.tsx
?  ?? test     ## test specs (typescript), *.spec.tsx
?  ?? @types   ## types I made for a few dependencies that didn't have them
?? build
   ?? lib      ## compiled project code (ES5 + type info, with inline sourcemaps), *.js and *.d.ts
   ?? test     ## compiled test specs (ES5 + type info, with inline sourcemaps), *.spec.js and *.spec.d.ts
Run Code Online (Sandbox Code Playgroud)

我有一个测试脚本,package.json它似乎可以正常工作:

    "test": "mocha --reporter mocha-multi --reporter-options mocha-multi=mocha-multi.json --recursive build/test/**/*.spec.js",
Run Code Online (Sandbox Code Playgroud)

(它生成 1413 行输出;目前所有测试都通过)

我试图找出如何运行nyc,使

  1. test运行中的所有测试
  2. test覆盖率报告中不包含任何文件
  3. 中的所有代码文件lib都包含在覆盖率报告中
  4. lib覆盖率报告中不包含类型信息文件

我想我用这个命令获得了#1:

npx nyc mocha --cache --reporter nyan build/test
Run Code Online (Sandbox Code Playgroud)

有这个输出:

 872 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_,------,
 0   _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_|   /\_/\
 0   _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-^|__( ^ .^)
     _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-  ""  ""

  872 passing (20s)

---------------------|----------|----------|----------|----------|-------------------|
File                 |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------------|----------|----------|----------|----------|-------------------|
All files            |    88.97 |    91.49 |    84.09 |    89.11 |                   |
 lib                 |    88.51 |    91.49 |    83.33 |    88.62 |                   |
  Awaitable.tsx      |    88.51 |    91.49 |    83.33 |    88.62 |... 79,405,419,420 |
 test                |      100 |      100 |      100 |      100 |                   |
  Abortable.spec.tsx |      100 |      100 |      100 |      100 |                   |
  Awaitable.spec.tsx |      100 |      100 |      100 |      100 |                   |
---------------------|----------|----------|----------|----------|-------------------|
Run Code Online (Sandbox Code Playgroud)

(但更丰富多彩;我使用--reporter nyan是因为我不想重复测试脚本的 1413 行输出)

该输出未能达到目标 2 和 3;这个问题是关于目标 2:

我如何告诉 nyctest从覆盖率报告中排除?

(报告错误也让我感到困扰——没有涵盖其他测试的测试,但这不是这个问题的内容。)

我尝试在命令中添加各种包含和排除,但都没有影响输出:

  • -x 构建/测试
  • -x '**/*.spec.js'
  • -x '**/*.spec.tsx'
  • -x '构建/测试/**'
  • -x 测试
  • -x '构建/测试/**/*.spec.js'
  • -x '构建/测试/**/*.spec.tsx'
  • -x '测试/**'
  • -x 'test/**/*.spec.js'
  • -x 'test/**/*.spec.tsx'
  • -n 库
  • -n 构建/库
  • -n 源代码/库
  • -x 构建/库
  • -x 库
  • -x 源代码/库
  • -X 测试
  • -X 构建/测试
  • -X 源代码/测试

(正如你所看到的,我不知道该BASEDIR是什么这些,我不希望看到该报告中的源文件名,但nyc显然这样做既东西srcbuild,但不显示在报告中无论是。 )

阅读此答案后,我尝试了包含选项,但没有帮助。在为 lib 添加排除项并没有看到任何效果后,我得到的印象是排除选项不起作用。我不会按照这个答案中的建议切换到 ES6,直到与我一起工作的人仍然支持 IE。

Sha*_*ing 6

在达到 100% 测试覆盖率并希望nyc check-coverage --branches 100 --functions 100 --lines 100 --statements 100通过后再次查看此问题,我找到了这个答案

是的,经过一番挖掘后,我设法让它发挥了作用。我已经添加

"nyc": {
  "include": "app", - only looks in the app folder
  "exclude": "**/*.spec.js" 
}
Run Code Online (Sandbox Code Playgroud)

到我的 package.json。

这导致我在我的这个工作解决方案:

"nyc": {
  "include": [ "build/lib/**/*.js" ],
  "exclude": [ "build/test/**/*.spec.js", "build/testlib/**/*.js" ]
}
Run Code Online (Sandbox Code Playgroud)

(在此期间,我添加了路径src/testlibbuild/testlib包含仅在测试中使用的帮助程序,这些帮助程序不应作为测试运行,并且应从测试覆盖率报告中排除。)