Repository navigation
test: migrate listExtensions test to unit test - #2704
Conversation
|
Hi @OrKoN, following up on #2698 and continuing #2639 (item 1), I've opened PR #2704 to migrate Applying our established Category 1 vs. Category 2 architectural split:
All verification steps ( |
Similar to other places, we should use the handlers in isolation using mocks and move e2e tests to McpContex.test.ts where the underlying logic is implemented. Perhaps we can combine all extension actions in a single test since this is also tested by Puppeteer. |
66c7310 to
5bc3086
Compare
|
Thanks for the feedback! I've updated the PR accordingly:
All tests, builds, and format checks ( |
OrKoN
left a comment
There was a problem hiding this comment.
LGTM thanks! could you please rebase?
cb3ffbd to
7706114
Compare
|
Hi @OrKoN , |
|
Hi @OrKoN, There was another rebase conflict, I've resolved it now. Please take another look when you get a chance. |
|
Let's do https://github.com/ChromeDevTools/chrome-devtools-mcp/blob/main/tests/tools/lighthouse.test.ts but the implementation for lighthouse is actually in the tool itself so we do need to keep e2e tests: what we should do is to use mocks for tests that do not test Lighthouse itself, e.g., tests like |
Hi @OrKoN, thanks for the pointer. Before I start on lighthouse.test.ts; confirming my understanding: since Lighthouse's audit logic is implemented directly in src/tools/lighthouse.ts (not delegated to McpPage/McpContext), tests verifying actual audit behavior (report content, scores, audit modes) stay real-browser, since there's no lower-level class to relocate that coverage to. Tests like 'restores emulation' that actually exercise McpPage's behavior via the lighthouse tool get converted to mocks at the handler level, with the underlying restoreEmulation() behavior verified directly in McpPage.test.ts if it isn't already. I'll go through the full file to check if any other tests fall into that second category beyond the one you named. Will open a PR once this is done. |
|
Yes, sounds right! |
|
Hi @OrKoN |
|
yeah that sounds fine! |
This PR migrates the mock-eligible test in
tests/tools/extensions.test.tsfrom 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 #2639 (item 1), applying the core architectural separation of concerns established across #2641, #2665, #2683, and #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 usingcreateHandlerMocks().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
lists installed extensions: Converted tocreateHandlerMocks(). Assertsresponse.setListExtensionswas invoked withsinon.assert.calledOnceWithExactly(response.setListExtensions).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 PuppeteerTargetcreation 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)