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

feat(jest-leak-detector): use FinalizationRegistry when it exists to get rid of external dependency #12973

Merged
merged 1 commit into from
Jun 30, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
39 changes: 27 additions & 12 deletions packages/jest-leak-detector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
/// <reference lib="es2021.WeakRef" />

import {promisify} from 'util';
import {setFlagsFromString} from 'v8';
Expand All @@ -15,6 +16,7 @@ const tick = promisify(setImmediate);

export default class LeakDetector {
private _isReferenceBeingHeld: boolean;
private _finalizationRegistry?: FinalizationRegistry<undefined>;

constructor(value: unknown) {
if (isPrimitive(value)) {
Expand All @@ -26,23 +28,36 @@ 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) {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
// When `_finalizationRegistry` is GCed the callback we set will no longer be called,
// so we need to assign it to `this` to keep it referenced
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);
enrilzhou marked this conversation as resolved.
Show resolved Hide resolved
} 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.
Expand Down