Skip to content

Commit

Permalink
tests: add smoketest for slow service worker (#6648)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Nov 27, 2018
1 parent 3f0a0dd commit a203a36
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lighthouse-cli/test/fixtures/offline-ready-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ const RUNTIME = 'runtime';

// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', event => {
self.skipWaiting();
if (self.location.search.includes('slow')) {
event.waitUntil(new Promise(resolve => setTimeout(resolve, 5000)));
} else {
self.skipWaiting();
}

const populateCaches = caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS));
Expand Down
18 changes: 14 additions & 4 deletions lighthouse-cli/test/fixtures/offline-ready.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,28 @@ <h4>
</h4>

<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/offline-ready-sw.js').then(function(registration) {
async function registerServiceWorker(qs = '') {
if (!('serviceWorker' in navigator)) return

try {
const registration = await navigator.serviceWorker.register(`/offline-ready-sw.js${qs}`)
console.log('service worker registration complete');

registration.addEventListener('statechange', e => {
console.log('sw registration is now', e.target.state);
});
}).catch(function(e) {
} catch (e) {
console.error('service worker is not so cool.', e);
throw e;
});
}
}

if (window.location.search.includes('slow')) {
window.addEventListener('load', () => registerServiceWorker('?delay=5000&slow'));
} else {
registerServiceWorker();
}

</script>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,17 @@ module.exports = [
},
},
},

{
requestedUrl: 'http://localhost:10503/offline-ready.html?slow',
finalUrl: 'http://localhost:10503/offline-ready.html?slow',
audits: {
'service-worker': {
score: 1,
},
'works-offline': {
score: 1,
},
},
},
];

0 comments on commit a203a36

Please sign in to comment.