Why does `npm test` take so long on only one test?

Viewed 5129

After npx create-react-app my-app I run npm test and I get the following:

 PASS  src/App.test.js
  ✓ renders learn react link (48ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.555s
Ran all test suites.

Watch Usage: Press w to show more.

Why does it take 2.5s to run the entire suite, but only 48ms to run the test? How can I speed up this process? Thanks!

UPDATE: I'm running this computer:

enter image description here

1 Answers

The answer is pretty simple: if your test takes 48ms and the entire duration of the suite takes 2.555 seconds, then there is other code that being run that eats up the other 2.507 seconds.

In other words, the code that has to run before and after this one test is significantly more than what you see without looking at the jest library's framework code.

That's why the runtime of the suite is longer (2.5s) than the sum of the tests (48ms).


To make the testing simpler (as well as easier) and more automatic there is a lot of "behind the scenes" code that is allowing you to:

  • write your tests the way you are, i.e. in a way that is easy to understand/write, but not necessarily how the compiler will actually see it; it will be compiled to something else
  • find your files that are needed for each test; it takes some time to get all the specified files together
  • compile the specified files (e.g. using webpack) to what you've specified in your build,
  • build/compile a testing environment to run your code in, as jest emulates a browser to keep your code similar to what it would run on in production
  • run a test
  • then output the results

An analogy to compare it to would be renting a ride-share; you have a minimum cost to just get in the car, but once you are there it is that plus a per-minute or per-mile cost. So, it costs you $2.507 to just rent a bike plus 4.8 cents a mile.


Once your testing environment is made at the beginning, your subsequent tests will not each require 2.5 seconds to run.

Useful links for jest related to time:


Ways that you can speed up your tests is to:

  • make sure your webpack/babel is configured for jest properly,
  • you use setup-teardown in an intelligent manner,
  • (optional/selective) you could run your tests in node rather than jest's default jsdom environment. Although the downside is your React tests will fail, so this only works with Node-only tests (server side).
  • Lookup how to write efficient tests, as optimized test code make a difference, here is one such article that provides some insight.

Another non-code option is to use a better/faster CPU or use a more efficient operating system.

Your 2.555s with the default create-react-app is multitudes faster compared to my 6.17s on a Windows 10, Asus GL502VMK.

Running it on a Ubuntu 20 Desktop VM (Virtualbox on the same Windows 10 machine mentioned above, 8 out of the 12GB Host's RAM, 4 out of the 8 Host's CPUs), it takes 2.105s.


You can find a lot of articles complaining about Jest and the execution time, so it doesn't seem that this is necessarily just you. There are parts of Jest that are just not fast.

Related