Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily get jest coverage to show only files in a specific folder

For my reactjs app, I'm adding integration tests to a view and want to keep an eye on coverage along the way. I'm currently getting all files on every run.

Question

While adding tests to increase coverage, how can I get jest coverage to show only files in a specific folder?

Examples tried

  • $ yarn test --collectCoverageFrom=src/app/components/Tools Test run, bu no coverage is showing here.

  • $ yarn test Tools --coverage --collectCoverageFrom=src/app/components/Tools I get Ran all test suites matching "Tools".

  • $ yarn test src/app/components/Tools --coverage
    Here I see the coverage percentage is smaller but still lists all files.

  • $ yarn test -o --coverage Again as the previous, I see the coverage percentage is smaller but still lists all files.

like image 789
Austin Thompson III Avatar asked Nov 16 '18 17:11

Austin Thompson III


People also ask

How can I check coverage of a specific file in Jest?

All you have to do is type yarn test [regex] --coverage , and this will run coverage only on the specified file(s).

How does Jest collect coverage?

Jest is collecting coverage only on the function under tests, not from the entire project. This means that despite we are seeing 100% coverage here, potentially we are testing only a fraction of our code. Now Jest is identify correctly what needs to be tested.

What is Jest statement coverage?

if you have a line of code that says var x= 10; console. log(x); that's one line and 2 statements. Statement coverage has each statement in the program been executed. Line coverage has each executable line in the source file been executed.


1 Answers

It would be best to have an argument in the CLI, but you can add a temporary script in your package.json:

"tools-coverage": "react-app-rewired test --env=jsdom src/app/components/Tools --coverage --collectCoverageFrom=src/app/components/Tools/**/*.js"

Running the full command in the terminal just doesn't work.

Now you can instead run $ yarn tools-coverage to just test your Tools folder you targeted in your package.json.

An example shown here in the create-react-app issue: https://github.com/facebook/create-react-app/issues/1455#issuecomment-277010725

like image 154
Barrett Gay Avatar answered Sep 25 '22 01:09

Barrett Gay