Skip to content

Instantly share code, notes, and snippets.

View gauntface's full-sized avatar
🤓
Mon-Fri: Working. Sat-Sun: Not Working.

Matt Gaunt-Seo gauntface

🤓
Mon-Fri: Working. Sat-Sun: Not Working.
View GitHub Profile
@gauntface
gauntface / fake-push.js
Created March 6, 2017 22:11
Example of a fake push event
const fakePushEvent = new PushEvent('push', {
data: JSON.stringify(pushData),
});
@gauntface
gauntface / sw-unit-tests.js
Created March 6, 2017 22:10
Wait for message and start mocha test runner.
self.addEventListener('message', (event) => {
if (event.data !== 'run-tests') {
return;
}
const runResults = mocha.run();
runResults.on('end', () => {
event.ports[0].postMessage({
failures: runResults.failures,
total: runResults.total,
@gauntface
gauntface / run-sw-tests.js
Created March 6, 2017 22:09
Run service worker unit tests and report if it failed.
it('should run sw-unit-tests.js unit tests', function() {
return navigator.serviceWorker.register('/test/sw/sw-unit-tests.js')
.then((reg) => {
return window.__waitForSWState(reg, 'activated');
})
.then((serviceWorker) => {
return sendMessage(serviceWorker, 'run-tests');
})
.then((results) => {
if (results.failures > 0) {
@gauntface
gauntface / run-sw-tests.js
Last active March 6, 2017 22:10
Example of sending a message to a service worker.
const sendMessage = (serviceWorker, message) => {
return new Promise(function(resolve, reject) {
const messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) {
if (event.data.error) {
reject(event.data.error);
} else {
resolve(event.data);
}
};
@gauntface
gauntface / sw-unit-tests.js
Created March 6, 2017 22:06
An example of Mocha running in a service worker / web worker.
importScripts('/node_modules/mocha/mocha.js');
mocha.setup({
ui: 'bdd',
reporter: null,
});
describe('First SW Test Suite', function() {
it('should test something', function() {
...
@gauntface
gauntface / run-sw-tests.js
Created March 6, 2017 22:06
Example browser test that registers a service worker file that will contain JS Unit Tests
describe('Run SW Unit Tests', function() {
beforeEach(function() {
return window.__testCleanup();
});
after(function() {
return window.__testCleanup();
});
it('should run sw-unit-tests.js unit tests', function() {
@gauntface
gauntface / run-sw-tests.js
Created March 6, 2017 22:04
Run service worker tests.
<script src="/test/browser/run-sw-tests.js"></script>
@gauntface
gauntface / my-first-test.js
Created March 6, 2017 22:03
Cleaning up before and after tests.
beforeEach(function() {
return window.__testCleanup();
});
after(function() {
return window.__testCleanup();
});
@gauntface
gauntface / index.html
Created March 6, 2017 22:03
Adding cleanup function to sw tests.
<script src="/test/utils/sw-test-cleanup.js"></script>
@gauntface
gauntface / sw-test-cleaup.js
Last active April 22, 2024 05:02
Function to unregister SW and clear out old caches.
window.__testCleanup = () => {
const unregisterSW = () => {
return navigator.serviceWorker.getRegistrations()
.then((registrations) => {
const unregisterPromise = registrations.map((registration) => {
return registration.unregister();
});
return Promise.all(unregisterPromise);
});
};