Do you want to request a _feature_ or report a _bug_?
_bug_
What is the current behavior?
Jest --detectLeaks is reporting leaks even on very minimal test suites.
I've set up an example repo here: https://github.com/kand/jest-22-mem-leak
There are two test files:
describe('memory leak suite 1', () => {
it('happens', () => {
console.log('suite 1 > test 1 > ran test');
});
});
describe('memory leak suite 2', () => {
it('happens', () => {
console.log('suite 2 > test 1 > ran test');
});
});
As well as some added console.log statements in node_modules/jest-leak-detector/build/index.js:
// line 51
console.log('[IN JEST (line 52)] attaching GC listener to', value.constructor.name);
weak(value, () => {
console.log('[IN JEST (line 54)] garbage collected value');
this._isReferenceBeingHeld = false
});
this._isReferenceBeingHeld = true;
// Ensure value is not leaked by the closure created by the "weak" callback.
value = null;
}
isLeaking() {
this._runGarbageCollector();
console.log('[IN JEST (line 66)] checking leak');
return this._isReferenceBeingHeld;
}
// line 69
Which produces the following output when tests are run:
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.
It doesn't appear that either of these tests should be producing a memory leak. Memory leaks are also reported when --runInBand is not used. It looks like the leak detector is checking for memory leaks before the JSDOMEnvironment object is cleaned up, which causes jest to report a memory leak, though I'm sure this is not the root cause.
I believe these issues are related:
https://github.com/facebook/jest/issues/1893
https://github.com/tmpvar/jsdom/issues/1682
https://github.com/tmpvar/jsdom/issues/1682
But 2 are closed and the open one hasn't had activity in a while.
What is the expected behavior?
No memory leaks are reported.
Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
[email protected]
[email protected]
[email protected]
[email protected]
macOS Sierra (version 10.12.6)
Note that this fails for both jsdom and node environments and for all Jest 22 releases
Fix is in the above PR
This is happening to me in jest v25
Also with me in Jest v25. Mine was even smaller:
describe('MemoryLeakTest', function () {
it('should do nothing', function () {
expect(true).toBeTruthy()
})
})
./node_modules/jest/bin/jest.js --detectLeaks
I submitted a new issue for this problem in v25: #9507
Most helpful comment
This is happening to me in jest v25