Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add smoketest for slow service worker #6648

Merged
merged 2 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we check for this anywhere? Or is the hope that a smoke test will fail and this would be used for debugging.

Maybe we should be pulling in errors-in-console into all our smoke tests :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we check for this anywhere?

nope

Or is the hope that a smoke test will fail and this would be used for debugging.

Bingo!

Maybe we should be pulling in errors-in-console into all our smoke tests :)

Not a bad idea, followup! :D

});
}
}

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 @@ -156,4 +156,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,
},
},
},
];