Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(leak-detector): wait more ticks for GC to run #10366

Merged
merged 3 commits into from Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

### Fixes

- `[jest-leak-detector]` Wait properly for GC runs due to changes in Node 14.7 ([#10366](https://github.com/facebook/jest/pull/10366))
- `[jest-worker]` Downgrade minimum node version to 10.13 ([#10352](https://github.com/facebook/jest/pull/10352))

### Chore & Maintenance
Expand Down
14 changes: 10 additions & 4 deletions packages/jest-leak-detector/src/index.ts
Expand Up @@ -7,9 +7,12 @@

import {setFlagsFromString} from 'v8';
import {runInNewContext} from 'vm';
import {promisify} from 'util';
import prettyFormat = require('pretty-format');
import {isPrimitive} from 'jest-get-type';

const tick = promisify(setImmediate);

export default class {
private _isReferenceBeingHeld: boolean;

Expand Down Expand Up @@ -46,12 +49,15 @@ export default class {
value = null;
}

isLeaking(): Promise<boolean> {
async isLeaking(): Promise<boolean> {
this._runGarbageCollector();

return new Promise(resolve =>
setImmediate(() => resolve(this._isReferenceBeingHeld)),
);
// wait some ticks to allow GC to run properly, see https://github.com/nodejs/node/issues/34636#issuecomment-669366235
for (let i = 0; i < 10; i++) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

measured, it takes about 0.3ms on my machine when running this module's unit tests

await tick();
}

return this._isReferenceBeingHeld;
}

private _runGarbageCollector() {
Expand Down