{"id":26641,"library":"webextensions-api-mock","title":"WebExtensions API Mock","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/stoically/webextensions-api-mock","tags":["javascript","webextensions","schemas","typescript"],"install":[{"cmd":"npm install webextensions-api-mock","lang":"bash","label":"npm"},{"cmd":"yarn add webextensions-api-mock","lang":"bash","label":"yarn"},{"cmd":"pnpm add webextensions-api-mock","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: sinon stubs are used for mocking. Must be installed separately to avoid instanceof issues.","package":"sinon","optional":false}],"imports":[{"note":"Package is ESM-first. For CommonJS, use const { default: browserMock } = require('webextensions-api-mock');","wrong":"const browserMock = require('webextensions-api-mock');","symbol":"default (browserMock)","correct":"import browserMock from 'webextensions-api-mock';"},{"note":"Named export for updating schema; also callable via CLI: webextensions-api-mock update","wrong":"const { update } = require('webextensions-api-mock');","symbol":"update","correct":"import { update } from 'webextensions-api-mock';"},{"note":"browserMock is a function that creates a new browser object, not a constructor.","wrong":"const browser = new browserMock();","symbol":"browserMock","correct":"import browserMock from 'webextensions-api-mock'; const browser = browserMock();"}],"quickstart":{"code":"import browserMock from 'webextensions-api-mock';\nimport { assert } from 'sinon';\n\n// Create a fresh browser mock\nconst browser = browserMock();\n\n// Simulate production code calling browser.tabs.create\n(browser.tabs.create as any)({ url: 'https://example.com' });\n\n// Assert the stub was called\nassert(browser.tabs.create.calledOnce, 'tabs.create should have been called');\n\n// Trigger an event listener\nbrowser.tabs.onCreated.addListener.yield();\n\n// Reset all stubs via the sandbox\nbrowser.sinonSandbox.resetHistory();\n","lang":"typescript","description":"Creates a WebExtensions API mock, calls a method, asserts stub behavior, triggers event listeners, and resets history."},"warnings":[{"fix":"Run `webextensions-api-mock update` to sync to latest Firefox stable schema, or manually update via API.","message":"Schema based on Firefox 72.0.2; newer APIs may be missing.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Install sinon as devDependency: npm install --save-dev sinon webextensions-api-mock","message":"sinon is a peer dependency, not bundled; version mismatch can cause assertion errors due to instanceof checks.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Create a new browserMock() in each test or use beforeEach to reset via browser.sinonSandbox.reset()","message":"Each call to browserMock() creates a new sinon sandbox; stubs are isolated. Reusing the same browser object across tests may cause state leakage.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pass mock arguments to yield() to simulate event data: browser.tabs.onCreated.addListener.yield({ id: 1, url: '...' });","message":"Event listeners are stubs with .addListener. Triggering with yield() works but does not simulate real event arguments.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"npm install --save-dev sinon@^7.5.0","cause":"sinon is a peer dependency and not installed automatically.","error":"Cannot find module 'sinon'"},{"fix":"const browser = browserMock();","cause":"Attempting to use new browserMock() instead of calling it as a function.","error":"browserMock is not a constructor"},{"fix":"Run `webextensions-api-mock update` to get latest schema, or check Firefox version compatibility.","cause":"Method namespace (e.g., tabs) not available because schema might not include it or upgrade needed.","error":"Cannot read property 'create' of undefined"},{"fix":"Use browser.sinonSandbox.resetHistory() (note: 'sinonSandbox' with capital S).","cause":"Property name typo (e.g., sinonSandbox vs sinonSandbox).","error":"browser.sinonSandbox.resetHistory is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}