nyc(伊斯坦布尔)从覆盖率报告中排除测试代码

时间:2018-04-28 20:10:47

标签: typescript mocha code-coverage 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

我在package.json中有一个似乎正常工作的测试脚本:

    "test": "mocha --reporter mocha-multi --reporter-options mocha-multi=mocha-multi.json --recursive build/test/**/*.spec.js",

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

我试图找出如何运行nyc以便

  1. test中的所有测试都已运行
  2. 覆盖率报告中未包含test中的任何文件
  3. lib中的所有代码文件都包含在覆盖率报告中
  4. 覆盖率报告中未包含lib中的类型信息文件
  5. 我认为我使用此命令正确#1:

    npx nyc mocha --cache --reporter nyan build/test
    

    有这个输出:

     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 |                   |
    ---------------------|----------|----------|----------|----------|-------------------|
    

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

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

    如何告知nyc从覆盖率报告中排除test

    (令我困扰的是报告错了 - 没有其他测试的测试,但这不是这个问题的关键。)

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

    • -x build / test
    • -x' ** / * .spec.js'
    • -x' ** / * .spec.tsx'
    • -x' build / test / **'
    • -x test
    • -x' build / test / ** / * .spec.js'
    • -x' build / test / ** / * .spec.tsx'
    • -x' test / **'
    • -x' test / ** / *。spec.js'
    • -x' test / ** / * .spec.tsx'
    • -n lib
    • -n build / lib
    • -n src / lib
    • -x build / lib
    • -x lib
    • -x src / lib
    • -X test
    • -X build / test
    • -X src / test

    (正如您所看到的,我不确定基础是什么;我不希望在报告中看到源文件名,但nyc显然在做某事同时显示srcbuild但未在报告中显示。)

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

1 个答案:

答案 0 :(得分:1)

在达到100%的测试覆盖率并希望nyc check-coverage --branches 100 --functions 100 --lines 100 --statements 100通过之后,我再次发现了这一点:this answer

  

对,经过一些挖掘,我设法使它开始工作。我添加了

"nyc": {
  "include": "app", - only looks in the app folder
  "exclude": "**/*.spec.js" 
}
     

到我的package.json。

这使我想到了我的这个可行的解决方案:

"nyc": {
  "include": [ "build/lib/**/*.js" ],
  "exclude": [ "build/test/**/*.spec.js", "build/testlib/**/*.js" ]
}

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