Registry / testing / webextensions-api-mock

webextensions-api-mock

JSON →
library1.0.0jsnpmunverified

Automatically generated sinon stubs for the WebExtensions API, designed to make browser extension testing easy. Version 1.0.0 uses the Firefox 72.0.2 schema and provides TypeScript types. Sinon is a peer dependency, not bundled, to avoid instanceof issues. Includes API and CLI to update schema to latest stable Firefox via webextensions-schema. Produces a fresh sandbox per call, with sinonSandbox exposed. Ideal for unit testing WebExtensions against sinon assertions and stubs.

npm install webextensions-api-mock
INSTALL
IMPORT
SIG · WEBEXTENSIONS-API-
W
webextensions-api-mock
testingjavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (browserMock)
import browserMock from 'webextensions-api-mock';
const browserMock = require('webextensions-api-mock');
Package is ESM-first. For CommonJS, use const { default: browserMock } = require('webextensions-api-mock');
update
import { update } from 'webextensions-api-mock';
const { update } = require('webextensions-api-mock');
Named export for updating schema; also callable via CLI: webextensions-api-mock update
browserMock
import browserMock from 'webextensions-api-mock'; const browser = browserMock();
const browser = new browserMock();
browserMock is a function that creates a new browser object, not a constructor.

Creates a WebExtensions API mock, calls a method, asserts stub behavior, triggers event listeners, and resets history.

import browserMock from 'webextensions-api-mock'; import { assert } from 'sinon'; // Create a fresh browser mock const browser = browserMock(); // Simulate production code calling browser.tabs.create (browser.tabs.create as any)({ url: 'https://example.com' }); // Assert the stub was called assert(browser.tabs.create.calledOnce, 'tabs.create should have been called'); // Trigger an event listener browser.tabs.onCreated.addListener.yield(); // Reset all stubs via the sandbox browser.sinonSandbox.resetHistory();
Debug
Known issues
deprecatedSchema based on Firefox 72.0.2; newer APIs may be missing.
fix
Run `webextensions-api-mock update` to sync to latest Firefox stable schema, or manually update via API.
affects: >=1.0.0
gotchasinon is a peer dependency, not bundled; version mismatch can cause assertion errors due to instanceof checks.
fix
Install sinon as devDependency: npm install --save-dev sinon webextensions-api-mock
affects: >=1.0.0
gotchaEach call to browserMock() creates a new sinon sandbox; stubs are isolated. Reusing the same browser object across tests may cause state leakage.
fix
Create a new browserMock() in each test or use beforeEach to reset via browser.sinonSandbox.reset()
affects: >=1.0.0
gotchaEvent listeners are stubs with .addListener. Triggering with yield() works but does not simulate real event arguments.
fix
Pass mock arguments to yield() to simulate event data: browser.tabs.onCreated.addListener.yield({ id: 1, url: '...' });
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'sinon'
sinon is a peer dependency and not installed automatically.
fix
npm install --save-dev sinon@^7.5.0
browserMock is not a constructor
Attempting to use new browserMock() instead of calling it as a function.
fix
const browser = browserMock();
Cannot read property 'create' of undefined
Method namespace (e.g., tabs) not available because schema might not include it or upgrade needed.
fix
Run `webextensions-api-mock update` to get latest schema, or check Firefox version compatibility.
browser.sinonSandbox.resetHistory is not a function
Property name typo (e.g., sinonSandbox vs sinonSandbox).
fix
Use browser.sinonSandbox.resetHistory() (note: 'sinonSandbox' with capital S).
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
sinonrequiredPeer dependency: sinon stubs are used for mocking. Must be installed separately to avoid instanceof issues.
Agent activity
15 hits · last 30 days
node
14
Resources
webextensions-api-mock — npm install webextensions-api-mock · libregistry