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

Skip to content

Refactor: extract chrome.scripting registry mock into shared helper#218

Merged
twschiller merged 1 commit into
mainfrom
shadow-root-shared-scripting-mock
Jun 7, 2026
Merged

Refactor: extract chrome.scripting registry mock into shared helper#218
twschiller merged 1 commit into
mainfrom
shadow-root-shared-scripting-mock

Conversation

@twschiller

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #217. Extracts the duplicated chrome.scripting.registerContentScripts / unregisterContentScripts / getRegisteredContentScripts mock from three near-identical registration tests into a shared installScriptingRegistry() helper under src/__test-mocks__/. Net: 251 lines deleted, 130 added across the three tests + the helper.

Each main-world bundle test (webdriver-probe-registration, checkout-checkbox-defense-registration, shadow-root-probe-registration) had been carrying its own ~50-line block wiring the three chrome.scripting mocks into an in-memory store with the same push/splice-by-id/filter semantics. The new bundle (the closed-shadow probe added in #217) was the rule-of-three trigger.

Notable choices

  • Helper returns a fresh handle per calllet registry; beforeEach(() => { registry = installScriptingRegistry(); }). Internally installScriptingRegistry() resets the underlying jest.Mocks before installing new implementations backed by a per-test store closure. Cleaner than exposing a reset() method that callers would need to remember.
  • Escape hatches preserved — the underlying mocks (registerMock, unregisterMock, getRegisteredMock) are exposed on the handle so a one-off test can mockImplementationOnce(() => Promise.reject(...)) to exercise a registration failure without rewriting the helper.
  • seed() instead of a constructor arg — pre-populating the store is opt-in per test ("already registered before startup"). Callers add entries inside jest.isolateModulesAsync before importing the registration module under test, mirroring how the inline mocks were used.
  • Sits in __test-mocks__/, not lib/ — it's test-environment plumbing, depends on chrome.scripting having been stubbed by chrome-mv3-extras.ts, and never ships in production bundles.
  • Drops the prefix-collision workaroundshadow-root-probe-registration.test.ts previously had shadowProbeRegisterMock/ShadowProbeRegistrationModule prefixes to avoid colliding with the same identifiers in the sibling webdriver test (both files lack imports and TypeScript merges their global scopes). With the colliding identifiers now living in the helper module, both tests use clean names.

Test plan

  • npx jest src/lib/__tests__/webdriver-probe-registration.test.ts src/lib/__tests__/checkout-checkbox-defense-registration.test.ts src/lib/__tests__/shadow-root-probe-registration.test.ts — 15 tests pass with the new helper.
  • npx jest — full extension suite, 93 suites / 1856 tests pass.
  • bun run check — biome + eslint clean.
  • bun run typecheck — clean.
  • bun run knip — clean.

🤖 Generated with Claude Code

Three main-world bundle registration tests (webdriver-probe,
checkout-checkbox-defense, shadow-root-probe) had identical ~50-line
blocks wiring chrome.scripting.registerContentScripts /
unregisterContentScripts / getRegisteredContentScripts into a stateful
in-memory store. Each new bundle would copy-paste the same mock — the
canonical rule-of-three trigger.

Extracted to src/__test-mocks__/chrome-scripting-registry.ts as
installScriptingRegistry(), exposing { registerMock, unregisterMock,
getRegisteredMock, seed, registered }. The per-test variance (which
module to import, which startup function to call, which script literal
to seed) stays local.

Net: 251 lines removed, 130 added across the three tests + the shared
helper. The seed/registered abstraction also drops the inline
RegistrationModule prefix-collision workaround in
shadow-root-probe-registration.test.ts (the colliding identifiers moved
into the helper module).

Stacked on shadow-root-main-world-probe (PR #217).

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

vercel Bot commented Jun 7, 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 7, 2026 10:05pm

Request Review

@twschiller twschiller merged commit 8f69fc2 into main Jun 7, 2026
7 checks passed
@twschiller twschiller deleted the shadow-root-shared-scripting-mock branch June 7, 2026 22:06
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.

1 participant