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.
While adding tests to increase coverage, how can I get jest coverage to show only files in a specific folder?
$ 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.
All you have to do is type yarn test [regex] --coverage , and this will run coverage only on the specified file(s).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With