Skip to content

test: migrate emulation tests off withMcpContext to unit tests - #2665

Merged
OrKoN merged 3 commits into
ChromeDevTools:mainfrom
maheshsingh20:test/finish-emulation-unit-tests
Sep 7, 2026
Merged

OrKoN merged 3 commits into
ChromeDevTools:mainfrom
maheshsingh20:test/finish-emulation-unit-tests

Conversation

@maheshsingh20

@maheshsingh20 maheshsingh20 commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Relates to #2639.

Follow-up to #2641 and feedback in #2660 (AGENTS.md).

Summary of Changes

  1. tests/tools/emulation.test.ts:

    • Completely migrated off withMcpContext, removing all real-browser launches, serverHooks, and HTTP fixtures.
    • All tool handler test suites (geolocation, viewport, userAgent, extraHttpHeaders, colorScheme, network, cpu) now use createHandlerMocks() from tests/mocks.ts and verify page.emulate() is invoked with exact expected arguments.
  2. tests/McpPage.test.ts:

    • Added unit test coverage for McpPage.emulate() using createMockPuppeteerPage():
      • Offline network throttling settings and network blocking guard.
      • Secondary DevTools session CPU throttling delegation.
      • Geolocation set/clear behavior.
      • Viewport set/clear with default parameter merging.
      • Extra HTTP headers set/clear behavior.

Verification

  • npm run check-format: Passed (clean ESLint + Prettier)
  • npm run build: Compiled with exit code 0
  • node scripts/test.js tests/tools/emulation.test.ts: 50/50 tests passed
  • node scripts/test.js tests/McpPage.test.ts: 32/32 tests passed

@maheshsingh20

maheshsingh20 commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN, opened this follow-up PR to finish migrating tests/tools/emulation.test.ts off withMcpContext per our discussion and the new AGENTS.md guidelines:

tests/tools/emulation.test.ts: All remaining real-browser tests (geolocation, viewport, userAgent, extraHttpHeaders, colorScheme, etc.) have been converted to mock-based unit tests using createHandlerMocks(), asserting page.emulate() arguments.
tests/McpPage.test.ts: Added unit test coverage for McpPage.emulate() with createMockPuppeteerPage() (covering geolocation, viewport defaults, extra headers, offline conditions, network blocking guard, and secondary session CPU throttling delegation).
Local lint, format checks, and test runner (50/50 emulation tests, 32/32 McpPage tests) are passing cleanly. Ready for your review...

Comment thread tests/McpPage.test.ts Resolved
Comment thread tests/McpPage.test.ts Resolved
Comment thread tests/tools/emulation.test.ts Resolved
Comment thread tests/tools/emulation.test.ts Resolved
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN
Addressed all four comments:

Moved the devtoolsUniverse session mock into createMcpPage() — removed duplication across tests.
Fixed the viewport test to pass only partial dimensions ({width: 400, height: 400}), asserting the defaults (deviceScaleFactor: 1, isMobile: false, hasTouch: false, isLandscape: false) are correctly merged in.
Renamed to 'emulates extraHttpHeaders'.
Added sinon.assert.calledTwice(...) alongside every .secondCall assertion — verified 9/9 across both files.

All 82 tests passing (32 McpPage + 50 emulation), build clean, check-format clean.

@OrKoN

OrKoN commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

@maheshsingh20 could you please mark individual comments as resolved as unresolved comments will prevent this from landing?

@maheshsingh20

Copy link
Copy Markdown
Contributor Author

@maheshsingh20 could you please mark individual comments as resolved as unresolved comments will prevent this from landing?

Yes marked!!

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

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN,
With the emulation tests fully migrated and the mock infrastructure in place, let me know which handler you'd like tackled next for item (1) — happy to keep working through this.

@maheshsingh20
maheshsingh20 deleted the test/finish-emulation-unit-tests branch September 7, 2026 12:39
@OrKoN

OrKoN commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Sounds good - I'll convert screencast.test.ts to use createHandlerMocks(), replacing the per-test sinon.stub(selectedPage, 'screencast') calls with direct stubs on the already-mocked page, and wiring context.getScreenRecorder()/setScreenRecorder() to track state via callsFake so tests can verify recorder lifecycle without a real browser.

pull Bot pushed a commit to Mu-L/chrome-devtools-mcp that referenced this pull request Sep 8, 2026
…meDevTools#2683)

This PR migrates `tests/tools/screencast.test.ts` from real-browser
tests (`withMcpContext`) to mock-based unit tests using the established
Sinon mock infrastructure.

