memlab-memory-tests
Skill này làm gì
Skill này chỉ cách viết kiểm thử đơn vị để kiểm tra một đối tượng đã được giải phóng thì không còn tồn tại, và cách chạy các kịch bản memlab trong một tác vụ kiểm thử sẽ thất bại khi phát hiện rò rỉ. Skill dành cho lập trình viên muốn các kiểm tra rò rỉ nằm trong bộ kiểm thử của họ.
Khi nào nên dùng
- ✓Khi bạn muốn một bài kiểm thử thất bại nếu một đối tượng không được giải phóng sau dispose.
- ✓Khi bạn muốn một kiểm tra rò rỉ trình duyệt chạy trong tích hợp liên tục.
- ✓Khi bạn đã có kiểm thử Playwright, Cypress hoặc Puppeteer và muốn kiểm tra bộ nhớ của chúng.
Liên kết skill
https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-memory-tests/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Thêm một bài kiểm thử Jest thất bại nếu lớp Widget của tôi vẫn còn trong bộ nhớ sau khi gọi dispose.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Agent gắn thẻ cho đối tượng, giải phóng nó, chụp heap snapshot và kiểm tra đối tượng được gắn thẻ đã biến mất. Với luồng trình duyệt, agent chạy một kịch bản memlab làm build thất bại khi phát hiện rò rỉ.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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
Câu hỏi thường gặp
Nó hỗ trợ những công cụ kiểm thử nào?
Ví dụ Node.js dùng Jest, và skill ghi chú rằng node:test và Mocha hoạt động theo cách tương tự.
Cần gói nào cho kiểm tra Node.js?
Gói @memlab/core, được cài làm phụ thuộc phát triển.
Có thể kiểm tra các kiểm thử đầu cuối hiện có không?
Có. Skill bao gồm việc chụp snapshot từ một kiểm thử Playwright, Cypress hoặc Puppeteer hiện có.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 5.1k
- Vị trí xếp hạng
- #15058
- Giấy phép
- MIT
- Cập nhật
- 31/10/2026
Bình luận
- …
Thêm skill từ kho mã này
memlab-analyze-heap-snapshot
5.1kGiúp agent của bạn tìm rò rỉ bộ nhớ trong ứng dụng JavaScript bằng cách phân tích heap snapshot và giải thích điều gì giữ các đối tượng tồn tại.
memlab-capture-heap-snapshots
5.1kHướng dẫn agent của bạn chụp heap snapshot JavaScript từ trình duyệt, Node.js, Electron hoặc React Native, để bạn có thể nghiên cứu mức dùng bộ nhớ.
memlab-find-web-leaks
5.1kGiúp agent của bạn tìm rò rỉ bộ nhớ trong trang web bằng cách chạy một bài kiểm thử tự động một tương tác và đọc các báo cáo rò rỉ được tạo ra.
Skill tương tự
test-driven-development
311kDạy agent của bạn viết kiểm thử thất bại trước, xem nó thất bại, rồi viết đoạn mã ngắn nhất để vượt qua trước khi làm bất kỳ tính năng hay bản sửa nào.
verification-before-completion
311kDạy agent của bạn chạy kiểm tra thực tế và đọc kết quả đầu ra trước khi khẳng định một việc, bản sửa hoặc bản build đã hoàn thành hay vượt qua.
migrate-to-shoehorn
310kThay các ép kiểu rủi ro trong tệp kiểm thử bằng thư viện shoehorn, để các bài kiểm thử dùng dữ liệu một phần vẫn qua được kiểm tra kiểu.