jest: [Bug]: the jest tests performance dropped badly from node 14 to 19

Version

29.4.2

Steps to reproduce

I have a suite of 2158 tests distributed in 396 files - actually, we have more that we decided to skip in the CI because of the actual issue.

Each test requires some imports, and some of them are done globally, via setupFiles and some are imported via the tests.

The total import size per test file is around 20MB.

We were using node 14 in our ecosystem, but because of some other factors, we decided to upgrade to the latest node version (19). Doing so we quickly noticed that our CI time for the jest tests more than doubled.

Those being said we start measuring our suite against multiple node versions. All tests were done multiple times on each node version with --no-cache enabled, in order to get the most accurate readings.

Node 14 - 60s Node 16 - 90s Node 18 - 120s Node 19 - 160s

For exact versions of node here is the detailed list.

 nvm ls
    19.5.0
  * 18.14.0 (Currently using 64-bit executable)
    16.16.0
    14.20.0

The npm command used for running the tests

jest --watchAll=true --config=\"./_tests/jest-tests.config.js\"

jest-test.config.js

const config = {
	verbose: true,
	moduleDirectories: [
		"node_modules",
		"<rootDir>/Dist/js/",
		"<rootDir>/Dist/js/static/"
	],
	testEnvironment: "jsdom",
	testMatch: [
		"**/_Tests/**/*spec.js",
		"**/_Tests/**/*Test.js",
		"**/_Tests/**/*Tests.js"
	],
	transformIgnorePatterns: [
		"/node_modules/",
		"<rootDir>/Dist/js/",
		"<rootDir>/Dist/js/static/"
	],
	setupFiles: [
		"<rootDir>/_Tests/Setup/Common.globals.js", // this is used for main-level mocks, and addressing some global variables
		"<rootDir>/_Tests/Setup/Common.setup.js" // used for importing global stuffs like jquery, fake-indexeddb, some bundled content 
	],
	rootDir: "..",
	roots: [
		"<rootDir>/_Tests",
		"<rootDir>/Scripts"
	],
	testResultsProcessor: "jest-junit",
	watchman: false
};

module.exports = config; 

Another important fact is that the jsdom was locked to 16.4.0 as this affects all the test suites.

Expected behavior

Performance to not decrease like that over upgrading the node version.

Actual behavior

The performance between node 19 and node 14 is almost 3 times worse.

Additional context

As this comes from a real project we can’t offer additional context at the given time.

Environment

System:
    OS: Windows 10 10.0.19044
    CPU: (12) x64 Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
  Binaries:
    Node: 18.14.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.3.1 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: 29.4.2 => 29.4.2

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 18
  • Comments: 26 (4 by maintainers)

Most upvoted comments

So, jest has a huge community, but nobody is interested in performance?!?

Just want to note that the fix from Node 21 made it into Node 20.10.0. My unit test runtime dropped by 50% with this fix.

Same thing. A huge drop of performance. I’ve used Angular 12 with jest 26 and CI and node 16 and tests run 5 minutes. Now I’m using Angular 15 with jest 29 and Node 18 and I’m getting 10-11 minutes with the same pack of tests.

Hi, same here. Any solution?

18.16.1 20.10.0
230s 140s

🎉

I recently got a new dev machine and wanted to re-run jest + webpack on versions 14/16/18 of nodejs.

new machine specs: System: OS: Windows 10 10.0.19044 CPU: (16) x64 11th Gen Intel® Core™ i9-11950H @ 2.60GHz Binaries: Node: 14.18.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.14.15 - C:\Program Files\nodejs\npm.CMD

Below are the updated results:

command mode 14.18.1 16.20.0 18.16.0
unit (jest 29.4.3) cold 72.0s 128.66 103.5s
unit (jest 29.4.3) hot 72.28s 90.71s 91.96s
build (webpack 5.70) cold 64.05s 88.28s 54.66s
build (webpack 5.70) hot 38.4s 46.16s 31.17s

Note: none of the source code or npm packages were changed between runs. On cold runs, I did delete cached webpack artefacts.

From node 14 to 18, our jest tests ran 27% slower while our webpack build was 18% faster. This leads me to believe that the issue is not solely with node itself but some interaction between node + jest.

Let me know if there is any other information that you need.

Do you have a reproduction for this somehow?

And can you try with Node 21, that should have some good improvements (but not related to regression between 18 and 20)

This is a duplicate of #11956 follow that incident for updates - tldr light is at the end of the tunnel - there is a pr that fixed node js

jest 29.5.1, node 16.10 -> 18.16

at github actions runner with maxWorkers=2

16.10 18.16 21min 27min

Ours went from 3min to 9min with 5 shards : /

Node 16 image

Node 20 image

Tested with node 18, same thing. Same workflow on both.

I’m currently in the process of upgrading a project to node 18 and jest 29 and noticed a similar drop in performance on newer node versions.

Our specs: Test Files: 495 Tests: 4135

jest config:

module.exports = {
    testEnvironment: "jsdom",
    testEnvironmentOptions: {
        url: "http://localhost" 
    },
    setupFiles: ["core-js", "<rootDir>/tests/setup"],
    setupFilesAfterEnv: [
        "<rootDir>/tests/matchers.ts",
        "<rootDir>/tests/jsdomPatch.js",
        "<rootDir>/enzyme.config.js"
    ],
    testMatch: ["**/*.spec.(js|ts|jsx|tsx)"],
    moduleFileExtensions: ["js", "json", "ts", "jsx", "tsx"],
    moduleNameMapper: require("./aliases.config").jest,
    coverageDirectory: "<rootDir>/coverage",
    coverageReporters: ["html", "json", "cobertura"],
    reporters: [
        "default"
    ],
    transformIgnorePatterns: [
        "node_modules/(?!(react-grid-layout/css|react-resizable/css|lodash-es|nanoid)/)"
    ],
    testResultsProcessor: "jest-sonar-reporter"
};

Environment System: OS: Windows 10 10.0.19044 CPU: (8) x64 Intel® Core™ i7-7700K CPU @ 4.20GHz Binaries: Node: 14.18.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD npm: 6.14.8 - ~\AppData\Roaming\npm\npm.CMD npmPackages: jest: 29.4.3 => 29.4.3

Test Performance Note: only node version was changed when collecting these run times. Cold runs always started with jest --clearCache running prior to the tests themselves

node 14.18.1 cold run: 239s hot run: 196s

node 18.14.2 cold run: 276s hot run: 240s