Skip to content

Commit

Permalink
fix: make leak-detector more aggressive when running GC (#14526)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Sep 14, 2023
1 parent 8cc8dae commit 485629b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))

### Performance

### Chore & Maintenance
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-leak-detector/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import LeakDetector from '../index';

jest.mock('v8', () => ({
...(jest.requireActual('v8') as Record<string, unknown>),
getHeapSnapshot: jest.fn(),
}));

const gc = globalThis.gc;

// Some tests override the "gc" value. Let's make sure we roll it back to its
Expand Down
13 changes: 12 additions & 1 deletion packages/jest-leak-detector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// <reference lib="es2021.WeakRef" />

import {promisify} from 'util';
import {setFlagsFromString} from 'v8';
import {getHeapSnapshot, setFlagsFromString} from 'v8';
import {runInNewContext} from 'vm';
import {isPrimitive} from 'jest-get-type';
import {format as prettyFormat} from 'pretty-format';
Expand Down Expand Up @@ -48,6 +48,17 @@ export default class LeakDetector {
await tick();
}

if (this._isReferenceBeingHeld) {
// triggering a heap snapshot is more aggressive than just `global.gc()`,
// but it's also quite slow, so only do it if we still think we're leaking.
// https://github.com/nodejs/node/pull/48510#issuecomment-1719289759
getHeapSnapshot();

for (let i = 0; i < 10; i++) {
await tick();
}
}

return this._isReferenceBeingHeld;
}

Expand Down

0 comments on commit 485629b

Please sign in to comment.