memlab-memory-tests
यह क्या करती है
यह दिखाती है कि यूनिट टेस्ट कैसे लिखें जो जाँचें कि dispose के बाद ऑब्जेक्ट सचमुच हट गया है, और memlab परिदृश्यों को ऐसे टेस्ट जॉब में कैसे चलाएँ जो लीक मिलने पर विफल हो। यह उन डेवलपर्स के लिए है जो लीक जाँच को अपने टेस्ट सूट में रखना चाहते हैं।
कब इस्तेमाल करें
- ✓जब आप ऐसा टेस्ट चाहते हैं जो dispose के बाद ऑब्जेक्ट रिलीज़ न होने पर विफल हो।
- ✓जब आप चाहते हैं कि ब्राउज़र की लीक जाँच continuous integration में चले।
- ✓जब आपके पास Playwright, Cypress या Puppeteer टेस्ट हैं और आप उनकी मेमोरी जाँचना चाहते हैं।
Skill का लिंक
https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-memory-tests/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“Jest टेस्ट जोड़िए जो विफल हो जाए, अगर dispose कॉल होने के बाद भी मेरी Widget क्लास मेमोरी में बची रहे।”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
आपका एजेंट ऑब्जेक्ट को टैग करता है, उसे रिलीज़ करता है, heap snapshot लेता है, और जाँचता है कि टैग किया गया ऑब्जेक्ट हट गया है। ब्राउज़र फ़्लो के लिए वह 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/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
अक्सर पूछे जाने वाले सवाल
कौन-से टेस्ट टूल समर्थित हैं?
Node.js का उदाहरण Jest इस्तेमाल करता है, और स्किल बताती है कि node:test और Mocha भी उसी तरह काम करते हैं।
Node.js जाँच के लिए कौन-सा पैकेज चाहिए?
@memlab/core पैकेज, जो development dependency के रूप में इंस्टॉल होता है।
क्या मौजूदा end-to-end टेस्ट भी जाँचे जा सकते हैं?
हाँ। स्किल बताती है कि मौजूदा Playwright, Cypress या Puppeteer टेस्ट से snapshots कैसे लिए जाएँ।
जानकारी
लेखक का मूल विवरण
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
- अपडेट की गई
- 31/10/2026
कमेंट
- …
इस repository की और skills
memlab-analyze-heap-snapshot
5.1kयह आपके एजेंट को heap snapshot पढ़कर JavaScript ऐप में मेमोरी लीक ढूँढने और समझाने में मदद करती है कि कौन-सी चीज़ें ऑब्जेक्ट को ज़िंदा रखती हैं।
memlab-capture-heap-snapshots
5.1kयह आपके एजेंट को ब्राउज़र, Node.js, Electron या React Native से JavaScript heap snapshot लेने में मार्गदर्शन देती है, ताकि आप मेमोरी उपयोग का अध्ययन कर सकें।
memlab-find-web-leaks
5.1kयह आपके एजेंट को एक इंटरैक्शन का स्क्रिप्ट वाला टेस्ट चलाकर और उससे बनी लीक रिपोर्ट पढ़कर वेब पेज में मेमोरी लीक ढूँढने में मदद करती है।
मिलती-जुलती skills
test-driven-development
311kयह आपके एजेंट को किसी भी फ़ीचर या सुधार से पहले पहले असफल होने वाला टेस्ट लिखना, उसे असफल होते देखना, फिर पास करने वाला सबसे छोटा कोड लिखना सिखाती है।
verification-before-completion
311kयह आपके एजेंट को किसी काम, सुधार या build के पूरा या पास होने का दावा करने से पहले असली जांच चलाकर उसका आउटपुट पढ़ना सिखाती है।
migrate-to-shoehorn
310kआपकी टेस्ट फ़ाइलों में जोखिम भरे type casts की जगह shoehorn लाइब्रेरी लगाती है, ताकि टेस्ट अधूरे डेटा से भी type checks पास कर सकें।