Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adopt jest-webextension-mock for chrome API stubs#143

Merged
twschiller merged 1 commit into
mainfrom
chore/adopt-jest-webextension-mock
Jun 5, 2026
Merged

Adopt jest-webextension-mock for chrome API stubs#143
twschiller merged 1 commit into
mainfrom
chore/adopt-jest-webextension-mock

Conversation

@twschiller

Copy link
Copy Markdown
Contributor

Summary

Replaces three hand-rolled `(globalThis as unknown as { chrome: ... }).chrome = { ... }` stubs with jest-webextension-mock 4.1.1, wired through Jest's `setupFiles`. Matches the convention from the team's other project.

`jest-webextension-mock` installs `globalThis.chrome` (and `browser`) with `jest.fn()` stubs for runtime, storage, tabs, action, browserAction, and the rest of the MV2/MV3 surface. It does not ship `chrome.scripting` (the MV3 content-script registration API), so a small `chrome-mv3-extras.ts` setup file patches that namespace in after `jest-webextension-mock`'s main. Both wired via `setupFiles` in `jest.config.cjs`.

Per-file changes

  • `roach-motel-annotate.test.ts` — drop the inline chrome stub; alias `chrome.runtime.sendMessage` as `jest.Mock` for the `.mockResolvedValue` / `.toHaveBeenCalledWith` control surface.
  • `webdriver-probe-annotate.test.ts` — same pattern; the `callsOfType` helpers that filter the mock's call log by message type are preserved.
  • `webdriver-probe-registration.test.ts` — drop the local `ChromeStub` interface and the per-`loadModule` stub object; `mockImplementation` on `chrome.scripting.{register,unregister,getRegistered}ContentScripts` preserves the stateful registration store.

Side cleanup

Drops a now-stale `biome-ignore` in `rule-engine.test.ts` (the explicit `Record<string, unknown>` type arg on `jest.requireActual` meant no `any` flowed through, so the suppression was unused).

Test plan

  • `bun run preflight` passes
  • All 1073 tests pass (no test count change vs main)
  • The three migrated tests still cover their original cases

🤖 Generated with Claude Code

Replace three hand-rolled `(globalThis as unknown as { chrome: ... }).chrome
= { ... }` stubs in test files with jest-webextension-mock 4.1.1 wired
through Jest's `setupFiles`. Matches the convention used in the team's
other project.

jest-webextension-mock installs `globalThis.chrome` (and `browser`) with
jest.fn() stubs for runtime, storage, tabs, action, browserAction, and
the rest of MV2/MV3 surface. It does not include chrome.scripting (the
MV3 content-script registration API), so add a small
`chrome-mv3-extras.ts` setup file that patches that namespace in after
jest-webextension-mock's main. Wired via setupFiles in jest.config.cjs.

Test-side changes per file:
- roach-motel-annotate.test.ts: drop the inline chrome stub; aliases
  chrome.runtime.sendMessage as a jest.Mock for the .mockResolvedValue /
  .toHaveBeenCalledWith control surface.
- webdriver-probe-annotate.test.ts: same pattern; keeps the
  callsOfType / injectCalls / detectionCalls helpers that filter the
  mock's call log by message type.
- webdriver-probe-registration.test.ts: drop the local ChromeStub
  interface and the per-loadModule stub object; mockImplementation on
  chrome.scripting.{register,unregister,getRegistered}ContentScripts
  preserves the stateful registration store.

Drop the now-unused biome-ignore in rule-engine.test.ts (the explicit
type arg on jest.requireActual<Record<string, unknown>> meant no any
flowed through, but the suppression was left behind).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@vercel

vercel Bot commented Jun 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agent-browser-shield-demo-site Ready Ready Preview, Comment Jun 5, 2026 2:01am

Request Review

@twschiller twschiller merged commit 4c2f5ae into main Jun 5, 2026
7 checks passed
@twschiller twschiller deleted the chore/adopt-jest-webextension-mock branch June 5, 2026 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant