Skip to content

test: migrate memory tests off withMcpContext - #2729

Merged
OrKoN merged 4 commits into
ChromeDevTools:mainfrom
maheshsingh20:test/memory-unit-tests
Sep 15, 2026
Merged

OrKoN merged 4 commits into
ChromeDevTools:mainfrom
maheshsingh20:test/memory-unit-tests

Conversation

@maheshsingh20

Copy link
Copy Markdown
Contributor

Description

This PR migrates fixture-based heap snapshot tests off withMcpContext to avoid launching and closing headless Chromium unnecessarily:

  • Formatting tests relocated: Moved fixture-based formatting tests from tests/tools/memory.test.ts to tests/formatters/HeapSnapshotFormatter.test.ts using HeapSnapshotManager directly (exercises genuine .heapsnapshot fixtures in Node without browser overhead).
  • Tool handler unit tests: Converted tool handlers in tests/tools/memory.test.ts to mock-based unit tests using createHandlerMocks() to verify parameter validation, default handling, and method delegation to context and response.
  • E2E test retained: Kept take_heapsnapshot as a real-browser e2e test with withMcpContext (genuinely requires live CDP captureHeapSnapshot on a real page).
  • Caching test relocated: Moved comparison cache test to tests/processors/HeapSnapshotManager.test.ts.
  • Mock helpers: Added typed mock factories to tests/mocks.ts.

Test Plan

  • npm run build
  • node scripts/test.js tests/processors/HeapSnapshotManager.test.ts
  • node scripts/test.js tests/formatters/HeapSnapshotFormatter.test.ts
  • node scripts/test.js tests/tools/memory.test.ts
  • npm run check-format
  • npm run docs:generate

Migrate fixture-based heap snapshot tests off withMcpContext to avoid unnecessary browser launches:
- Relocate fixture-based formatting tests to HeapSnapshotFormatter tests using HeapSnapshotManager directly.
- Convert tool handler tests in memory tests to mock unit tests verifying parameter validation and delegation using createHandlerMocks().
- Retain take_heapsnapshot as an e2e test with withMcpContext.
- Relocate snapshot comparison caching test to HeapSnapshotManager tests.
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN,

This PR implements the split we discussed for migrating memory tests off withMcpContext:

  1. Fixture formatting tests relocated: Moved all fixture-based formatting tests to tests/formatters/HeapSnapshotFormatter.test.ts using HeapSnapshotManager directly (exercises genuine .heapsnapshot fixtures in Node with zero browser launches).
  2. E2E test retained: Kept take_heapsnapshot as a real-browser e2e test with withMcpContext (genuinely requires live CDP captureHeapSnapshot).
  3. Tool handler unit tests: Converted the remaining 12 tool handlers in tests/tools/memory.test.ts to fast mock unit tests using createHandlerMocks() to verify parameter parsing, default fallbacks, and delegation.
  4. Caching test relocated: Moved comparison cache test to tests/processors/HeapSnapshotManager.test.ts.

All tests pass cleanly and formatting checks are green. Could you please take a look when you have a moment? Thanks!

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add the following impovements:

  1. Restore dropped error handling & lifecycle tests in HeapSnapshotManager.test.ts:
    • Add tests for getNodesById() throwing on non-existent class ID.
    • Add tests for getDetailedClassDiff() throwing on invalid classIndex.
    • Add tests for disposeSnapshot() and getStats() / getStaticData().
  2. Add unit tests for heap snapshot formatting in tests/McpResponse.test.ts:
    Test response.setHeapSnapshot*() and response.handle() with mock data in tests/McpResponse.test.ts to ensure coverage for headers, stats formatting, pagination summaries, and node sorting.
  3. Complete tool handler test cases in tests/tools/memory.test.ts:
    Add the missing default and paginated parameter test cases for get_heapsnapshot_retainers, get_heapsnapshot_duplicate_strings, get_heapsnapshot_retaining_paths, and unit test take_heapsnapshot.

@maheshsingh20

Copy link
Copy Markdown
Contributor Author

@OrKoN Thanks for the review! I've added all requested improvements in the latest commit:

  1. Restored error handling & lifecycle tests in HeapSnapshotManager.test.ts:

    • Added tests for getNodesById() throwing on non-existent class ID.
    • Added tests for getDetailedClassDiff() throwing on invalid classIndex.
    • Added tests verifying disposeSnapshot() and getStats() / getStaticData().
  2. Heap snapshot formatting unit tests in tests/McpResponse.test.ts:

    • Added unit tests for response.setHeapSnapshot*() and response.handle() with mock data covering headers, stats formatting, pagination summaries, descending node sorting by retainedSize, edge nodes, retaining paths (with truncation notes), dominators, diffs, duplicate strings, and object details.
  3. Completed tool handler test cases in tests/tools/memory.test.ts:

    • Added unit test for take_heapsnapshot.
    • Added default options and paginated parameter test cases for get_heapsnapshot_retainers, get_heapsnapshot_duplicate_strings, and get_heapsnapshot_retaining_paths.

All tests and format checks are passing cleanly.

- Restore dropped error handling and lifecycle tests in HeapSnapshotManager.test.ts

- Add unit tests for heap snapshot formatting in tests/McpResponse.test.ts

- Complete tool handler test cases for get_heapsnapshot_* and take_heapsnapshot in tests/tools/memory.test.ts
@OrKoN
OrKoN self-requested a review September 14, 2026 06:19
@OrKoN
OrKoN enabled auto-merge September 14, 2026 10:47
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Thanks for the approval, @OrKoN!

It looks like the CI run failed only on windows-latest with node 24 due to a flaky test in script.test.js (Error: Requesting main frame too early!), while all memory tests passed.

Could you please re-run the failed check when you have a moment? Thanks!

@OrKoN
OrKoN added this pull request to the merge queue Sep 15, 2026
Merged via the queue into ChromeDevTools:main with commit 350404e Sep 15, 2026
20 checks passed
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN,
As this PR is merged, I want to take next task in line.

@maheshsingh20
maheshsingh20 deleted the test/memory-unit-tests branch September 15, 2026 12:40
@OrKoN

OrKoN commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

Let's migrate https://github.com/ChromeDevTools/chrome-devtools-mcp/blob/main/tests/tools/network.test.ts next while ensuring that we maintain/move the e2e coverage to the network collector tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants