jest: not able to run the coverage for one file

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

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 17
  • Comments: 24 (5 by maintainers)

Most upvoted comments

Doesn’t this solve the issue? --collectCoverageOnlyFrom

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

collectCoverageOnlyFrom works collectCoverageFrom does not

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

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

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

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

Wow, awesome change thanks 👍

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

Thanks, but same result