nyc (istanbul) exclude test code from coverage reports

陌路散爱 提交于 2020-02-03 10:10:50

问题


I'm trying to add coverage report generation to a typescript library project.

The layout includes these directories:

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

I have a test script in package.json that appears to work correctly:

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

(it generates 1413 lines of output; currently all tests are passing)

I'm trying to figure out how to run nyc so that

  1. All tests in test are run
  2. No files in test are included in the coverage report
  3. All code files in lib are included in the coverage report
  4. No type info files in lib are included in the coverage report

I think I'm getting #1 right with this command:

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

which has this output:

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

(but more colorful; I'm using --reporter nyan because I'd rather not repeat the 1413 lines of output from the test script)

That output fails goals 2 and 3; this question is about goal 2:

How do I tell nyc to exclude test from the coverage report?

(It also bothers me that the report is wrong - there are no tests that cover other tests, - but that is not what this question is about.)

I've tried adding a variety of includes and excludes to the command, none of which have affected the output:

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

(As you can see, I'm not sure what the basedir is for these; I wasn't expecting to see the source file names in the report, but nyc is clearly doing something with both of src and build but not showing either in the report.)

I tried the include options after reading this answer, and it didn't help. After adding excludes for lib and seeing no effect, I'm getting the impression that the exclude option just doesn't work. I won't switch to ES6 as suggested in this answer until noone I work with still supports IE.


回答1:


Looking at this again after reaching 100% test coverage and wanting nyc check-coverage --branches 100 --functions 100 --lines 100 --statements 100 to pass I found this answer:

Right, after some digging I've managed to get this working. I've added

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

to my package.json.

Which lead me to this working solution in mine:

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

(In between I added paths src/testlib and build/testlib containing helpers used only in tests, which should not be run as tests, and which should be excluded from test coverage reporting.)



来源:https://stackoverflow.com/questions/50080629/nyc-istanbul-exclude-test-code-from-coverage-reports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!