Before we move on to actually writing our tests, we should cover a few best practices.

By default, each test produces the terminal output that we saw in the previous exercise. Jest allows us to customize this output by using command line flags. Though there are many command-line flags, one of the most commonly used is the --coverage flag:

jest __tests__/ --coverage

This --coverage flag allows us to get a report of which lines of our code were actually tested. In addition to being outputted in the terminal, this report becomes available in a directory named coverage/ that is created at runtime.

jest coverage report

This report can help us make sure that our code has been thoroughly tested. From the report, we can see that there are four categories of our code that are being analyzed:

  • Statement coverage analyzes the percentage of the program’s statements that have been executed.
  • Branch coverage analyzes the percentage of the program’s edge cases that have executed.
  • Function coverage analyzes the percent of the program’s functions that have been called during testing.
  • Line coverage analyzes the percentage of the program’s executable lines in the source file that have been executed.

Currently, each of those sections will show up at 0% coverage which makes sense – we haven’t written any tests yet! Strategies for interpreting and analyzing this coverage report are outside of the scope of this lesson, however, you should keep an eye on how this report changes as we continue to write our tests.

Though the coverage report is incredibly useful, it can be annoying to type out the full command every time we want to run our tests! Therefore, it is a good practice to preconfigure our test commands in our package.json file to allow us to run tests through npm with a simple terminal command.

To do this, we set up the "test" script in our package.json file’s "scripts" property:

"scripts": { // other scripts... "test": "jest __tests__/ --coverage" }

We can now run tests on all of our Jest test files while also creating a coverage report by running this terminal command:

npm test

In the future, if we wanted to change how we run our tests, we could adjust the package.json file and then continue using the npm test command. At any point, we are still able to use jest commands directly in the terminal.



We’d like to be able to execute our test with the npm test command.

Enable this by editing the "test" script in the package.json file. It should run jest tests on the files found in the __tests__/ directory while also producing a coverage report.

Make sure to hit “Run” to save your package.json file.


Run the test command in the terminal to verify that everything was configured properly! You should see that a new coverage directory was created with the report and its files.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?