vitest-chrome is a testing utility designed to provide a comprehensive mock of the Chrome Extension API, enabling developers to test their Chrome extension logic within a Vitest environment without requiring a real browser. As of version 0.1.0, it offers a complete set of mocked functions and events, all type-checked and based on the official `@types/chrome` definitions. This allows for robust and reliable testing of Chrome API interactions, including event listeners, synchronous API calls, and asynchronous functions relying on callbacks or `chrome.runtime.lastError`. The library is primarily used as a development dependency and integrates directly with Vitest's global setup to expose the mocked `chrome` object, simplifying the testing workflow for extension development.
npm install vitest-chromeVerified import paths — ran on the pinned version, not inferred.
Demonstrates mocking Chrome API events, adding listeners, programmatically triggering them with specific arguments using `callListeners`, and clearing them with `clearListeners`.
Use `.mockImplementation(() => { /* return value */ })` for synchronous functions and `.mockImplementation((args, callback) => callback(response))` for asynchronous functions with callbacks.Configure `setupFiles` in your `vitest.config.js` and add `import { chrome } from 'vitest-chrome'; Object.assign(global, chrome);` in the specified setup file (e.g., `vitest.init.ts`).Ensure your tests that assert on `lastError` explicitly set it within the mock implementation of the callback-based function, and access it only inside the mocked callback provided to the API call.
Ensure your Vitest configuration includes a `setupFiles` entry, and that the setup file contains `import { chrome } from 'vitest-chrome'; Object.assign(global, chrome);`Add a mock implementation for the specific API function, e.g., `chrome.runtime.getManifest.mockImplementation(() => ({ /* mock manifest data */ }));`Change `import chrome from 'vitest-chrome'` to `import { chrome } from 'vitest-chrome'` (or `import * as chrome from 'vitest-chrome'`).No dependency data recorded yet.