memlab-memory-tests
What it does
It shows how to write unit tests that check a released object is gone, and how to run memlab scenarios in a test job that fails when leaks are found. It is for developers who want leak checks to stay in their test suite.
When to use it
- ✓When you want a test that fails if an object is not released after dispose.
- ✓When you want a browser leak check to run in continuous integration.
- ✓When you have existing Playwright, Cypress, or Puppeteer tests and want to check their memory.
Skill link
https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-memory-tests/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Add a Jest test that fails if my Widget class is still in memory after dispose is called.”
5 new skills a week. 1 minute to read.
How it works
The agent tags the object, releases it, takes a heap snapshot, and checks that the tagged object is gone. For browser flows it runs a memlab scenario that fails the build when leaks are found.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Memory leak tests with memlab
Pick the kind of test from what has to stay leak-free:
| Code under test | Test |
|---|---|
| A Node.js module, class or server code | Heap assertion in a unit test |
| A web UI interaction | memlab scenario run from the test job |
| An existing Playwright, Cypress or Puppeteer test | Snapshots from the existing test |
Node.js heap assertions
takeNodeMinimalHeap() from @memlab/core snapshots the current process and
returns a queryable heap. Assert that the object you released is gone:
// widget.memory.test.js (Jest; node:test and Mocha work the same way)
const {config, takeNodeMinimalHeap, tagObject} = require('@memlab/core');
const {Widget} = require('./widget');
test('Widget is released after dispose()', async () => {
config.muteConsole = true;
let widget = new Widget();
tagObject(widget, 'widget-under-test'); // marks it without changing it
widget.dispose();
widget = null;
const heap = await takeNodeMinimalHeap();
expect(heap.hasObjectWithTag('widget-under-test')).toBe(false);
}, 30000);
npm install --save-dev @memlab/core
tagObject(obj, tag)+heap.hasObjectWithTag(tag)checks one specific instance.heap.hasObjectWithClassName('Widget')checks that no instance of the class is left;heap.getAnyObjectWithClassName('Widget')returns one to inspect.heap.hasObjectWithPropertyName(name)checks for any object with that property.- Drop every reference the test holds (set variables to
null) before taking the heap, or the test itself keeps the object alive. - Taking and parsing a snapshot takes from under a second to many seconds, depending on heap size. Give the test a generous timeout, and keep these tests in their own file so the rest of the suite stays fast.
- For retained sizes, use
takeNodeFullHeap()from@memlab/heap-analysis. The minimal heap skips that computation to parse faster. - When the assertion fails, find out what still holds the object: save a
snapshot with
dumpNodeHeapSnapshot()from@memlab/coreand inspect it (see thememlab-analyze-heap-snapshotskill).
Browser leak tests in CI
Write a scenario for the interaction (see the memlab-find-web-leaks skill),
then run it from the test job with @memlab/api and fail on leaks:
// settings-dialog.memory.test.js
const {run} = require('@memlab/api');
const scenario = require('./scenarios/settings-dialog');
test('settings dialog does not leak', async () => {
const {leaks} = await run({
scenario,
skipWarmup: true, // skip the server warm-up visits
consoleMode: 'CONTINUOUS_TEST', // no progress animation in CI logs
workDir: '/tmp/memlab/settings-dialog', // must exist; keep it as a CI artifact
});
expect(leaks.length).toBe(0);
}, 10 * 60 * 1000);
npm install --save-dev @memlab/api
runreturns{leaks, runResult}.leaksis the list of clustered leak traces; an empty list means no leaks were found. The work directory has the snapshots and browser console output (data/cur/) and the readable traces (data/out/leaks.txt). Upload it from CI so a failure can be investigated.CONTINUOUS_TESTmode skips screenshots and waits an extra 10 seconds after the initial page load. LeaveconsoleModeout when you want screenshots of each step.- Create
workDirbefore callingrun(for example withfs.mkdirSync(dir, {recursive: true})). runlaunches headless Chromium through Puppeteer. On Linux CI images, install Chromium's shared libraries, or setchromiumBinaryto a browser the image already has.- The CLI equivalent is
memlab run --scenario <file> --work-dir <dir> --sc, where--scis the continuous-test console mode. Assert on the result in a script withBrowserInteractionResultReaderandfindLeaksfrom@memlab/api. - Keep one interaction per scenario, so a failure points at one feature.
Snapshots from an existing E2E test
Take three Chromium heap snapshots in the existing test: baseline (before the
interaction), target (after it) and final (after undoing it). The
memlab-capture-heap-snapshots skill has a CDP helper for Puppeteer and
Playwright. Then:
const {findLeaksBySnapshotFilePaths} = require('@memlab/api');
test('the flow under test does not leak', async () => {
// ...drive the flow and write the
FAQ
Which test tools does it support?
The Node.js example uses Jest, and the skill notes that node:test and Mocha work in the same way.
What package is needed for Node.js checks?
The @memlab/core package, installed as a development dependency.
Can existing end-to-end tests be checked too?
Yes. The skill covers taking snapshots from an existing Playwright, Cypress, or Puppeteer test.
Details
Original description by the author
Write automated memory leak tests with memlab. Use it for Node.js unit tests that assert an object was garbage collected (takeNodeMinimalHeap, tagObject, hasObjectWithClassName), for browser leak tests in CI that run a memlab scenario through @memlab/api and fail when leaks are found, and for checking heap snapshots from Playwright, Cypress or Puppeteer tests. Use when the user wants a regression test for a memory leak, asks how to assert that memory is released, or wants memory leak checks in Jest, Mocha, node:test or a CI pipeline.
repo: meta/memlab
Popularity
- GitHub stars
- 5.1k
- Ranking position
- #15058
- License
- MIT
- Updated
- 10/31/2026
Comments
- …
More skills from this repository
memlab-analyze-heap-snapshot
5.1kHelps your agent find memory leaks in a JavaScript app by analyzing a heap snapshot and explaining what keeps objects alive.
memlab-capture-heap-snapshots
5.1kGuides your agent in taking JavaScript heap snapshots from a browser, Node.js, Electron, or React Native, so you can study memory use.
memlab-find-web-leaks
5.1kHelps your agent find memory leaks in a web page by running a scripted test of one interaction and reading the leak reports it produces.
Similar skills
test-driven-development
311kTeaches your agent to write a failing test first, watch it fail, then write the smallest code that passes, before building any feature or fix.
verification-before-completion
311kTeaches your agent to run real checks and read their output before claiming that a task, fix, or build is complete or passing.
migrate-to-shoehorn
310kReplaces risky type casts in your test files with the shoehorn library, so tests can use partial data and still pass type checks.