![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
service-worker-mock
Advanced tools
Readme
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: 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 |
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');
describe('Service worker', () => {
beforeEach(() => {
Object.assign(global, makeServiceWorkerEnv());
jest.resetModules();
});
it('should add listeners', () => {
require('../sw.js');
expect(self.listeners['install']).toBeDefined();
expect(self.listeners['activate']).toBeDefined();
expect(self.listeners['fetch']).toBeDefined();
});
});
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();
});
});
MIT
FAQs
Service Worker Mock ========================= A mock service worker environment generator.
The npm package service-worker-mock receives a total of 22,758 weekly downloads. As such, service-worker-mock popularity was classified as popular.
We found that service-worker-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.