Skip to content

Commit

Permalink
Unregister service worker after each test
Browse files Browse the repository at this point in the history
Some service-workers seems to be left over between test runs.  This
causes the SDK to clear its (valid) internal state which causes tests to
fail. Unregister the service worker after each test to ensure each test
starts fresh
  • Loading branch information
James Lees committed Jan 25, 2021
1 parent c539258 commit 9d51a8c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
12 changes: 10 additions & 2 deletions end-to-end-tests/sdk-get-registration-state.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { launchServer, createChromeWebDriver, NOTIFICATIONS_DEFAULT, NOTIFICATIONS_GRANTED, NOTIFICATIONS_DENIED } from './test-utils';
import {
launchServer,
createChromeWebDriver,
NOTIFICATIONS_DEFAULT,
NOTIFICATIONS_GRANTED,
NOTIFICATIONS_DENIED,
unregisterServiceWorker
} from './test-utils';
import * as PusherPushNotifications from '../src/push-notifications';

let killServer = null;
Expand Down Expand Up @@ -87,10 +94,11 @@ test('.getState should return PERMISSION_DENIED if start has not been called and

afterEach(async () => {
if (chromeDriver) {
await unregisterServiceWorker(chromeDriver)
await chromeDriver.quit();
}
})

afterAll(() => {
if (killServer) {
killServer();
Expand Down
10 changes: 9 additions & 1 deletion end-to-end-tests/sdk-set-user-id.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ErrolTestClient } from './errol-client';
import { launchServer, createChromeWebDriver, NOTIFICATIONS_GRANTED } from './test-utils';
import {
launchServer,
createChromeWebDriver,
NOTIFICATIONS_GRANTED,
unregisterServiceWorker
} from './test-utils';

let killServer = null;
let chromeDriver = null;
Expand Down Expand Up @@ -38,9 +43,12 @@ beforeEach(() => {
deleteDbRequest.onsuccess = asyncScriptReturnCallback;
deleteDbRequest.onerror = asyncScriptReturnCallback;
});

})();
});

afterEach(() => unregisterServiceWorker(chromeDriver));

test('SDK should set user id with errol', async () => {
await chromeDriver.get('https://localhost:3000');
await chromeDriver.wait(() => {
Expand Down
9 changes: 8 additions & 1 deletion end-to-end-tests/sdk-start.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { launchServer, createChromeWebDriver, NOTIFICATIONS_GRANTED } from './test-utils';
import {
launchServer,
createChromeWebDriver,
NOTIFICATIONS_GRANTED,
unregisterServiceWorker
} from './test-utils';

let killServer = null;
let chromeDriver = null;
Expand All @@ -14,6 +19,8 @@ beforeAll(() => {
});
});

afterEach(() => unregisterServiceWorker(chromeDriver));

test('SDK should register a device with errol', async () => {
await chromeDriver.get('https://localhost:3000');
await chromeDriver.wait(() => {
Expand Down
9 changes: 8 additions & 1 deletion end-to-end-tests/sdk-stop.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ErrolTestClient } from './errol-client';
import { launchServer, createChromeWebDriver, NOTIFICATIONS_GRANTED } from './test-utils';
import {
launchServer,
createChromeWebDriver,
NOTIFICATIONS_GRANTED,
unregisterServiceWorker
} from './test-utils';

let killServer = null;
let chromeDriver = null;
Expand Down Expand Up @@ -34,6 +39,8 @@ beforeEach(() => {
})();
});

afterEach(() => unregisterServiceWorker(chromeDriver));

test('Calling .stop should clear SDK state', async () => {
const errolClient = new ErrolTestClient(
'1b880590-6301-4bb5-b34f-45db1c5f5644'
Expand Down
12 changes: 12 additions & 0 deletions end-to-end-tests/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ export async function createChromeWebDriver(notificationPermission = NOTIFICATIO
return driver;
}

export async function unregisterServiceWorker(chromeDriver) {
return chromeDriver.executeAsyncScript(async () => {
const asyncScriptReturnCallback = arguments[arguments.length - 1];
let serviceWorkerRegistration = await window.navigator.serviceWorker.getRegistration()
if (serviceWorkerRegistration) {
await serviceWorkerRegistration.unregister()
}
asyncScriptReturnCallback()
})
}

function createTempBrowserPreferences(testSiteURL, notificationPermission) {
let notifications = {}
if (notificationPermission !== NOTIFICATIONS_DEFAULT) {
Expand Down Expand Up @@ -195,3 +206,4 @@ function rimraf(dir_path) {
fs.rmdirSync(dir_path);
}
}

0 comments on commit 9d51a8c

Please sign in to comment.