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

Viewed 13645

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.

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

Related