memlab-memory-tests
Fungsinya
Skill ini menunjukkan cara menulis tes unit yang memastikan objek yang sudah dilepas benar-benar hilang, dan cara menjalankan skenario memlab dalam pekerjaan tes yang gagal saat kebocoran ditemukan. Skill ini untuk pengembang yang ingin pemeriksaan kebocoran tetap ada di rangkaian tes mereka.
Kapan dipakai
- ✓Saat Anda ingin tes yang gagal jika objek tidak dilepas setelah dispose.
- ✓Saat Anda ingin pemeriksaan kebocoran browser berjalan di integrasi berkelanjutan.
- ✓Saat Anda sudah punya tes Playwright, Cypress, atau Puppeteer dan ingin memeriksa memorinya.
Link skill
https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-memory-tests/SKILL.mdSalin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.
Cara pakai
Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.
Pesan siap tempel
Untuk Claude Code, Codex, atau Cursor: jalankan di folder proyek, lalu skill langsung terpasang.
Unduh SKILL.md lalu taruh di folder skill agen Anda.
Contoh permintaan ke AI
“Tambahkan tes Jest yang gagal jika kelas Widget saya masih ada di memori setelah dispose dipanggil.”
5 skill baru setiap minggu. 1 menit baca.
Cara kerjanya
Agent menandai objek, melepaskannya, mengambil heap snapshot, dan memeriksa bahwa objek yang ditandai sudah hilang. Untuk alur browser, agent menjalankan skenario memlab yang menggagalkan build saat kebocoran ditemukan.
Konten teknis (SKILL.md asli)
Teks asli dari pembuat, dalam bahasa Inggris, langsung dari 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
Pertanyaan umum
Framework tes apa yang didukung?
Contoh Node.js memakai Jest, dan skill menyebut bahwa node:test dan Mocha bekerja dengan cara yang sama.
Paket apa yang diperlukan untuk pemeriksaan Node.js?
Paket @memlab/core, dipasang sebagai dependensi pengembangan.
Apakah tes end-to-end yang sudah ada bisa diperiksa?
Ya. Skill mencakup pengambilan snapshot dari tes Playwright, Cypress, atau Puppeteer yang sudah ada.
Detail
Deskripsi asli dari pembuat
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
Popularitas
- Bintang GitHub
- 5.1k
- Peringkat
- #15058
- Lisensi
- MIT
- Diperbarui
- 31/10/2026
Komentar
- …
Skill lain dari repositori ini
memlab-analyze-heap-snapshot
5.1kMembantu agent Anda menemukan kebocoran memori dalam aplikasi JavaScript dengan menganalisis heap snapshot dan menjelaskan apa yang membuat objek tetap hidup.
memlab-capture-heap-snapshots
5.1kMemandu agent Anda mengambil heap snapshot JavaScript dari browser, Node.js, Electron, atau React Native, sehingga Anda bisa mempelajari penggunaan memori.
memlab-find-web-leaks
5.1kMembantu agent Anda menemukan kebocoran memori di halaman web dengan menjalankan tes terskrip atas satu interaksi dan membaca laporan kebocoran yang dihasilkannya.
Skill serupa
test-driven-development
311kMengajari agent Anda menulis tes yang gagal dulu, melihatnya gagal, lalu menulis kode terkecil yang lulus, sebelum membangun fitur atau perbaikan apa pun.
verification-before-completion
311kMengajari agent Anda menjalankan pemeriksaan nyata dan membaca keluarannya sebelum menyatakan tugas, perbaikan, atau build sudah selesai atau lulus.
migrate-to-shoehorn
310kMengganti type cast berisiko di file tes Anda dengan pustaka shoehorn, sehingga tes bisa memakai data parsial dan tetap lulus pemeriksaan tipe.