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

not able to run the coverage for one file #7427

Closed
nasreddineskandrani opened this issue Nov 29, 2018 · 24 comments
Closed

not able to run the coverage for one file #7427

nasreddineskandrani opened this issue Nov 29, 2018 · 24 comments

Comments

@nasreddineskandrani
Copy link
Contributor

nasreddineskandrani commented Nov 29, 2018

🚀 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.
@rickhanlonii
Copy link
Member

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?

@nasreddineskandrani
Copy link
Contributor Author

nasreddineskandrani commented Nov 29, 2018

done. waiting for feedback :)

@rickhanlonii
Copy link
Member

Wow, awesome change thanks 👍

@sslgeorge
Copy link

sslgeorge commented Mar 11, 2019

Is anybody working on this right now?

@SimenB
Copy link
Member

SimenB commented Mar 18, 2019

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

@mwalkerpear
Copy link

Doesn't this solve the issue?
--collectCoverageOnlyFrom

@joelbarbosa
Copy link

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

@falnos24865
Copy link

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.

@joelbarbosa
Copy link

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

@skube
Copy link

skube commented Sep 6, 2019

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

@ghost
Copy link

ghost commented Oct 18, 2019

collectCoverageOnlyFrom works
collectCoverageFrom does not

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

@skube
Copy link

skube commented Oct 18, 2019

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.

@joelbarbosa
Copy link

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

@skube
Copy link

skube commented Oct 18, 2019

Thanks, but same result

@joelbarbosa
Copy link

joelbarbosa commented Oct 19, 2019

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

@skube
Copy link

skube commented Oct 25, 2019

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)

@joelbarbosa
Copy link

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

@lriostavira
Copy link

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

@ameliexu
Copy link

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?

@ElVisPL
Copy link

ElVisPL commented Oct 20, 2020

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

@nasreddineskandrani
Copy link
Contributor Author

nasreddineskandrani commented Oct 20, 2020

I close it since it's solved. Thanks

@acostaf
Copy link

acostaf commented Nov 13, 2020

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

@davidomarf
Copy link

Hi!

I wanted to do this directly from the Jest CLI options, but since none worked, I did a workaround that may fit your use case.

I only wanted to know what was the coverage of the files I was writing tests for, and piping the output with grep was enough:

yarn jest myPattern | grep myPattern

This will log something like this:

PASS {path of a file that matched your pattern}
PASS {path of a file that matched your pattern}
 {path of one parent directory}                           |      100 |    83.33 |      100 |      100 |                   |
   {path of one of your files}                            |      100 |    83.33 |      100 |      100 |                24 |
   {path of one of your files}                            |      100 |    83.33 |      100 |      100 |               103 |
Jest: "global" coverage threshold for statements (x%) not met: y%
Jest: "global" coverage threshold for lines (x%) not met: y%
Jest: "global" coverage threshold for functions (x%) not met: y%

Test Suites: x passed, x total
Tests:       y passed, y total
Snapshots:   0 total
Time:        Xs
Ran all test suites matching /myPattern/i.

If a test fails it'll still display the expectation errors, only without colors.

@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 May 11, 2021
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