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

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/meta/memlab/blob/main/plugins/memlab/skills/memlab-memory-tests/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“أضف اختبار Jest يفشل إذا بقي صنف Widget الخاص بي في الذاكرة بعد استدعاء dispose.”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

يضع وكيلك علامة على الكائن ثم يحرره، ويلتقط لقطة كومة، ويتحقق من اختفاء الكائن المعلَّم. وفي تدفقات المتصفح يشغّل سيناريو 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
آخر تحديث
31‏/10‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة