Member-only story

Jest: Increase Coverage in Unit Tests

Neelesh Arora
3 min readJul 8, 2023

--

Jest is one of the most popular unit testing tools in JavaScript. In this article, I will share a few tips that might help you to achieve maximum code coverage.

Note: This article is from the back-end perspective and the tips might not work for the front-end technologies like React.

If you wish to know the basics of Jest first, please check out my article here.

In the recent days, I have been continuously working on unit tests. Here are a few things that I have experienced that might help you in increasing overall coverage.

Test the initial coverage with the Parent file/function

Suppose, you are having three files in your project. Create a simple test case (like toBeDefined) for the main function and run the npx jest with — coverage flag. This will return you the current overall coverage and the list of files that are involved in the execution of parent function.

Initial Test file looks like this

And the output of the command is

--

--

Neelesh Arora
Neelesh Arora

Written by Neelesh Arora

Senior Software Engineer | Back-end Developer

No responses yet