Skip to content
Branch: master
Find file History

Latest commit

kevhender Update Readme to reflect changes to listeners map (#134)
In v2, `self.listeners` is a `Map` object, instead of a plain `object`. This change updates the readme to give examples that use this updated object, rather than the v1 method.
Latest commit 944e508 Feb 12, 2020


Type Name Latest commit message Commit time
Failed to load latest commit information.
__tests__ Publish minor to 2.0.5 (#133) Feb 12, 2020
models Publish minor to 2.0.5 (#133) Feb 12, 2020
utils Publish minor to 2.0.5 (#133) Feb 12, 2020
LICENSE Initial public commit Mar 8, 2017 Update Readme to reflect changes to listeners map (#134) Feb 12, 2020
fetch.js Workbox mock upstream (#109) Jan 29, 2019
index.js Workbox mock upstream (#109) Jan 29, 2019
package.json Publish minor to 2.0.5 (#133) Feb 12, 2020
yarn.lock update shelving-mock-indexeddb to 1.1.0 Sep 4, 2019

Service Worker Mock

A mock service worker environment generator.


Testing service workers is difficult. Each file produces side-effects by calls to self.addEventListener, and the service worker environment is unlike a normal web or node context. This package makes it easy to turn a Node.js environment into a faux service worker environment. Additionally, it adds some helpful methods for testing integrations.

The service worker mock creates an environment with the following properties, based on the current Mozilla Service Worker Docs.

const env = {
  // Environment polyfills
  skipWaiting: Function,
  caches: CacheStorage,
  clients: Clients,
  registration: ServiceWorkerRegistration,
  addEventListener: Function,
  Request: constructor Function,
  Response: constructor Function,
  URL: constructor Function,

  // Test helpers
  listeners: Map,
  trigger: Function,
  snapshot: Function,
Test Helper description
listeners [Map] A key/value map of active listeners (install/activate/fetch/etc).
trigger [Function] Used to trigger active listeners (await self.trigger('install')).
snapshot [Function] Used to generate a snapshot of the service worker internals (see below).
Snapshot Property description
caches [Object] A key/value map of current cache contents.
clients [Array] A list of active clients.
notifications [Array] A list of active notifications

Additionally we provide a fetch mock in service-worker-mock/fetch to easily get up and running (see Getting Started for example).

Getting Started

The service worker mock is best used by applying its result to the global scope, then calling require('../sw.js') with the path to your service worker file. The file will use the global mocks for things like adding event listeners.

const makeServiceWorkerEnv = require('service-worker-mock');
const makeFetchMock = require('service-worker-mock/fetch');

describe('Service worker', () => {
  beforeEach(() => {
      // If you're using sinon ur similar you'd probably use below instead of makeFetchMock
      // fetch: sinon.stub().returns(Promise.resolve())
  it('should add listeners', () => {


The following is an example snippet derived from tests/basic.js. The test is based on the service worker example provided by Google. In it, we will verify that on activate, the service worker deletes old caches and creates the new one.

const makeServiceWorkerEnv = require('service-worker-mock');

describe('Service worker', () => {
  beforeEach(() => {
    Object.assign(global, makeServiceWorkerEnv());
  it('should delete old caches on activate', async () => {

      // Create old cache

      // Activate and verify old cache is removed
      await self.trigger('activate');



You can’t perform that action at this time.