jest-leak-detector
Advanced tools
Comparing version 28.1.1 to 28.1.3
@@ -7,5 +7,8 @@ /** | ||
*/ | ||
/// <reference lib="es2021.weakref" /> | ||
/// <reference lib="es2021.weakref" /> | ||
declare class LeakDetector { | ||
private _isReferenceBeingHeld; | ||
private _finalizationRegistry?; | ||
constructor(value: unknown); | ||
@@ -12,0 +15,0 @@ isLeaking(): Promise<boolean>; |
@@ -64,2 +64,3 @@ 'use strict'; | ||
*/ | ||
/// <reference lib="es2021.WeakRef" /> | ||
const tick = (0, _util().promisify)(setImmediate); | ||
@@ -69,2 +70,3 @@ | ||
_isReferenceBeingHeld; | ||
_finalizationRegistry; | ||
@@ -81,21 +83,33 @@ constructor(value) { | ||
); | ||
} | ||
} // TODO: Remove the `if` and `weak-napi` when we drop node 12, as v14 supports FinalizationRegistry | ||
let weak; | ||
if (globalThis.FinalizationRegistry) { | ||
// 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) { | ||
if (!err || err.code !== 'MODULE_NOT_FOUND') { | ||
throw err; | ||
this._finalizationRegistry.register(value, undefined); | ||
} else { | ||
let weak; | ||
try { | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
weak = require('weak-napi'); | ||
} catch (err) { | ||
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, () => (this._isReferenceBeingHeld = false)); | ||
} | ||
weak(value, () => (this._isReferenceBeingHeld = false)); | ||
this._isReferenceBeingHeld = true; // Ensure value is not leaked by the closure created by the "weak" callback. | ||
@@ -102,0 +116,0 @@ |
{ | ||
"name": "jest-leak-detector", | ||
"version": "28.1.1", | ||
"version": "28.1.3", | ||
"repository": { | ||
@@ -21,3 +21,3 @@ "type": "git", | ||
"jest-get-type": "^28.0.2", | ||
"pretty-format": "^28.1.1" | ||
"pretty-format": "^28.1.3" | ||
}, | ||
@@ -34,3 +34,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "eb954f8874960920ac50a8f976bb333fbb06ada9" | ||
"gitHead": "2cce069800dab3fc8ca7c469b32d2e2b2f7e2bb1" | ||
} |
6534
124
Updatedpretty-format@^28.1.3