test(docs-infra): unregister the SW after each test (#28290)

This ensures that the SW is cleaned up, even in cases where
synchronization is disabled (and thus the clean-up inside `goTo()`
happens without waiting for Angular).

PR Close #28290
This commit is contained in:
George Kalpakas 2019-01-23 00:43:53 +02:00 committed by Jason Aden
parent 6ad1c47df8
commit 6e949f9e98
3 changed files with 23 additions and 8 deletions

View File

@ -10,7 +10,11 @@ describe(browser.baseUrl, () => {
beforeAll(() => page.init());
beforeEach(() => browser.waitForAngularEnabled(false));
afterEach(() => browser.waitForAngularEnabled(true));
afterEach(async () => {
await page.unregisterSw();
await browser.waitForAngularEnabled(true);
});
describe('(with sitemap URLs)', () => {
page.sitemapUrls.forEach((path, i) => {

View File

@ -42,15 +42,10 @@ export class SitePage {
* (The SW is unregistered to ensure that subsequent requests are passed through to the server.)
*/
async goTo(url: string) {
const unregisterServiceWorker = (cb: () => void) => navigator.serviceWorker
.getRegistrations()
.then(regs => Promise.all(regs.map(reg => reg.unregister())))
.then(cb);
await browser.get(url || this.baseUrl);
await browser.executeScript('document.body.classList.add(\'no-animations\')');
await browser.waitForAngular();
await browser.executeAsyncScript(unregisterServiceWorker);
await this.unregisterSw();
};
/**
@ -60,4 +55,16 @@ export class SitePage {
// Make an initial request to unregister the ServiceWorker.
await this.goTo('');
}
/**
* Unregister the ServiceWorker (if registered).
*/
async unregisterSw() {
const unregisterSwFn = (cb: () => void) => navigator.serviceWorker
.getRegistrations()
.then(regs => Promise.all(regs.map(reg => reg.unregister())))
.then(cb);
await browser.executeAsyncScript(unregisterSwFn);
}
}

View File

@ -7,7 +7,11 @@ describe(browser.baseUrl, () => {
beforeAll(() => page.init());
beforeEach(() => browser.waitForAngularEnabled(false));
afterEach(() => browser.waitForAngularEnabled(true));
afterEach(async () => {
await page.unregisterSw();
await browser.waitForAngularEnabled(true);
});
describe('(smoke tests)', () => {
it('should show the home page', () => {