npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hipsterbrown/cloudflare-worker-mock

v1.0.0

Published

Cloudflare Worker Mock ========================= A fork of [Pinterest's mock service worker environment generator](https://npmjs.com/package/service-worker-mock) with support for cloudflare worker APIs.

Downloads

2

Readme

Cloudflare Worker Mock

A fork of Pinterest's mock service worker environment generator with support for cloudflare worker APIs.

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 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: Object,
  trigger: Function,
  snapshot: Function,
};

Test Helper | description -------------- | ----------- 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 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(() => {
    Object.assign(
      global,
      makeServiceWorkerEnv(),
      makeFetchMock(),
      // If you're using sinon ur similar you'd probably use below instead of makeFetchMock
      // fetch: sinon.stub().returns(Promise.resolve())
    );
    jest.resetModules();
  });
  it('should add listeners', () => {
    require('../sw.js');
    expect(self.listeners['install']).toBeDefined();
    expect(self.listeners['activate']).toBeDefined();
    expect(self.listeners['fetch']).toBeDefined();
  });
});

Use

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());
    jest.resetModules();
  });
  
  it('should delete old caches on activate', async () => {
      require('../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();
      expect(self.snapshot().caches['precache-v1']).toBeDefined();
  });
});

License

MIT