Starting in v25, the --detectLeaks option will always report a leak when using env=jsdom.
Worked up to version: v24.9.0
Stopped working in version: v25.0.0
https://github.com/dylanwulf/jest-25-detect-leak
npm run test: run test with jsdom environment
npm run test:sixteen: run test with jest-environment-jsdom-sixteen
npm run test:node: run test with node environment
When using jsdom or jest-environment-jsdom-sixteen it reports a memory leak even though the only thing in the test is a console log. When using the node environment, it does not report a leak.
Should not report a memory leak for a simple test that clearly does not leak memory.
https://github.com/dylanwulf/jest-25-detect-leak
npx envinfo --preset jestPaste the results here:
System:
OS: Windows 10 10.0.18363
CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Binaries:
Node: 12.14.1 - C:\Program Files\nodejs\node.EXE
Yarn: 1.21.1 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.13.4 - C:\Program Files\nodejs\npm.CMD
npmPackages:
jest: 25.1.0 => 25.1.0
Thanks! We probably mess with some global or some such we don't cleanup...
This very minimal test point leaks
describe('MemoryLeakTest', function () {
it('should should do nothing', function () {
expect(true).toBeTruthy()
})
})
run with ./node_modules/jest/bin/jest.js --detectLeaks.
I debugged and this issue from jsdom package, I will try to fix it and update in new version in Jest. Ref https://github.com/jsdom/jsdom/issues/2757
hi @SimenB , ref https://github.com/jsdom/jsdom/pull/2825
jsdom maybe not release a bug fix version 15. Will I downgrade the current version of jsdom to use or upgrade to the new version? Upgrading to the new version will only compatible node >= 10, I don't think this is a good way.
You can use https://www.npmjs.com/package/jest-environment-jsdom-sixteen to get v16 in your tests. Jest itself well upgrade the default version in Jest 26
@SimenB in Jest 26, jest-environment-jsdom should be downgrade version of jsdom? can I make a PR to do it?
Jest 26 is months away, and we'll upgrade, not downgrade. So no need for a PR yet 馃檪
If you want the old version of jsdom back, you can install jest-environment-jsdom@24 and use that through config.
I was able to reproduce this using the above code snippet, and have tracked the issue down to commit https://github.com/jsdom/jsdom/commit/75a921eab52c239b2468e08be9f547f46c7f86bd released in jsdom 14.
It is possible to work around the issue by adding the following code snippet to the teardown method of jest-environment-jsdom
async teardown() {
// ...
const OpenElementStack = require('parse5/lib/parser/open-element-stack')
if (OpenElementStack.prototype.pop.toString().includes('OriginalPop')) {
delete OpenElementStack.prototype.pop;
}
if (OpenElementStack.prototype.push.toString().includes('OriginalPush')) {
delete OpenElementStack.prototype.push;
}
}
}
edit: I should've read a little closer, I didn't notice that lamhieu-vk already found the issue and put up a PR. I hope the workaround steps are at least helpful for someone.
@terite
Hey, to be honest I don't know what is teardown in Jest. Could you please write which file and what exactly should be changed to work around this ? Thanks!
This has been fixed upstream in JSDOM now. Unfortunately, since we still support Node 8, we cannot upgrade to a fixed version. @terite do you think it makes sense to add your hack to jest-environment-jsdom? It seems pretty invasive, but we can probably lock down our version of jsdom (it probably won't be updated anyways, but better safe than sorry I think) and it should be relatively safe.
@jeysal @thymikee thoughts on ^?
Alternatively, people can install jest-environment-jsdom-sixteen which will install the fixed version of JSDOM (when the fix is released, which I guess should happen any minute now since the fix just got merged).
Maybe there's a slight chance that jsdom could release a patch to v15?
I asked about release a patch to v15 but it is not supported! https://github.com/jsdom/jsdom/pull/2825#issuecomment-582072648
hmm... if we upgrade jsdom to v16 has some breaking changes, can I create a PR for it (for Jest 26)? because I have a stash in local, I updated while creating https://github.com/jsdom/jsdom/pull/2825
Is there a way to use jest-environment-jsdom-sixteen for all @jest-environment jsdom, but without specifying it in jest.config.js? Cause I have jestEnvironment: node there and don't want jsdom to be default.
@kirillgroshkov sorry, missed your q. It's @jest-environment jsdom-sixteen
Most helpful comment
I debugged and this issue from
jsdompackage, I will try to fix it and update in new version in Jest. Ref https://github.com/jsdom/jsdom/issues/2757