### Summary
- References ChromeDevTools#2639 (item 1)
- Follow-up building on ChromeDevTools#2641 and ChromeDevTools#2665
- Rewrites `tests/tools/screencast.test.ts` to use
`createHandlerMocks()` instead of `withMcpContext()`, eliminating real
browser launches.
- Adds `createMockScreenRecorder()` helper and wires
`getScreenRecorder`/`setScreenRecorder` and `ensureExtension` in
`tests/mocks.ts`.
- Re-exports `ScreenRecorder` from `src/third_party/index.ts` to comply
with internal import boundaries.
pull Bot pushed a commit to oogalieboogalie/chrome-devtools-mcp that referenced this pull request Sep 9, 2026
… tests to McpPage (ChromeDevTools#2698)

This PR migrates tests in `tests/tools/snapshot.test.ts` to mock-based
unit tests using the established Sinon mock infrastructure and relocates
the real-browser integration tests to `tests/McpPage.test.ts`.

### Summary
- References ChromeDevTools#2639 (item 1).
- Follow-up building on ChromeDevTools#2641, ChromeDevTools#2665, and ChromeDevTools#2683.
- Relocated the 6 real-browser `waitFor` integration tests to a
dedicated `waitForTextOnPage()` suite in `tests/McpPage.test.ts` to
directly cover the `mcpPage.waitForTextOnPage` method.
- Converted all tool handler tests in `tests/tools/snapshot.test.ts`
(`take_snapshot` and `wait_for`) to mock unit tests using
`createHandlerMocks()`, completely removing `withMcpContext` from the
file.
- Asserted exact method calls on `response` and `page` using
`sinon.assert.calledOnceWithExactly()`.
- Maintained `afterEach(() => sinon.restore())` cleanup per `AGENTS.md`.

### Verification
- `npm run test tests/tools/snapshot.test.ts` (4 passed, 0 failed, exit
code: 0)
- `npm run test tests/McpPage.test.ts` (39 passed, 0 failed, exit code:
0)
- `npm run check-format` (exit code: 0)
- `npm run docs:generate` (exit code: 0)
- `git status` (clean working tree, exit code: 0)
pull Bot pushed a commit to Pet3cy/chrome-devtools-mcp that referenced this pull request Sep 11, 2026
This PR migrates the mock-eligible test in
`tests/tools/extensions.test.ts` from a real-browser test
(`withMcpContext`) to a mock-based unit test using the established Sinon
mock infrastructure.

### Architectural Principle (Category 1 vs. Category 2 Split)
This PR continues issue ChromeDevTools#2639 (item 1), applying the core architectural
separation of concerns established across ChromeDevTools#2641, ChromeDevTools#2665, ChromeDevTools#2683, and
ChromeDevTools#2698:

- **Category 1 (Handler wiring & response verification -> Mock unit
tests)**:
Tests that only verify tool parameter parsing and response manipulation
should not launch real browsers. Launching Chrome just to assert that a
tool invoked `response.setListExtensions()` introduces unnecessary
startup latency and lifecycle flakiness without providing real
integration coverage. These are converted to deterministic unit tests
using `createHandlerMocks()`.

- **Category 2 (Genuine browser, extension, and protocol behavior ->
Retained real-browser tests)**:
Tests that depend on genuine browser mechanics must remain in
`withMcpContext`. Mocking them would require simulating Chrome's
internal extension loader, V8 script execution, or Puppeteer's target
lifecycle inside mocks, creating brittle and artificial tests that do
not validate real behavior.

### Summary of Changes
- **Converted to mocks (Category 1)**:
- `lists installed extensions`: Converted to `createHandlerMocks()`.
Asserts `response.setListExtensions` was invoked with
`sinon.assert.calledOnceWithExactly(response.setListExtensions)`.
- **Intentionally retained as real-browser tests (Category 2)**:
- `installs and uninstalls an extension and verifies it in
chrome://extensions`: Genuinely installs an unpacked extension into
Chrome and verifies real extension lifecycle and management.
- `reloads an extension`: Verifies Chrome's live extension reload
mechanics and registry reappearance.
- `triggers an extension action`: Validates asynchronous Puppeteer
`Target` creation when an extension action fires.
- `verifies that content script console logs are received`: Tests real
content script execution in a live navigated page and CDP console
capture.

### Verification
- `powershell -Command "Remove-Item -Recurse -Force build -ErrorAction
SilentlyContinue"; npm run build` (exit code: 0)
- `node scripts/test.js tests/tools/extensions.test.ts` (6 passed, 0
failed, exit code: 0)
- `npm run check-format` (exit code: 0)
- `npm run docs:generate` (exit code: 0)
- `git status` (clean working tree, exit code: 0)
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