memlab-memory-tests
Was er macht
Dieser Skill zeigt, wie man Unit-Tests schreibt, die prüfen, dass ein freigegebenes Objekt verschwunden ist, und wie man memlab-Szenarien in einem Test-Job laufen lässt, der bei Lecks fehlschlägt. Er ist für Entwickler gedacht, die Leck-Prüfungen in ihrer Testsuite behalten wollen.
Wann du ihn nutzt
- ✓Wenn du einen Test willst, der fehlschlägt, wenn ein Objekt nach dispose nicht freigegeben wird.
- ✓Wenn du eine Leck-Prüfung für den Browser in der kontinuierlichen Integration laufen lassen willst.
- ✓Wenn du vorhandene Playwright-, Cypress- oder Puppeteer-Tests hast und deren Speicher prüfen willst.
Link zum Skill
https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-memory-tests/SKILL.mdKopiere ihn und füge ihn in deine KI ein (Claude, ChatGPT oder Cursor) mit der Bitte, den Anweisungen zu folgen.
So nutzt du ihn
Am einfachsten: Kopiere die Nachricht und füge sie in deine KI ein. Sie liest den Skill und folgt den Anweisungen.
Fertige Nachricht zum Einfügen
Für Claude Code, Codex oder Cursor: Führe den Befehl in deinem Projektordner aus, dann wird der Skill installiert.
Lade die SKILL.md herunter und lege sie in den Skills-Ordner deines Agenten.
Beispielanfrage an deine KI
“Füge einen Jest-Test hinzu, der fehlschlägt, wenn meine Widget-Klasse nach dem Aufruf von dispose noch im Speicher ist.”
5 neue Skills pro Woche. 1 Minute Lesezeit.
So funktioniert es
Der Agent markiert das Objekt, gibt es frei, nimmt einen Heap-Snapshot auf und prüft, dass das markierte Objekt verschwunden ist. Für Browser-Abläufe führt er ein memlab-Szenario aus, das den Build fehlschlagen lässt, wenn Lecks gefunden werden.
Technischer Inhalt (originale SKILL.md)
Originaltext des Autors, auf Englisch, direkt von 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
Häufige Fragen
Welche Testwerkzeuge unterstützt er?
Das Node.js-Beispiel nutzt Jest, und der Skill merkt an, dass node:test und Mocha auf die gleiche Weise funktionieren.
Welches Paket braucht man für Node.js-Prüfungen?
Das Paket @memlab/core, installiert als Entwicklungsabhängigkeit.
Können bestehende End-to-End-Tests auch geprüft werden?
Ja. Der Skill deckt das Erstellen von Snapshots aus bestehenden Playwright-, Cypress- oder Puppeteer-Tests ab.
Details
Originalbeschreibung des Autors
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
Beliebtheit
- GitHub-Sterne
- 5.1k
- Platz im Ranking
- #15058
- Lizenz
- MIT
- Aktualisiert
- 31.10.2026
Kommentare
- …
Weitere Skills aus diesem Repository
memlab-analyze-heap-snapshot
5.1kHilft deinem Agenten, Speicherlecks in einer JavaScript-App zu finden, indem er einen Heap-Snapshot analysiert und erklärt, was Objekte am Leben hält.
memlab-capture-heap-snapshots
5.1kFührt deinen Agenten beim Erstellen von JavaScript-Heap-Snapshots aus Browser, Node.js, Electron oder React Native, damit du die Speichernutzung untersuchen kannst.
memlab-find-web-leaks
5.1kHilft deinem Agenten, Speicherlecks auf einer Webseite zu finden, indem er eine skriptgesteuerte Prüfung einer Interaktion ausführt und die erzeugten Leck-Berichte liest.
Ähnliche Skills
test-driven-development
311kBringt deinem Agenten bei, vor jedem Feature oder Fix zuerst einen fehlschlagenden Test zu schreiben, ihn fehlschlagen zu sehen und dann den kleinsten passenden Code zu schreiben.
verification-before-completion
311kBringt deinem Agenten bei, echte Prüfungen auszuführen und ihre Ausgabe zu lesen, bevor er behauptet, eine Aufgabe, ein Fix oder ein Build sei fertig oder bestehe.
migrate-to-shoehorn
310kErsetzt riskante Typumwandlungen in deinen Testdateien durch die Bibliothek shoehorn, damit Tests mit unvollständigen Daten arbeiten und trotzdem die Typprüfung bestehen.