Jest - Simple tests are slow Jest - Simple tests are slow typescript typescript

Jest - Simple tests are slow


Another possibility is that ts-jest is slow. There was an issue about that, and it was not completely resolved.

There are various workarounds discussed. They consist of setting isolatedModules=true and also --maxWorkers=1. That is, in jest.config.js

'use strict';module.exports = {    preset: 'ts-jest',    testEnvironment: 'node',    globals: {        'ts-jest': {            isolatedModules: true        }    },}

and run

yarn test --maxWorkers=1

Could be worth trying. Alternatively, it is possible to forgo ts-jest and use babel transpilation.


Read these two links:

https://itnext.io/how-to-make-your-sluggish-jest-v23-tests-go-faster-1d4f3388bcddhttps://github.com/facebook/jest/issues/7963

Here's a list of things to consider. They aren't specific to your case, but since the title of the question is quite general I thought they might help some percentage of visitors. They shouldn't be tried blindly, they are simply a starting point to research.

Things to try to speed up your jest tests:

  1. Run in watch mode with --watch

    jest optimizes when you use --watch.

  2. run on your host computer instead of in docker?-> I was previously using docker exec -it <containername> yarn test and found it faster when I changed to using my host.

  3. upgrade jest version it seems like there were some bugs that made some versions slowerhttps://github.com/facebook/jest/pull/8046

    note: that yarn upgrade obeys the ~ and ^ version signifiers, if you know what you're doing, you might just want to remove and re addyarn remove jestyarn add -D jest that will just get you the latest

  4. change the test environment from jsdom to node

"jest": {  "testEnvironment": "node"}
  1. Run the tests syncronously.. allows jest to optimize?

add --runInBand option

  1. Setting max workers might make it faster?

add --maxWorkers=4 option

In my case I upgraded the jest version, started using --watch and --runInBand and running on my host instead of via docker, and my test time went from 2 mins to 10 seconds. I don't know what the problem was exactly in my case.


I think the answer will ultimately need to come from the Angular team. The documentation for platformBrowserDynamicTesting is sparse (https://angular.io/api/platform-browser-dynamic/testing/platformBrowserDynamicTesting).

Perhaps platformBrowserDynamicTesting emulates a browser and loads the entire DOM for your application into memory. In this case, a nearly 10 second ramp up for an Angular application (without any cached JavaScript) seems reasonable. Maybe I am interpreting this wrong, but per your reports, it looks like the actual test is running in 6 milliseconds which seems like it should be fulfilling your requirement of a "fast test". I would be curious to see how long the tests take if you add another simple "should be fast 2" test. If the total is still under 10 seconds, that suggests your actual tests are taking very little time in comparison to the ramp up of the Angular platformBrowserDynamicTesting utility.