Skip to content

Commit c4d2b08

Browse files
sam-gcavolkovi
authored andcommitted
Foundation for integration tests for auth-exp (#3453)
* Add integration test foundation * Formatting * Update to use a describe wrapper instead * Formatting * PR feedback * Removed custom describe * Formatting
1 parent bb855ea commit c4d2b08

File tree

4 files changed

+138
-6
lines changed

4 files changed

+138
-6
lines changed

packages-exp/auth-exp/karma.conf.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
*/
1717

1818
const karmaBase = require('../../config/karma.base');
19-
20-
const files = ['src/**/*.test.ts', 'test/**/*.test.ts'];
19+
const { argv } = require('yargs');
2120

2221
module.exports = function(config) {
2322
const karmaConfig = Object.assign({}, karmaBase, {
2423
// files to load into karma
25-
files: files,
24+
files: getTestFiles(argv),
2625
preprocessors: { '**/*.ts': ['webpack', 'sourcemap'] },
2726
// frameworks to use
2827
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
@@ -32,4 +31,14 @@ module.exports = function(config) {
3231
config.set(karmaConfig);
3332
};
3433

35-
module.exports.files = files;
34+
function getTestFiles(argv) {
35+
if (argv.unit) {
36+
return ['src/**/*.test.ts', 'test/helpers/**/*.test.ts'];
37+
} else if (argv.integration) {
38+
return ['test/integration/**/*.test.ts'];
39+
} else {
40+
return ['src/**/*.test.ts', 'test/**/*.test.ts'];
41+
}
42+
}
43+
44+
module.exports.files = getTestFiles(argv);

packages-exp/auth-exp/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"dev": "rollup -c -w",
2222
"test": "yarn type-check && run-p lint test:browser",
2323
"test:browser": "karma start --single-run",
24-
"test:browser:debug": "karma start",
24+
"test:browser:unit": "karma start --single-run --unit",
25+
"test:browser:integration": "karma start --single-run --integration",
26+
"test:browser:debug": "karma start --auto-watch",
2527
"test:node": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha src/**/*.test.* --opts ../../config/mocha.node.opts",
2628
"type-check": "tsc -p . --noEmit",
2729
"prepare": "yarn build",
@@ -40,8 +42,8 @@
4042
},
4143
"license": "Apache-2.0",
4244
"devDependencies": {
43-
"@rollup/plugin-strip": "^1.3.2",
4445
"@firebase/app-exp": "0.x",
46+
"@rollup/plugin-strip": "^1.3.2",
4547
"rollup": "1.32.1",
4648
"rollup-plugin-json": "4.0.0",
4749
"rollup-plugin-replace": "2.2.0",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { deleteApp, initializeApp } from '@firebase/app-exp';
19+
import { initializeAuth } from '@firebase/auth-exp/index.browser';
20+
import { Auth, User } from '@firebase/auth-types-exp';
21+
22+
import { _generateEventId } from '../../../src/core/util/event_id';
23+
24+
// eslint-disable-next-line @typescript-eslint/no-require-imports
25+
const PROJECT_CONFIG = require('../../../../../config/project.json');
26+
27+
export const PROJECT_ID = PROJECT_CONFIG.projectId;
28+
export const AUTH_DOMAIN = PROJECT_CONFIG.authDomain;
29+
export const API_KEY = PROJECT_CONFIG.apiKey;
30+
31+
interface IntegrationTestAuth extends Auth {
32+
cleanUp(): Promise<void>;
33+
}
34+
35+
export function randomEmail(): string {
36+
return `${_generateEventId('test.email.')}@test.com`;
37+
}
38+
39+
export function getTestInstance(): Auth {
40+
const app = initializeApp({
41+
apiKey: API_KEY,
42+
projectId: PROJECT_ID,
43+
authDomain: AUTH_DOMAIN
44+
});
45+
46+
const createdUsers: User[] = [];
47+
const auth = initializeAuth(app) as IntegrationTestAuth;
48+
auth.settings.appVerificationDisabledForTesting = true;
49+
50+
auth.onAuthStateChanged(user => {
51+
if (user) {
52+
createdUsers.push(user);
53+
}
54+
});
55+
56+
auth.cleanUp = async () => {
57+
// Clear out any new users that were created in the course of the test
58+
for (const user of createdUsers) {
59+
try {
60+
await user.delete();
61+
} catch {
62+
// Best effort. Maybe the test already deleted the user ¯\_(ツ)_/¯
63+
}
64+
}
65+
66+
await deleteApp(app);
67+
};
68+
69+
return auth;
70+
}
71+
72+
export async function cleanUpTestInstance(auth: Auth): Promise<void> {
73+
await (auth as IntegrationTestAuth).cleanUp();
74+
}
75+
76+
export function initIntegrationTestContext(): Auth {
77+
const auth = getTestInstance();
78+
79+
afterEach(async () => {
80+
await auth.signOut();
81+
});
82+
83+
after(async () => {
84+
await cleanUpTestInstance(auth);
85+
});
86+
87+
return auth;
88+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect } from 'chai';
19+
20+
import { signInAnonymously } from '@firebase/auth-exp/index.browser';
21+
import { OperationType } from '@firebase/auth-types-exp';
22+
23+
import { initIntegrationTestContext } from '../helpers/integration/helpers';
24+
25+
describe('smoke test', () => {
26+
const auth = initIntegrationTestContext();
27+
28+
it('signs in anonymously', async () => {
29+
const userCred = await signInAnonymously(auth);
30+
expect(auth.currentUser).to.eq(userCred.user);
31+
expect(userCred.operationType).to.eq(OperationType.SIGN_IN);
32+
});
33+
});

0 commit comments

Comments
 (0)