Skip to content

test: migrate screencast tests off withMcpContext to unit tests - #2683

Merged
OrKoN merged 6 commits into
ChromeDevTools:mainfrom
maheshsingh20:test/screencast-unit-tests
Sep 8, 2026
Merged

OrKoN merged 6 commits into
ChromeDevTools:mainfrom
maheshsingh20:test/screencast-unit-tests

Conversation

@maheshsingh20

Copy link
Copy Markdown
Contributor

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

@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN, opened #2683 to convert tests/tools/screencast.test.ts off withMcpContext, following the same pattern from #2641/#2665. All 12 tests migrated to createHandlerMocks(), plus a createMockScreenRecorder() helper added to mocks.ts. Full npm test, build, and check-format all passing locally. Ready for your review whenever you get a chance.

Comment thread tests/mocks.ts Resolved
Comment thread tests/tools/screencast.test.ts Resolved
Comment thread tests/mocks.ts Resolved
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN
Applied all changes mentioned in feedbacks. Ready for review.

Comment thread tests/tools/screencast.test.ts Resolved
@maheshsingh20

maheshsingh20 commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN
Addressed all six:

  • Removed screenRecorderData and callsFake state simulation from mocks.ts — both stubs are now bare/unconfigured.
  • Replaced as unknown as MockMcpPage + Object.defineProperty with Object.assign(page, {pptrPage}).
  • Removed all as never / as `${string}.mp4` / as `${string}.webm` casts — typed via declaration annotations instead.
  • Asserted context.ensureExtension calls with expected path/extension throughout.
  • Asserted context.setScreenRecorder calls directly via calledOnceWithExactly / notCalled.
  • Passed explicit filePath in both CLI-option tests to avoid real temp directory creation.

All 12 tests passing, build/format/docs clean. Pushed as 267274f.

@OrKoN
OrKoN enabled auto-merge September 8, 2026 08:57
@OrKoN
OrKoN added this pull request to the merge queue Sep 8, 2026
Merged via the queue into ChromeDevTools:main with commit 02c5911 Sep 8, 2026
20 checks passed
@maheshsingh20
maheshsingh20 deleted the test/screencast-unit-tests branch September 8, 2026 09:23
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN, thanks for the thorough review on this one - that second round of feedback tightened things up nicely. screencast.test.ts is now fully migrated. Let me know what's next on the list for item (1) whenever you're ready.

@OrKoN

OrKoN commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Let's try to do https://github.com/ChromeDevTools/chrome-devtools-mcp/blob/main/tests/tools/snapshot.test.ts next

Hi @OrKoN, following up on #2683 and continuing #2639 (item 1), I have opened #2698 to migrate tests/tools/snapshot.test.ts.

  • Converted to mock unit test: includes a snapshot under browser_snapshot now uses createHandlerMocks() with sinon.assert.calledOnceWithExactly(), eliminating unnecessary browser launches for pure response verification.
  • Stub cleanup: Added afterEach(() => sinon.restore()) per AGENTS.md.
  • Intentionally retained real-browser tests: All 6 browser_wait_for tests remain in withMcpContext since they verify genuine DOM updates, live accessibility-tree text matching, iframe traversal, and timing behavior.

All verification steps (npm run test tests/tools/snapshot.test.ts, npm run check-format, npm run docs:generate) passed cleanly with exit code 0. Please take a look!

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