Code Coverage with Jest [+ Enzyme & React]

526
526
Jun 26 · 1 min read

You’ve hit the stage where the code base is too complex and now you need to refactor. You want to catch the parts you didn’t test and you want to ensure that everything you move and change has coverage.

Luckily this is really easy with Jest, in our case we’re using Jest in conjunction with React and Enzyme but the setup should be the same for any environment whether you’re doing Angular, Ember, Vue, or even just Node or VanillaJS.

In your package.json file, add this test:coverage command to your scripts:

..."scripts": {
...
"test:coverage": "react-scripts test --coverage --findRelatedTests ./src/**"
...
},
...

You can run it with npm run test:coverage

After that you can open coverage/lcov-report/index.html and it’ll open something like this in your browser:

526

Written by

526

Connecting the world, keeping it safe. Senior Engineer @ newcontext.com, CEO @ awakenedgames.com