Vitest preset
Installation
Add a single entry to vitest.config.ts:
import { defineConfig } from 'vitest/config';
export default defineConfig({ test: { setupFiles: ['extforge/testing/vitest'], },});No other setup is required.
What it does
On first import, the preset:
- Calls
installChromeFakes()— mounts fakechrome.*namespaces onglobalThis.chrome. Each namespace is wrapped in a Proxy so accessing an unmodeled method throws immediately with a descriptive error. - Registers a
beforeEachhook — callsresetChromeFakes(fakes)before every test to clear storage state, listener arrays, and spy call counts. - Exports the
fakesbag — so test files can fire events and inspect state.
The fakes export
import { fakes } from 'extforge/testing/vitest';fakes is a ChromeFakes object:
interface ChromeFakes { runtime: RuntimeFake; // fakes.runtime.fireOnMessage(...) storage: StorageFake; // fakes.storage.chrome.local.__state() tabs: TabsFake; // fakes.tabs.__seed([...]) action: ActionFake; // fakes.action.reset() scripting: ScriptingFake; // fakes.scripting.__nextResult(value) reset(): void; // called automatically by beforeEach}Each namespace’s fake is documented in chrome-fakes.
When NOT to use the preset
Do not use the preset if:
- You already define
globalThis.chromein your test environment (the preset throws ifchromeis already defined). - You need fine-grained control over which fakes are installed per test suite. Use
createChromeFakes()or the per-namespace factories (createRuntimeFake(), etc.) instead and install them manually. - You are testing in a non-Vitest environment (e.g. Jest). The preset imports
beforeEachfrom Vitest. For Jest, construct fakes manually and registerbeforeEach(() => fakes.reset())yourself.