Socket
Socket
Sign inDemoInstall

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 - npm Package Compare versions

Comparing version 1.0.0 to 1.2.0

models/Headers.js

8

index.js

@@ -0,1 +1,2 @@

const url = require('url');
const CacheStorage = require('./models/CacheStorage');

@@ -5,2 +6,4 @@ const Clients = require('./models/Clients');

const handleEvents = require('./utils/events').handleEvents;
const Request = require('./models/Request');
const Response = require('./models/Response');

@@ -33,3 +36,6 @@ module.exports = function makeServiceWorkerEnv() {

};
}
},
Request: Request,
Response: Response,
URL: url.URL || url.parse
};

@@ -36,0 +42,0 @@

3

package.json
{
"name": "service-worker-mock",
"version": "1.0.0",
"version": "1.2.0",
"main": "index.js",

@@ -18,2 +18,3 @@ "repository": {

"workers",
"testing",
"mock"

@@ -20,0 +21,0 @@ ],

@@ -6,3 +6,3 @@ Service Worker Mock

## 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.
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.

@@ -18,2 +18,5 @@ The service worker mock creates an environment with the following properties, based on the current [Mozilla Service Worker Docs](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API).

addEventListener: Function,
Request: constructor Function,
Response: constructor Function,
URL: constructor Function,

@@ -39,4 +42,23 @@ // Test helpers

## 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.
```js
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();
});
});
```
## Use
The following is an example snippet derived from [__tests__/basic.js](https://github.com/pinterest/service-workers/blob/master/packages/service-worker-mock/__tests__/basic.js). The test is based on the [service worker example](https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/basic/service-worker.js) provided by Google.
The following is an example snippet derived from [__tests__/basic.js](https://github.com/pinterest/service-workers/blob/master/packages/service-worker-mock/__tests__/basic.js). The test is based on the [service worker example](https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/basic/service-worker.js) provided by Google. In it, we will verify that on `activate`, the service worker deletes old caches and creates the new one.

@@ -47,5 +69,9 @@ ```js

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

@@ -59,2 +85,3 @@ // Create old cache

expect(self.snapshot().caches.OLD_CACHE).toBeUndefined();
expect(self.snapshot().caches['precache-v1']).toBeDefined();
});

@@ -61,0 +88,0 @@ });

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