Jest: API for transformers to opt files out of coverage?

Created on 2 Jan 2018  ·  11Comments  ·  Source: facebook/jest

Continuing discussion with @kulshekhar and @GeeWee in https://github.com/kulshekhar/ts-jest/issues/378.

In TypeScript, it's not uncommon to have files with no output JS code, just interface/type definitions:

// types.ts
export type MyNumber = number;

These end up being counted as 2/2 uncovered lines in the file because the file does get compiled into a .js equivalent:

// types.js (what it would look like)
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=types.js.map

Here's a min repro: https://github.com/JoshuaKGoldberg/ts-jest-wallaby-vscode/tree/ts-jest-coverage-repro

The simplest approach I can think of would be to allow ts-jest to mark the file as excluded from code coverage.

Most helpful comment

And do you finally have a simple solution about "Including *.ts and excluding *.d.ts" ?

All 11 comments

Yes some sort of way to exclude files from coverage would be nice somehow, we've debated several ways to do it on our side, but haven't come up with anything that seemed acceptable.

@SimenB - Because, for example, in TypeScript projects it is common to have type definition files (*.d.ts), so default behavior should not require users to manually configure such configuration options in order to get the desired result.

Shouldn't empty modules be marked as 100% covered by default anyway?

coverage is for lines executed, blank lines don't count. You might want to raise an issue with istanbul if you think 0/0 lines covered should mean 100%, it's not something controlled from Jest's side.

Unfortunately it's not 0/0 lines covered; it's 0/2 lines covered.

λ npm run test -- --coverage

> [email protected] test C:\Code\ts-jest-wallaby-vscode
> jest "--coverage"

 PASS  tests\sum.test.ts
  √ adds 1 + 2 to equal 3 (4ms)

----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |       50 |      100 |      100 |       60 |                |
 sum.ts   |      100 |      100 |      100 |      100 |                |
 types.ts |        0 |      100 |      100 |        0 |            1,2 |
----------|----------|----------|----------|----------|----------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.777s
Ran all test suites.
types.ts
0% Statements 0/3 100% Branches 0/0 100% Functions 0/0 0% Lines 0/2

If anyone is using TypeScript + TS-Lint, then adding I at the beginning of interfaces is a requirement. Happily, this means that you can use the following Jest rule to exclude them from your code coverage:

 "collectCoverageFrom": [
      "**/*.{ts}",
      "!**/I[A-Z]*.{ts}",
  ]

Unfortunately it's not 0/0 lines covered; it's 0/2 lines covered.

Ah... 🙁 It also has Stmts though, how is that counted?

TypeScript + TS-Lint
I at the beginning of interfaces is a requirement

You can change that behavior (link). Some projects, including TypeScript itself, enforce the opposite.

Also, Internal.ts -> coverage?

Stmts

0/3 statements ☹️

And do you finally have a simple solution about "Including *.ts and excluding *.d.ts" ?

If you are using *.d.ts files you can use

collectCoverageFrom: [
  '**/*.[tj]s', // includes js and ts files
  '!**/*.d.ts' // excludes .d.ts files
]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nsand picture nsand  ·  3Comments

ianp picture ianp  ·  3Comments

Secretmapper picture Secretmapper  ·  3Comments

paularmstrong picture paularmstrong  ·  3Comments

samzhang111 picture samzhang111  ·  3Comments