File tree Expand file tree Collapse file tree 3 files changed +19
-1
lines changed
packages/jest-leak-detector/src Expand file tree Collapse file tree 3 files changed +19
-1
lines changed Original file line number Diff line number Diff line change 4
4
5
5
### Fixes
6
6
7
+ - `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))
8
+
7
9
### Performance
8
10
9
11
### Chore & Maintenance
Original file line number Diff line number Diff line change 7
7
8
8
import LeakDetector from '../index';
9
9
10
+ jest.mock('v8', () => ({
11
+ ...(jest.requireActual('v8') as Record<string, unknown>),
12
+ getHeapSnapshot: jest.fn(),
13
+ }));
14
+
10
15
const gc = globalThis.gc;
11
16
12
17
// Some tests override the "gc" value. Let's make sure we roll it back to its
Original file line number Diff line number Diff line change 7
7
/// <reference lib="es2021.WeakRef" />
8
8
9
9
import {promisify} from 'util';
10
- import {setFlagsFromString} from 'v8';
10
+ import {getHeapSnapshot, setFlagsFromString} from 'v8';
11
11
import {runInNewContext} from 'vm';
12
12
import {isPrimitive} from 'jest-get-type';
13
13
import {format as prettyFormat} from 'pretty-format';
@@ -48,6 +48,17 @@ export default class LeakDetector {
48
48
await tick();
49
49
}
50
50
51
+ if (this._isReferenceBeingHeld) {
52
+ // triggering a heap snapshot is more aggressive than just `global.gc()`,
53
+ // but it's also quite slow, so only do it if we still think we're leaking.
54
+ // https://github.com/nodejs/node/pull/48510#issuecomment-1719289759
55
+ getHeapSnapshot();
56
+
57
+ for (let i = 0; i < 10; i++) {
58
+ await tick();
59
+ }
60
+ }
61
+
51
62
return this._isReferenceBeingHeld;
52
63
}
53
64
You can’t perform that action at this time.
0 commit comments