Well, create-react-app is giving you a lot out of the box but it is also imposing some constraints...
You can't just add the flag --coverage
to npm run test
because that script is already running your tests in watch mode (and things like running only failed tests and other things that you can do in the interactive mode would affect your coverage report anyway).
So first thing first, run your test in single run mode.
CI=true react-scripts test --env=jsdom
And because you already have a test task in your package.json you can simplify your task as so:
CI=true npm test -- --env=jsdom
Alright, we are getting closer...
So now, on top of that add the --coverage
flag and you are all set:
CI=true npm test -- --env=jsdom --coverage
To summarize your npm task could be like:
"test:coverage": "CI=true npm test -- --env=jsdom --coverage"
And you will see your report in the terminal and the coverage
folder will be generated, where you can see a lot of useful info, by the way!
jest
has inbuilt coverage toolIstanbul
built into it. You just need to usenpm run test --coverage
npm run coverage