Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for transformers to opt files out of coverage? #5209

Closed
JoshuaKGoldberg opened this issue Jan 2, 2018 · 15 comments
Closed

API for transformers to opt files out of coverage? #5209

JoshuaKGoldberg opened this issue Jan 2, 2018 · 15 comments

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Jan 2, 2018

Continuing discussion with @kulshekhar and @GeeWee in kulshekhar/ts-jest#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.

@GeeWee
Copy link

GeeWee commented Jan 5, 2018

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
Copy link
Member

SimenB commented Jan 6, 2018

@nottoseethesun
Copy link

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

@RehanSaeed
Copy link

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

@SimenB
Copy link
Member

SimenB commented Jan 16, 2018

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.

@JoshuaKGoldberg
Copy link
Contributor Author

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

λ npm run test -- --coverage

> ts-jest-wallaby-vscode@2.0.2 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

@RehanSaeed
Copy link

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}",
  ]

@SimenB
Copy link
Member

SimenB commented Jan 22, 2018

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

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

@JoshuaKGoldberg
Copy link
Contributor Author

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

@webia1
Copy link

webia1 commented May 18, 2018

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

@srbarba
Copy link

srbarba commented Oct 22, 2020

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

collectCoverageFrom: [
  '**/*.[tj]s', // includes js and ts files
  '!**/*.d.ts' // excludes .d.ts files
]

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@marekdedic
Copy link

Death to the stale bot!

Jk, this is still relevant.

@github-actions github-actions bot removed the Stale label Feb 25, 2022
@SimenB
Copy link
Member

SimenB commented Feb 27, 2022

I clones the repository in the OP, and just updating all versions to latest shows correct coverage (I guess?). Probably fixed by some source map change at some point through the years

image

types.ts is gone at least. Fixing the ts-jest warning makes no difference.

If this is still an issue, please open a new issue with a reproduction using Jest 27 (or jest 28 alpha, but I don't think anything there has changed anything from v27)

@SimenB SimenB closed this as completed Feb 27, 2022
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants