Jest 22 appears to be reporting memory leaks on very minimal test suites that don't have any obvious memory leaks.
To reproduce the memory leak:
- Install dependencies.
- Add identifiable
console.log
statements to lines 54 and 66 ofnode_modules/jest-leak-detector/build/index.js
. This will show the order of execution whenjest
attempts to detect leaks and garbage collect. - Run
yarn test
.
With one additional log statement at line 52 to print the value
that has a weak
callback attached to it, the output of the test is as follows:
➜ jest-22-mem-leak git:(master) ✗ yarn test
yarn test v1.0.2
$ jest --detectLeaks --runInBand
FAIL ./file-1.test.js
● Test suite failed to run
EXPERIMENTAL FEATURE!
Your test suite is leaking memory. Please ensure all references are cleaned.
There is a number of things that can leak memory:
- Async operations that have not finished (e.g. fs.readFile).
- Timers not properly mocked (e.g. setInterval, setTimeout).
- Keeping references to the global scope.
at node_modules/jest-cli/build/test_scheduler.js:115:22
[IN JEST (line 52)] attaching GC listener to JSDOMEnvironment
[IN JEST (line 66)] checking leak
[IN JEST (line 52)] attaching GC listener to JSDOMEnvironment
FAIL ./file-2.test.js
● Test suite failed to run
EXPERIMENTAL FEATURE!
Your test suite is leaking memory. Please ensure all references are cleaned.
There is a number of things that can leak memory:
- Async operations that have not finished (e.g. fs.readFile).
- Timers not properly mocked (e.g. setInterval, setTimeout).
- Keeping references to the global scope.
at node_modules/jest-cli/build/test_scheduler.js:115:22
Test Suites: 2 failed, 2 total
Tests: 0 total
Snapshots: 0 total
Time: 0.734s
Ran all test suites.
[IN JEST (line 54)] garbage collected value
[IN JEST (line 66)] checking leak
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
This shows that the weak
listener is attached, the leak check occurs, then later the value
is garbage collected meaning a JSDOMEnvironment
is still around when leak detection occurs causing jest to report a leak has happened.
Leaks are also reported in with this code when not using --runInBand
but in that case console output is not printed.