跳到正文
Skillpedia
测试5.1k · #15058

memlab-memory-tests

它能做什么

它展示如何编写单元测试,检查已释放的对象是否真的被回收,以及如何在测试任务中运行 memlab 场景,发现泄漏时让任务失败。适合希望把泄漏检查保留在测试套件中的开发者。

适用场景

  • ✓当你希望对象在 dispose 之后未被释放时测试失败时。
  • ✓当你希望在持续集成中运行浏览器泄漏检查时。
  • ✓当你已有 Playwright、Cypress 或 Puppeteer 测试,并想检查它们的内存时。

技能链接

https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-memory-tests/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-memory-tests/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请添加一个 Jest 测试,如果调用 dispose 之后我的 Widget 类仍在内存中,测试就失败。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体为对象做标记,释放它,拍摄堆快照,然后检查被标记的对象是否已消失。对于浏览器流程,它会运行一个 memlab 场景,发现泄漏时让构建失败。

技术内容(原始 SKILL.md)

作者在 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/core and inspect it (see the memlab-analyze-heap-snapshot skill).

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
  • run returns {leaks, runResult}. leaks is 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_TEST mode skips screenshots and waits an extra 10 seconds after the initial page load. Leave consoleMode out when you want screenshots of each step.
  • Create workDir before calling run (for example with fs.mkdirSync(dir, {recursive: true})).
  • run launches headless Chromium through Puppeteer. On Linux CI images, install Chromium's shared libraries, or set chromiumBinary to a browser the image already has.
  • The CLI equivalent is memlab run --scenario <file> --work-dir <dir> --sc, where --sc is the continuous-test console mode. Assert on the result in a script with BrowserInteractionResultReader and findLeaks from @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 
常见问题

它支持哪些测试工具?

Node.js 示例使用 Jest,该技能说明 node:test 和 Mocha 的用法相同。

Node.js 检查需要什么包?

需要 @memlab/core 包,并作为开发依赖安装。

现有的端到端测试也能检查吗?

可以。该技能涵盖从现有的 Playwright、Cypress 或 Puppeteer 测试中拍摄快照。

详情

作者的原始描述

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

热度

GitHub 星标
5.1k
排名
#15058
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能