Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jest-webextension-mock

RickyMarou94.8kISC4.0.0

Mock the components of a WebExtension

readme

npm npm Codecov Greenkeeper badge Twitter

Install

For npm:

npm i --save-dev jest-webextension-mock

For yarn:

yarn add --dev jest-webextension-mock

Setup

Require module directly

In your package.json under the jest section add the setupFiles attribute with this module name.

"jest": {
  "setupFiles": [
    "jest-webextension-mock"
  ]
}

Use setup file

Alternatively you can create a new setup file and require this module.

__setups__/chrome.js

require('jest-webextension-mock');

And add that file to your setupFiles:

"jest": {
  "setupFiles": [
    "./__setups__/chrome.js"
  ]
}

Usage

Use this module to check that API calls were made when expected.

describe('your function to test', () => {
  it('should have called a webextension API', () => {
    yourFunctionToTest();
    expect(chrome.tabs.update).toHaveBeenCalled();
  });
});

Check the API was called with certain parameters.

describe('your function to test', () => {
  it('should have called a webextension API', () => {
    yourFunctionToTest();
    expect(chrome.tabs.update).toHaveBeenCalledWith({
      url: 'https://example.com/'
    });
  });
});

And you can reset the API mocks to ensure APIs are only called when needed.

beforeEach(() => {
  browser.geckoProfiler.start.mockClear();
  browser.geckoProfiler.stop.mockClear();
});

it('should toggle the profiler on from stopped', () => {
  const store = mockStore(reducer(undefined, {}));
  const expectedActions = [
    { type: 'PROFILER_START', status: 'start' },
    { type: 'PROFILER_START', status: 'done' },
  ];
  return store.dispatch(actions.toggle()).then(() => {
    expect(browser.geckoProfiler.start).toHaveBeenCalledTimes(1);
    expect(store.getActions()).toEqual(expectedActions);
  });
});

Development

npm install
npm test

changelog

jest-webextension-mock

4.0.0

Major Changes

  • b8594f9: Drop support for node 14 and 16, add support for node 18 and 22

3.9.1

Patch Changes

  • Fix storage returning undefined for unknown keys

3.9.0

Minor Changes

  • Add support for storage.session

3.8.16

Patch Changes

  • Fix storage.get method throwing when not provided with a key

3.8.15

Patch Changes

  • Forgot to build the package before publishing 🤦

3.8.14

Patch Changes

  • Fix different storage types using the same store

3.8.13

Patch Changes

  • 2800518: Add storage onChange event listeners