From f07775154bd3d24e5379978d9cfd1f00bfb154ee Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 5 Aug 2020 21:29:37 +0200 Subject: [PATCH] fix(leak-detector): wait more ticks for GC to run (#10366) --- CHANGELOG.md | 1 + packages/jest-leak-detector/src/index.ts | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ba9036ea887..2b5fe3ee8d19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/jest-leak-detector/src/index.ts b/packages/jest-leak-detector/src/index.ts index 2d068275e68a..6cb7dd23923c 100644 --- a/packages/jest-leak-detector/src/index.ts +++ b/packages/jest-leak-detector/src/index.ts @@ -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; @@ -46,12 +49,15 @@ export default class { value = null; } - isLeaking(): Promise { + async isLeaking(): Promise { 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++) { + await tick(); + } + + return this._isReferenceBeingHeld; } private _runGarbageCollector() {