Skip to content

Commit b6ff847

Browse files
committed
Clean up the test infra a bit
1 parent a333b74 commit b6ff847

File tree

7 files changed

+109
-125
lines changed

7 files changed

+109
-125
lines changed

packages-exp/auth-exp/scripts/run-node-tests.js

Lines changed: 2 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages-exp/auth-exp/scripts/run-node-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ if (argv.integration) {
4949
testConfig.push('test/integration/flows/*.local.test.ts');
5050
}
5151
} else if (argv.webdriver) {
52-
testConfig = ['test/integration/webdriver/**.test.ts'];
52+
testConfig = ['test/integration/webdriver/**.test.ts', '--delay'];
5353
}
5454

5555
let args = [

packages-exp/auth-exp/test/integration/webdriver/anonymous.test.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,38 @@
1818
// eslint-disable-next-line import/no-extraneous-dependencies
1919
import { OperationType, UserCredential } from '@firebase/auth-exp';
2020
import { expect } from 'chai';
21-
import { TestFunction, AuthDriver } from './util/authdriver';
21+
import { TestFunction } from './util/auth_driver';
22+
import { browserDescribe } from './util/test_runner';
2223

2324
/**
2425
* Simple smoke test to demonstrate webdriver testing and serve as a template
2526
* for future tests; anonymous is largely covered by the headless tests in
2627
* test/integration/flows/anonymous.test.ts
2728
*/
28-
describe('WebDriver anonymous auth test', () => {
29-
const driver = new AuthDriver();
30-
31-
driver.runTestsInAvailableBrowsers(() => {
32-
it('basic sign in is possible', async () => {
33-
const cred: UserCredential = await driver.call(
34-
TestFunction.SIGN_IN_ANONYMOUSLY
35-
);
36-
expect(cred).not.to.be.null;
37-
expect(cred.user.isAnonymous).to.be.true;
38-
expect(cred.operationType).to.eq(OperationType.SIGN_IN);
39-
expect(await driver.getUserSnapshot()).to.eql(cred.user);
40-
});
29+
browserDescribe('WebDriver anonymous auth test', driver => {
30+
it('basic sign in is possible', async () => {
31+
const cred: UserCredential = await driver.call(
32+
TestFunction.SIGN_IN_ANONYMOUSLY
33+
);
34+
expect(cred).not.to.be.null;
35+
expect(cred.user.isAnonymous).to.be.true;
36+
expect(cred.operationType).to.eq(OperationType.SIGN_IN);
37+
expect(await driver.getUserSnapshot()).to.eql(cred.user);
38+
});
4139

42-
it('same user persists after refresh and sign in', async () => {
43-
const { user: before }: UserCredential = await driver.call(
44-
TestFunction.SIGN_IN_ANONYMOUSLY
45-
);
46-
await driver.refresh();
40+
it('same user persists after refresh and sign in', async () => {
41+
const { user: before }: UserCredential = await driver.call(
42+
TestFunction.SIGN_IN_ANONYMOUSLY
43+
);
44+
await driver.refresh();
4745

48-
// First, is the user signed in from persistence?
49-
expect(await driver.getUserSnapshot()).to.eql(before);
46+
// First, is the user signed in from persistence?
47+
expect(await driver.getUserSnapshot()).to.eql(before);
5048

51-
// Then, sign in again and check
52-
const { user: after }: UserCredential = await driver.call(
53-
TestFunction.SIGN_IN_ANONYMOUSLY
54-
);
55-
expect(after.uid).to.eq(before.uid);
56-
});
49+
// Then, sign in again and check
50+
const { user: after }: UserCredential = await driver.call(
51+
TestFunction.SIGN_IN_ANONYMOUSLY
52+
);
53+
expect(after.uid).to.eq(before.uid);
5754
});
5855
});

packages-exp/auth-exp/test/integration/webdriver/static/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@ window.anonymous = async () => {
3030
return userCred;
3131
};
3232

33-
window.reset = () => auth.signOut();
33+
window.reset = () => {
34+
sessionStorage.clear();
35+
localStorage.clear();
36+
const del = indexedDB.deleteDatabase('firebaseLocalStorageDb');
37+
38+
return new Promise(resolve => {
39+
del.addEventListener('success', () => resolve());
40+
del.addEventListener('error', () => resolve());
41+
del.addEventListener('blocked', () => resolve());
42+
});
43+
}
3444

3545
window.authInit = () => {
3646
return new Promise(resolve => {

packages-exp/auth-exp/test/integration/webdriver/util/authdriver.ts renamed to packages-exp/auth-exp/test/integration/webdriver/util/auth_driver.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ import {
2626
} from '../../../helpers/integration/settings';
2727
import { authTestServer } from './test_server';
2828

29-
/** Browsers to run the tests on */
30-
const BROWSERS = ['chrome', 'firefox'];
31-
3229
/** Available functions within the browser. See static/index.js */
3330
export enum TestFunction {
3431
SIGN_IN_ANONYMOUSLY = 'anonymous',
@@ -55,27 +52,6 @@ export class AuthDriver {
5552
await this.webDriver.quit();
5653
}
5754

58-
/** Wraps a set of tests and runs them in multiple browsers */
59-
runTestsInAvailableBrowsers(cb: () => void): void {
60-
for (const browser of BROWSERS) {
61-
context(`Under browser: ${browser}`, () => {
62-
before(async () => {
63-
await this.start(browser);
64-
});
65-
66-
after(async () => {
67-
await this.stop();
68-
});
69-
70-
afterEach(async () => {
71-
await this.reset();
72-
});
73-
74-
cb();
75-
});
76-
}
77-
}
78-
7955
async call<T>(fn: TestFunction): Promise<T> {
8056
// When running on firefox we can't just return result immediately. For
8157
// some reason, the binding ends up causing a cycle dependency issue during
@@ -101,6 +77,7 @@ export class AuthDriver {
10177

10278
async reset(): Promise<void> {
10379
await resetEmulator();
80+
await this.webDriver.get(authTestServer.address!);
10481
return this.call(TestFunction.RESET);
10582
}
10683

@@ -114,7 +91,7 @@ export class AuthDriver {
11491
await this.waitForAuthInit();
11592
}
11693

117-
private async injectConfigAndInitAuth(): Promise<void> {
94+
async injectConfigAndInitAuth(): Promise<void> {
11895
if (!USE_EMULATOR) {
11996
throw new Error(
12097
'Local testing against emulator requested, but ' +
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { AuthDriver } from './auth_driver';
2+
3+
/*
4+
* The most expensive operation in these tests is setting up / tearing down the
5+
* driver. In order to avoid that cost, all of the tests are collected and
6+
* bundled into single suites for each browser. To do this, we create a new
7+
* describe function that is used to generate the new suites.
8+
*
9+
* This test is started with the --delay flag, which allows us to control when
10+
* test execution starts. Collection of the tests is synchronous, but we need
11+
* a way to ensure that run() is called after they're all added. To accomplish
12+
* this, we put the final construction of the suites (and the subsequent run()
13+
* call) after a delay of 1ms.
14+
*/
15+
16+
interface TempSuite {
17+
generator: (driver: AuthDriver) => void;
18+
title: string;
19+
}
20+
21+
/** The browsers that these tests will run in */
22+
const BROWSERS = ['chrome', 'firefox'];
23+
24+
/** One single AuthDriver instance to control everything */
25+
const DRIVER = new AuthDriver();
26+
const SUITES: TempSuite[] = [];
27+
28+
/** Main entry point for all WebDriver tests */
29+
export function browserDescribe(title: string, generator:(driver: AuthDriver) => void): void {
30+
SUITES.push({
31+
title, generator
32+
});
33+
}
34+
35+
// Construct the final suites after a delay of 1ms, then kick off tests
36+
setTimeout(() => {
37+
for (const browser of BROWSERS) {
38+
describe(`Testing in browser "${browser}"`, () => {
39+
before(async () => {
40+
await DRIVER.start(browser);
41+
42+
// Prime for the first test.
43+
await DRIVER.reset();
44+
});
45+
46+
after(async () => {
47+
await DRIVER.stop();
48+
});
49+
50+
// It's assumed that the tests will start with a clean slate (i.e.
51+
// no storage).
52+
beforeEach(async () => {
53+
await DRIVER.injectConfigAndInitAuth();
54+
});
55+
56+
afterEach(async () => {
57+
await DRIVER.reset();
58+
});
59+
60+
for (const {title, generator} of SUITES) {
61+
describe(title, () => generator(DRIVER));
62+
}
63+
});
64+
}
65+
66+
run();
67+
}, 1);

packages-exp/auth-exp/test/integration/webdriver/util/test_server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class AuthTestServer {
3838
this.app.use([INTEGRATION_TEST_ASSETS]);
3939
}
4040

41-
get address(): string | null {
42-
return this.server ? `http://localhost:${PORT_NUMBER}` : null;
41+
get address(): string {
42+
return `http://localhost:${PORT_NUMBER}`;
4343
}
4444

4545
async start(): Promise<void> {

0 commit comments

Comments
 (0)