Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

service-worker-mock

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

service-worker-mock

Service Worker Mock ========================= A mock service worker environment generator.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
22K
decreased by-4.54%
Maintainers
1
Weekly downloads
 
Created
Source

Service Worker Mock

A mock service worker environment generator.

Why?

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 any environment into a feaux 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,

  // Test helpers
  listeners: Object,
  trigger: Function,
  snapshot: Function,
};
Test Helperdescription
listeners[Object] 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 Propertydescription
caches[Object] A key/value map of current cache contents.
clients[Array] A list of active clients.
notifications[Array] A list of active notifications

Use

The following is an example snippet derived from tests/basic.js. The test is based on the service worker example provided by Google.

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

describe('Service worker', () => {
  it('should delete old caches on activate', async () => {
      Object.assign(global, makeServiceWorkerEnv());
      require('./path/to/sw.js');

      // Create old cache
      await self.caches.open('OLD_CACHE');
      expect(self.snapshot().caches.OLD_CACHE).toBeDefined();

      // Activate and verify old cache is removed
      await self.trigger('activate');
      expect(self.snapshot().caches.OLD_CACHE).toBeUndefined();
  });
});

License

MIT

Keywords

FAQs

Package last updated on 08 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc