From a12280c8a8de5d8e630d502b5c11719f9510c0ca Mon Sep 17 00:00:00 2001 From: enrilchow <1350696073@qq.com> Date: Wed, 29 Jun 2022 01:39:13 +0800 Subject: [PATCH] feat(jest-leak-detector): use FinalizationRegistry when it exists to get rid of external dependency --- CHANGELOG.md | 2 ++ packages/jest-leak-detector/src/index.ts | 37 ++++++++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edd253da566b..a40667e699ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-leak-detector]` Use native `FinalizationRegistry` when it exists to get rid of external C dependency ([#12973](https://github.com/facebook/jest/pull/12973)) + ### Fixes -`[jest-runtime]` Avoid star type import from `@jest/globals` ([#12949](https://github.com/facebook/jest/pull/12949)) diff --git a/packages/jest-leak-detector/src/index.ts b/packages/jest-leak-detector/src/index.ts index 33a5e8cefe97..715c72ac6661 100644 --- a/packages/jest-leak-detector/src/index.ts +++ b/packages/jest-leak-detector/src/index.ts @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ +/// import {promisify} from 'util'; import {setFlagsFromString} from 'v8'; @@ -15,6 +16,7 @@ const tick = promisify(setImmediate); export default class LeakDetector { private _isReferenceBeingHeld: boolean; + private _finalizationRegistry?: FinalizationRegistry; constructor(value: unknown) { if (isPrimitive(value)) { @@ -26,23 +28,34 @@ export default class LeakDetector { ); } - let weak: typeof import('weak-napi'); + // TODO: Remove the `if` and `weak-napi` when we drop node 12, as v14 supports FinalizationRegistry + if (globalThis.FinalizationRegistry) { + this._finalizationRegistry = new FinalizationRegistry(() => { + this._isReferenceBeingHeld = false; + }); - try { - // eslint-disable-next-line import/no-extraneous-dependencies - weak = require('weak-napi'); - } catch (err: any) { - if (!err || err.code !== 'MODULE_NOT_FOUND') { - throw err; + this._finalizationRegistry.register(value as object, undefined); + } else { + let weak: typeof import('weak-napi'); + + try { + // eslint-disable-next-line import/no-extraneous-dependencies + weak = require('weak-napi'); + } catch (err: any) { + if (!err || err.code !== 'MODULE_NOT_FOUND') { + throw err; + } + + throw new Error( + 'The leaking detection mechanism requires newer version of node that supports ' + + 'FinalizationRegistry, update your node or install the "weak-napi" package' + + 'which support current node version as a dependency on your main project.', + ); } - throw new Error( - 'The leaking detection mechanism requires the "weak-napi" package to be installed and work. ' + - 'Please install it as a dependency on your main project', - ); + weak(value as object, () => (this._isReferenceBeingHeld = false)); } - weak(value as object, () => (this._isReferenceBeingHeld = false)); this._isReferenceBeingHeld = true; // Ensure value is not leaked by the closure created by the "weak" callback.