# webextensions-api-mock

> WebExtensions API as sinon stubs

Latest version **1.0.0** (published 2020-01-28) · MPL-2.0 license · 0 weekly downloads

## Install

```sh
npm install webextensions-api-mock
pnpm add webextensions-api-mock
yarn add webextensions-api-mock
bun add webextensions-api-mock
```

Provides the command `webextensions-api-mock`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-01-28 |
| First published | 2019-11-21 |
| Weekly downloads | 0 |
| License | MPL-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10.0.0 |
| Dependencies | 2 |
| Unpacked size | 742.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | stoically |
| Maintainers | stoically |
| Keywords | webextensions, schemas |

## Links

- npm: https://www.npmjs.com/package/webextensions-api-mock
- Repository: https://github.com/stoically/webextensions-api-mock
- Issues: https://github.com/stoically/webextensions-api-mock/issues
- npm.io page: https://npm.io/package/webextensions-api-mock

## Dependencies (2)

- [prettier](https://npm.io/package/prettier.md) ^1.19.1
- [webextensions-schema](https://npm.io/package/webextensions-schema.md) ^0.5.0

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2020-01-28
- 0.2.5 — 2019-11-25
- 0.2.4 — 2019-11-24
- 0.2.3 — 2019-11-24
- 0.2.2 — 2019-11-23
- 0.2.1 — 2019-11-21
- 0.2.0 — 2019-11-21
- 0.1.2 — 2019-11-21
- 0.1.1 — 2019-11-21
- 0.1.0 — 2019-11-21

## README

# WebExtensions API Mock

Automatically generated [`sinon stubs`](https://sinonjs.org/releases/latest/stubs/), together with types, for the [WebExtensions API](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API). Currently based on the Firefox version _72.0.2_ schema. Comes with an API and CLI that lets you update the schema and types to the _latest stable Firefox version_, thanks to [`webextensions-schema`](https://github.com/stoically/webextensions-schema).

To get globally declared `window.browser` types for production code I'd recommened installing `@types/firefox-webext-browser`, those are automatically generated by [jsmnbom/definitelytyped-firefox-webext-browser](https://github.com/jsmnbom/definitelytyped-firefox-webext-browser)

## Install

```shell
npm install --save-dev sinon webextensions-api-mock
```

**Important**: `sinon` is a peer dependency, so you have to install it yourself. That's because it can otherwise lead to unexpected assertion behavior when sinon does `instanceof` checks internally. It also allows to upgrade sinon without the need to bump the version in `webextensions-api-mock`.

## Usage

```ts
import browserMock from 'webextensions-api-mock';
// or
// const { default: browserMock } = require('webextensions-api-mock');

const browser = browserMock();
```

All `browser` methods are now [`sinon stubs`](https://sinonjs.org/releases/latest/stubs/) and hence you can use them as usual in your test, e.g. if your production code calls `browser.tabs.create()` you can assert it

```ts
assert(browser.tabs.create.called);
```

To trigger event listeners, you can use the [`yield` method on stubs](https://sinonjs.org/releases/latest/stubs/).

```ts
browser.tabs.onCreated.addListener.yield();
```

Every call to `browserMock()` creates a new `browser` object based on a new
[`sinon sandbox`](https://sinonjs.org/releases/latest/sandbox/). The sinon sandbox itself is exposed as `sinonSandbox` property on the
`browser` object. So to e.g. reset the history of all browser stubs, you'd call

```ts
browser.sinonSandbox.resetHistory();
```

## Update schema and types

Update the schema and types to the latest available stable Firefox version. Feel
free to PR updated schema and types if they're not up-to-date.

### Using API

```ts
import { update } from 'webextensions-api-mock';
// or
// const { update } = require('webextensions-api-mock');

(async () => {
  await update();
})();
```

### Using CLI

```
webextensions-api-mock update
```

---
_Source: https://npm.io/package/webextensions-api-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
