From 6e949f9e988e77bb5291531b742c95f6554477ec Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Wed, 23 Jan 2019 00:43:53 +0200 Subject: [PATCH] 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 --- .../deployment/e2e/redirection.e2e-spec.ts | 6 +++++- aio/tests/deployment/e2e/site.po.ts | 19 +++++++++++++------ .../deployment/e2e/smoke-tests.e2e-spec.ts | 6 +++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/aio/tests/deployment/e2e/redirection.e2e-spec.ts b/aio/tests/deployment/e2e/redirection.e2e-spec.ts index 0a71249f54..559cfb2d98 100644 --- a/aio/tests/deployment/e2e/redirection.e2e-spec.ts +++ b/aio/tests/deployment/e2e/redirection.e2e-spec.ts @@ -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) => { diff --git a/aio/tests/deployment/e2e/site.po.ts b/aio/tests/deployment/e2e/site.po.ts index d04026e965..4fba85022e 100644 --- a/aio/tests/deployment/e2e/site.po.ts +++ b/aio/tests/deployment/e2e/site.po.ts @@ -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); + } } diff --git a/aio/tests/deployment/e2e/smoke-tests.e2e-spec.ts b/aio/tests/deployment/e2e/smoke-tests.e2e-spec.ts index 01efe85636..2da958f390 100644 --- a/aio/tests/deployment/e2e/smoke-tests.e2e-spec.ts +++ b/aio/tests/deployment/e2e/smoke-tests.e2e-spec.ts @@ -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', () => {