Refactor: extract chrome.scripting registry mock into shared helper#218
Merged
Conversation
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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #217. Extracts the duplicated
chrome.scripting.registerContentScripts/unregisterContentScripts/getRegisteredContentScriptsmock from three near-identical registration tests into a sharedinstallScriptingRegistry()helper undersrc/__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 threechrome.scriptingmocks 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
let registry; beforeEach(() => { registry = installScriptingRegistry(); }). InternallyinstallScriptingRegistry()resets the underlyingjest.Mocks before installing new implementations backed by a per-test store closure. Cleaner than exposing areset()method that callers would need to remember.registerMock,unregisterMock,getRegisteredMock) are exposed on the handle so a one-off test canmockImplementationOnce(() => 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 insidejest.isolateModulesAsyncbefore importing the registration module under test, mirroring how the inline mocks were used.__test-mocks__/, notlib/— it's test-environment plumbing, depends onchrome.scriptinghaving been stubbed bychrome-mv3-extras.ts, and never ships in production bundles.shadow-root-probe-registration.test.tspreviously hadshadowProbeRegisterMock/ShadowProbeRegistrationModuleprefixes 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