Module for verifying whether an object has been garbage collected or not
npx @tessl/cli install tessl/npm-jest-leak-detector@30.1.0Jest Leak Detector is a TypeScript utility for verifying whether an object has been garbage collected or not. It creates weak references to objects and forces garbage collection to determine if objects are properly released from memory. This library is primarily used within the Jest testing framework ecosystem for memory leak detection during test execution.
npm install jest-leak-detectorimport LeakDetector from "jest-leak-detector";For CommonJS:
const LeakDetector = require("jest-leak-detector");import LeakDetector from "jest-leak-detector";
async function detectLeak() {
let reference = { data: "some object" };
// Create detector to monitor the object
const detector = new LeakDetector(reference);
// Check if object is still in memory (should be true)
let isLeaking = await detector.isLeaking();
console.log(isLeaking); // true
// Remove reference
reference = null;
// Check again after nullifying reference (should be false)
isLeaking = await detector.isLeaking();
console.log(isLeaking); // false
}Creates a leak detector that monitors whether a given object has been garbage collected.
class LeakDetector {
/**
* Creates a leak detector for monitoring object garbage collection
* @param value - The object to monitor (cannot be a primitive value)
* @param opt - Optional configuration for detection behavior
* @throws TypeError if value is a primitive (undefined, null, boolean, number, string, symbol)
*/
constructor(value: unknown, opt?: LeakDetectorOptions);
/**
* Checks if the monitored object is still in memory
* Forces garbage collection and checks if the object is still referenced
* @returns Promise resolving to true if object is still in memory, false if garbage collected
*/
isLeaking(): Promise<boolean>;
}Usage Examples:
// Monitor simple objects
const detector = new LeakDetector({ foo: "bar" });
const isLeaking = await detector.isLeaking();
// Monitor functions
const fn = () => console.log("test");
const fnDetector = new LeakDetector(fn);
// Monitor arrays
const arr = [1, 2, 3];
const arrDetector = new LeakDetector(arr);
// With configuration options
const detector = new LeakDetector(someObject, {
shouldGenerateV8HeapSnapshot: false
});Options for customizing leak detection behavior.
interface LeakDetectorOptions {
/**
* Whether to generate V8 heap snapshots for more aggressive garbage collection
* Heap snapshots provide more thorough GC but are slower to execute
* @default true
*/
shouldGenerateV8HeapSnapshot: boolean;
}The LeakDetector throws errors in the following cases:
"Primitives cannot leak memory. You passed a {type}: <{formatted_value}>"// These will throw TypeError
new LeakDetector(null); // TypeError: Primitives cannot leak memory. You passed a object: <null>
new LeakDetector(42); // TypeError: Primitives cannot leak memory. You passed a number: <42>
new LeakDetector("string"); // TypeError: Primitives cannot leak memory. You passed a string: <"string">
new LeakDetector(true); // TypeError: Primitives cannot leak memory. You passed a boolean: <true>
new LeakDetector(undefined); // TypeError: Primitives cannot leak memory. You passed a undefined: <undefined>
new LeakDetector(Symbol()); // TypeError: Primitives cannot leak memory. You passed a symbol: <Symbol()>The leak detector uses several advanced JavaScript and Node.js features:
v8.getHeapSnapshot()--expose-gc and --no-expose-gc flags