Repository navigation
test: migrate emulation tests off withMcpContext to unit tests - #2665
Conversation
|
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. |
|
Hi @OrKoN Moved the devtoolsUniverse session mock into createMcpPage() — removed duplication across tests. All 82 tests passing (32 McpPage + 50 emulation), build clean, check-format clean. |
|
@maheshsingh20 could you please mark individual comments as resolved as unresolved comments will prevent this from landing? |
Yes marked!! |
|
Hi @OrKoN, |
|
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. |
…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.
… 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)
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)
Relates to #2639.
Follow-up to #2641 and feedback in #2660 (
AGENTS.md).Summary of Changes
tests/tools/emulation.test.ts:withMcpContext, removing all real-browser launches,serverHooks, and HTTP fixtures.geolocation,viewport,userAgent,extraHttpHeaders,colorScheme,network,cpu) now usecreateHandlerMocks()fromtests/mocks.tsand verifypage.emulate()is invoked with exact expected arguments.tests/McpPage.test.ts:McpPage.emulate()usingcreateMockPuppeteerPage():Verification
npm run check-format: Passed (clean ESLint + Prettier)npm run build: Compiled with exit code 0node scripts/test.js tests/tools/emulation.test.ts: 50/50 tests passednode scripts/test.js tests/McpPage.test.ts: 32/32 tests passed