piotrwitek / ts-mocha

Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity
MIT License
190 stars 25 forks source link

Using nyc with ts-mocha #42

Closed paulmelnikow closed 4 years ago

paulmelnikow commented 5 years ago

Hi! Are there any examples for using nyc with ts-mocha? We've just adopted ts-mocha in Shields in badges/shields#3722, and our frontend test coverage stopped working at the same time (at least, the new TypeScript files aren't getting included in the coverage reports, even if I add them to the config).

Shields is a relatively complicated repo and I had to fiddle with our nycrc to get frontend test coverage in the first place.

I found these resources for TS coverage with Istanbul/NYC, but they aren't for ts-mocha:

Some pointers or an example repo where this is working would be really helpful!

piotrwitek commented 5 years ago

Hey, could you share the error log?

paulmelnikow commented 5 years ago

Hi! The tests run without errors, however there's no mention of the .ts files in the coverage report.

Let me know if you'd like to take it for a spin. I could make a branch with a partial set of changes to the config.

JimLynchCodes commented 5 years ago

hey, so weird. I am also having issues with nyc and ts-mocha. For me, the lines report just shows up with all zeros.

nyc ts-mocha -p ./tsconfig.json ./**/*.test.ts --watch-extensions ts

Output:

 ClosestNumberFinder.getAscendingPairsOfClosestNumbers
    ✓ should return pairs of with the lowest absolute difference.

  1 passing (16ms)

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

Are there specific things we need to have in tsconfig or the .nycrc maybe in order for it to read the line coverage correctly?

JimLynchCodes commented 5 years ago

woah, check it out @piotrwitek. It says I am a contributor now! 😆

juank11memphis commented 5 years ago

Hello,

Has anybody found a solution for the issue of running nyc with ts-mocha yet? I am still getting the report with only zeros.

Thanks.

djorg83 commented 5 years ago

Try adding the --exit option to the end of your mocha arguments.

juank11memphis commented 5 years ago

Hi @djorg83 I was already using the --exit argument, that does not seems to fix the problem unfortunately.

ceisele-r commented 4 years ago

We are unfortunately experiencing the same issue of only reported zeros with the combination of ts-mocha and ncy - even though we already use ts-mocha .... --exit and have not yet found a solution to it.

ceisele-r commented 4 years ago

Oh well, I have to correct myself. I got it working by:

Now we have the coverage summary in the stdout and the coverage reports being built!

ceisele-r commented 4 years ago

Ok, further to add to my last answer - it only works for us if we put the nyc config in package.json.

If we put it in .nycrc file, it does not work and reports no coverage. But if we put it in package.json, it works.

mucsi96 commented 4 years ago

Let me share my experience as well. I got the same issue. Was able to resolve this only adding "extension": [".ts"], to nyc configuration.

Summary:

my config in .nycrc.json

{
  "cache": false,
  "extension": [".ts"],
  "check-coverage": true,
  "include": ["src/**"],
  "statements": 90,
  "branches": 90,
  "functions": 95,
  "lines": 90
}

my package.json

{
  "scripts": {
    "test": "ts-mocha test/**/*.test.ts --timeout 10000",
    "test:coverage": "nyc --reporter=html --reporter=text --reporter=text-summary npm test"
  }
}

output

ERROR: Coverage for branches (72.73%) does not meet global threshold (90%)
--------------------|----------|----------|----------|----------|-------------------|
File                |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|
All files           |    90.86 |    72.73 |    95.35 |       90 |                   |
 additionalLinks.ts |      100 |      100 |      100 |      100 |                   |
 class.ts           |    86.36 |       70 |      100 |    86.36 |          10,19,40 |
 dependencies.ts    |    78.57 |       50 |       75 |    76.92 |          15,16,19 |
 description.ts     |       75 |       50 |      100 |       75 |                 8 |
 enumeration.ts     |    90.91 |       50 |      100 |    90.91 |                10 |
 examples.ts        |      100 |      100 |      100 |      100 |                   |
 function.ts        |    96.15 |       80 |      100 |    95.24 |                42 |
 index.ts           |       85 |    66.67 |       75 |    83.33 |          13,14,23 |
 reflection.ts      |    92.86 |    83.33 |      100 |    92.86 |                21 |
 subSection.ts      |      100 |      100 |      100 |      100 |                   |
 title.ts           |      100 |      100 |      100 |      100 |                   |
 type.ts            |    88.89 |     62.5 |      100 |     87.5 |             13,25 |
 typeInfo.ts        |       80 |       50 |      100 |       80 |                 8 |
 variable.ts        |      100 |      100 |      100 |      100 |                   |
--------------------|----------|----------|----------|----------|-------------------|

=============================== Coverage summary ===============================
Statements   : 90.86% ( 159/175 )
Branches     : 72.73% ( 48/66 )
Functions    : 95.35% ( 41/43 )
Lines        : 90% ( 144/160 )
================================================================================
juank11memphis commented 4 years ago

Thanks a lot @mucsi96 that did the trick for me!! :)

walterdl commented 4 years ago

@mucsi96 it works like a charm!

EyMaddis commented 3 years ago

unfortunately this is not true when using yarn run, I guess yarn may create a new sub shell for every call... When using yarn, the statements count is also way of, I guess it is testing the internals of yarn

chordmemory commented 1 year ago

I'm having mixed results with nyc and ts-mocha. Using similar configs too mucsi96, it seems like any file with typescript syntax is silently failing to be instrumented by nyc, resulting in it missing from the coverage report.