Jest: not able to run the coverage for one file

Created on 29 Nov 2018  ·  22Comments  ·  Source: facebook/jest

🚀 Feature Proposal

coverage shows for full run but for specific files nothing or mixed

I want to be able to run with:

  • o option from jest menu
  • jest --watch --coverage STRING_FOR_REGEX_TESTS_SELECTION
  • jest --watch --coverage --runTestsByPath SPECIFIC_FILE_PATH

And have the coverage for the files impacted only.

Motivation

it's all about productivity.

1. Tech Debt of tests

When we work on tech debt of tests => it's generally one by one (file by file).
So I mostly run targeting the file i am working on. The run in watch mode is faster this way. I would like to see the coverage related to the current file i am targeting (should be faster to generate?)

2. Same thing apply for development of new features

Devs don't do TDD most of the time and do the test after. Testing files one by one is faster especially if you split between multiple developers the work.

this is a reality happening in a lot of companies.

Example

Jest version: "23.6.0"
jest config:

module.exports = {
  preset: 'jest-preset-angular',
  setupTestFrameworkScriptFile: '<rootDir>/src/setupJest.ts',
  roots: ['src/app'],
  moduleFileExtensions: ['ts', 'js']
};

in this repo branch:
(sorry i didn't clean i just commited the state since i am playing with jest to help in vscode-jest repo)
https://github.com/nasreddineskandrani/full-example-angular/tree/jest-playaround

  1. clone the repo
  2. yarn install

-- Example 1 Steps

  • yarn run testjest:watch -- --coverage
  • run all tests (the coverage is fine) and the coverage result is:
    image
  • now use w then o (Press o to only run tests related to changed files.)
    then you change the file src\app\game\game-a.service.spec.ts the condition true to false and vise versa
    Resulting coverage (PROBLEM):
    image

-- Example 2 Steps

when i run:
yarn run testjest:watch -- --coverage game-a.service.spec.ts
I get:
image

WHY HERO.SERVICE impacted here? setupJest.ts ....?

-- Example 3 Steps

when i run:
yarn run testjest:watch --coverage --runTestsByPath src/app/game/game-a.service.spec.ts
I get:
image

WHY HERO.SERVICE impacted here? setupJest.ts ....?

Result expected:

  • I want to be able to see only the coverage of the file i changed
  • I want to be able to see only the coverage of the file(s) i watch (with regex or path)
    => without doing any custom config since the file(s) impacted are known.

Most helpful comment

Doesn't this solve the issue?
--collectCoverageOnlyFrom

All 22 comments

hey @nasreddineskandrani, thanks for using Jest - this is not a bug, but it is a pretty good feature request

Can you update the description to use the feature request template?

done. waiting for feedback :)

Wow, awesome change thanks 👍

Is anybody working on this right now?

Nope, feel free to tackle it if you've got some time!

Doesn't this solve the issue?
--collectCoverageOnlyFrom

jest --coverage --collectCoverageFrom=path/to/your/file.js

jest --coverage --collectCoverageFrom=path/to/your/file.js

This does not work for me, it runs a full test run regardless of what is specified - i.e. collectCoverageFrom does nothing.

jest --coverage --collectCoverageFrom=path/to/your/file.js

This does not work for me, it runs a full test run regardless of what is specified - i.e. collectCoverageFrom does nothing.

please, try this way.
example:

yarn jest app/components/search/__tests__/SearchPage.js --coverage --collectCoverageFrom=app/components/search/SearchPage.js

While the test suite appears to run, I get a Failed to collect coverage from...

collectCoverageOnlyFrom works
collectCoverageFrom does not

eg. jest <suite I want to run> --coverage --collectCoverageOnlyFrom path/to/file.jsx

Should this work with TypeScript too? I'm trying the following but it still craps out

yarn jest MyComponent --coverage --collectCoverageOnlyFrom=src/components/MyComponent/MyComponent.tsx

I get Jest encountered an unexpected token even though running yarn coverage works fine.

try this way.
yarn jest src/components/MyComponent/MyComponent.tsx --coverage --collectCoverageOnlyFrom=src/components/MyComponent/MyComponent.tsx

Thanks, but same result

sorry what I meant was yarn jest src/components/MyComponent/test/MyComponent.tsx --coverage --collectCoverageOnlyFrom=src/components/MyComponent/MyComponent.tsx
the first path is for test folder

My setup is a little different so for me, running:

yarn jest src/components/Button/Button.test.tsx --coverage --collectCoverageOnlyFrom=src/components/Button/Button.tsx

Still results in Jest encountered an unexpected token error —  even though running yarn coverage works fine.

Example output:

   8 | `;
   9 |
> 10 | function StyledComponents({ children, ...rest }: ButtonProps): ReactElement {
     |                                                ^
  11 |   return <StyledButton {...rest}>{children}</StyledButton>;
  12 | }
  13 |
STACK: SyntaxError: /Users/skube/Code/my-app/src/components/Button/Button.tsx: Unexpected token, expected "," (10:47)

Maybe you need to see some jest config with typescript:
https://basarat.gitbooks.io/typescript/docs/testing/jest.html

any update on this? is there an option to collect coverage from tested files only?

try this way.
yarn jest src/components/MyComponent/MyComponent.tsx --coverage --collectCoverageOnlyFrom=src/components/MyComponent/MyComponent.tsx

is there any way to get coverage correctly with --coverage --changedSince=master?

try this way.
yarn jest src/components/MyComponent/MyComponent.tsx --coverage --collectCoverageOnlyFrom=src/components/MyComponent/MyComponent.tsx

It works
In most use cases both paths are the same (tested files and files that we want coverage from).
It would be nice if for collectCoverageOnlyFrom without value it would take path of files that are actually tested

I close it since it's solved. Thanks

Can you please open this again ? It is broken on jest 26.6.3
running this display the whole set of test files on the report including the untested files

yarn jest src/components/MyComponent/MyComponent.test.tsx --coverage --collectCoverageOnlyFrom=src/components/MyComponent/MyComponent.tsx

Was this page helpful?
0 / 5 - 0 ratings