Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest - Simple tests are slow

I am using Jest to test an angular app and it is taking a really long time for simple tests to run and I can not seem to figure out why.

My Jest setup in package.json:

"jest": {   "modulePaths": [     "<rootDir>/src",     "<rootDir>/node_modules"   ],   "testPathIgnorePatterns": [     ".git/.*",     "node_modules/.*"   ],   "transformIgnorePatterns": [     "node_modules/.*",     ".*\\.js"   ],   "setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.js",   "preset": "jest-preset-angular",   "testEnvironment": "jsdom",   "testRegex": "src/app/.*\\.spec\\.ts$",   "moduleFileExtensions": [     "ts",     "js",     "json"   ],   "verbose": true,   "cacheDirectory": ".jest-cache",   "coveragePathIgnorePatterns": [     ".*\\.(shim\\.ngstyle|ngfactory)\\.ts"   ],   "globals": {     "ts-jest": {       "tsConfigFile": "./tsconfig.json"     },     "__TRANSFORM_HTML__": true   } } 

My Jest setup file:

'use strict'; require('core-js/es6/reflect'); require('core-js/es7/reflect'); require('zone.js'); require('zone.js/dist/proxy.js'); require('zone.js/dist/sync-test'); require('zone.js/dist/async-test'); require('zone.js/dist/fake-async-test'); require('jest-zone-patch');  const getTestBed = require('@angular/core/testing').getTestBed; const BrowserDynamicTestingModule = require('@angular/platform-browser-dynamic/testing').BrowserDynamicTestingModule; const platformBrowserDynamicTesting = require('@angular/platform-browser-dynamic/testing')  .platformBrowserDynamicTesting;  getTestBed().initTestEnvironment(     BrowserDynamicTestingModule,     platformBrowserDynamicTesting() ); 

Here is my simple test:

fdescribe('RichTextEditorComponent', () => {   it('should be fast', () => {     expect(true).toBeTruthy();   }); }); 

Does anyone have any idea as to why this is taking 9+ seconds? enter image description here

like image 320
Tucker Avatar asked Jul 13 '17 17:07

Tucker


People also ask

Why are my Jest tests slow?

Jest is slow on Windows Unfortunately Jest tests run significantly slower on Windows machines than on Mac OS and Linux due to slower crawling of the Windows file system.

Why does Jest take so long to start?

This is mostly startup time because jest reports the test itself running in ~100ms. We would like to get this time down to allow for a better developer experience. The fact that all tests together take almost ten minutes doesn't bother us that much, but running a single test should ideally finish in less than a second.

What is the fastest Jest?

Wallaby is the fastest available JavaScript test runner. Jest in watch mode (in the best case scenario) re-runs all tests in all test files related to changed files based on hg/git uncommitted files.


2 Answers

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.

like image 83
user7610 Avatar answered Oct 13 '22 18:10

user7610


Read these two links:

https://itnext.io/how-to-make-your-sluggish-jest-v23-tests-go-faster-1d4f3388bcdd https://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 slower https://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 add yarn remove jest yarn 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.

like image 25
Julian Orinyol Avatar answered Oct 13 '22 18:10

Julian Orinyol