From 6fe9cc601aae48a296a07f0afa1844db3a415501 Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Fri, 26 Feb 2021 11:48:27 -0800 Subject: [PATCH 1/8] Integration tests with emulator --- packages-exp/auth-exp/karma.conf.js | 19 +++++++++++++- packages-exp/auth-exp/package.json | 1 + .../test/helpers/integration/helpers.ts | 25 ++++++++----------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/packages-exp/auth-exp/karma.conf.js b/packages-exp/auth-exp/karma.conf.js index 6b96b877eea..024753e63a7 100644 --- a/packages-exp/auth-exp/karma.conf.js +++ b/packages-exp/auth-exp/karma.conf.js @@ -24,7 +24,9 @@ module.exports = function (config) { files: getTestFiles(argv), // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter - frameworks: ['mocha'] + frameworks: ['mocha'], + + client: Object.assign({}, karmaBase.client, getClientConfig(argv)), }); config.set(karmaConfig); @@ -48,4 +50,19 @@ function getTestFiles(argv) { } } +function getClientConfig(argv) { + if (!argv.local) { + return {}; + } + + return { + authAppConfig: { + apiKey: 'local-api-key', + projectId: 'hackweek-demo-mfa', + authDomain: 'local-auth-domain', + }, + authEmulatorPort: '9099', + }; +} + module.exports.files = getTestFiles(argv); diff --git a/packages-exp/auth-exp/package.json b/packages-exp/auth-exp/package.json index c0a3a0baeaa..75142bc8562 100644 --- a/packages-exp/auth-exp/package.json +++ b/packages-exp/auth-exp/package.json @@ -27,6 +27,7 @@ "test:browser": "karma start --single-run", "test:browser:unit": "karma start --single-run --unit", "test:browser:integration": "karma start --single-run --integration", + "test:browser:integration:emulator": "karma start --single-run --integration --local --auto-watch", "test:browser:debug": "karma start --auto-watch", "test:browser:unit:debug": "karma start --auto-watch --unit", "test:cordova": "karma start --single-run --cordova", diff --git a/packages-exp/auth-exp/test/helpers/integration/helpers.ts b/packages-exp/auth-exp/test/helpers/integration/helpers.ts index 508b3239384..12e78d283b6 100644 --- a/packages-exp/auth-exp/test/helpers/integration/helpers.ts +++ b/packages-exp/auth-exp/test/helpers/integration/helpers.ts @@ -18,15 +18,9 @@ import { deleteApp, initializeApp } from '@firebase/app-exp'; import { Auth, User } from '../../../src/model/public_types'; -import { getAuth } from '../../../'; // Use browser OR node dist entrypoint depending on test env. +import { initializeAuth, useAuthEmulator } from '../../../'; // Use browser OR node dist entrypoint depending on test env. import { _generateEventId } from '../../../src/core/util/event_id'; - -// eslint-disable-next-line @typescript-eslint/no-require-imports -const PROJECT_CONFIG = require('../../../../../config/project.json'); - -export const PROJECT_ID = PROJECT_CONFIG.projectId; -export const AUTH_DOMAIN = PROJECT_CONFIG.authDomain; -export const API_KEY = PROJECT_CONFIG.apiKey; +import { getAppConfig, getEmulatorUrl } from './settings'; interface IntegrationTestAuth extends Auth { cleanUp(): Promise; @@ -37,15 +31,18 @@ export function randomEmail(): string { } export function getTestInstance(): Auth { - const app = initializeApp({ - apiKey: API_KEY, - projectId: PROJECT_ID, - authDomain: AUTH_DOMAIN - }); + const app = initializeApp(getAppConfig()); const createdUsers: User[] = []; - const auth = getAuth(app) as IntegrationTestAuth; + const auth = initializeAuth(app) as IntegrationTestAuth; auth.settings.appVerificationDisabledForTesting = true; + const emulatorUrl = getEmulatorUrl(); + + console.log(emulatorUrl); + + if (emulatorUrl) { + useAuthEmulator(auth, emulatorUrl, {disableWarnings: true}); + } auth.onAuthStateChanged(user => { if (user) { From aa91df49750e67d8d45c64d229e3d4ddf7fc2130 Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Fri, 26 Feb 2021 11:48:37 -0800 Subject: [PATCH 2/8] Missing file --- .../test/helpers/integration/settings.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 packages-exp/auth-exp/test/helpers/integration/settings.ts diff --git a/packages-exp/auth-exp/test/helpers/integration/settings.ts b/packages-exp/auth-exp/test/helpers/integration/settings.ts new file mode 100644 index 00000000000..8229cad9a19 --- /dev/null +++ b/packages-exp/auth-exp/test/helpers/integration/settings.ts @@ -0,0 +1,40 @@ +import { FirebaseOptions } from '@firebase/app-exp'; + +// __karma__ is an untyped global +// eslint-disable-next-line @typescript-eslint/no-explicit-any +declare const __karma__: any; + +// eslint-disable-next-line @typescript-eslint/no-require-imports +const PROJECT_CONFIG = require('../../../../../config/project.json'); + +const EMULATOR_PORT = process.env.AUTH_EMULATOR_PORT; +const EMULATOR_PROJECT_ID = process.env.AUTH_EMULATOR_PROJECT_ID; + +export const USE_EMULATOR = !!EMULATOR_PORT; + +export const PROJECT_ID = USE_EMULATOR ? EMULATOR_PROJECT_ID : PROJECT_CONFIG.projectId; +export const AUTH_DOMAIN = USE_EMULATOR ? 'emulator-auth-domain' : PROJECT_CONFIG.authDomain; +export const API_KEY = USE_EMULATOR ? 'emulator-api-key' : PROJECT_CONFIG.apiKey; + + + +export function getAppConfig(): FirebaseOptions { + // Prefer the karma config, then fallback on node process.env stuff + return getKarma()?.config?.authAppConfig || { + apiKey: API_KEY, + projectId: PROJECT_ID, + authDomain: AUTH_DOMAIN + }; +}; + +export function getEmulatorUrl(): string | null { + // Check karma first, then fallback on node process + const emulatorPort: string | null = getKarma()?.config?.authEmulatorPort || (USE_EMULATOR ? EMULATOR_PORT : null); + + return emulatorPort ? `http://localhost:${emulatorPort}` : null; +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function getKarma(): any { + return typeof __karma__ !== 'undefined' ? __karma__ : undefined; +} \ No newline at end of file From 929a1216a3429e386c63d60f5520e4974ddcd3ae Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Mon, 1 Mar 2021 12:26:45 -0800 Subject: [PATCH 3/8] Go back to 'getAuth' --- packages-exp/auth-exp/test/helpers/integration/helpers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages-exp/auth-exp/test/helpers/integration/helpers.ts b/packages-exp/auth-exp/test/helpers/integration/helpers.ts index 12e78d283b6..ef2eaefad54 100644 --- a/packages-exp/auth-exp/test/helpers/integration/helpers.ts +++ b/packages-exp/auth-exp/test/helpers/integration/helpers.ts @@ -18,7 +18,7 @@ import { deleteApp, initializeApp } from '@firebase/app-exp'; import { Auth, User } from '../../../src/model/public_types'; -import { initializeAuth, useAuthEmulator } from '../../../'; // Use browser OR node dist entrypoint depending on test env. +import { getAuth, useAuthEmulator } from '../../../'; // Use browser OR node dist entrypoint depending on test env. import { _generateEventId } from '../../../src/core/util/event_id'; import { getAppConfig, getEmulatorUrl } from './settings'; @@ -34,7 +34,7 @@ export function getTestInstance(): Auth { const app = initializeApp(getAppConfig()); const createdUsers: User[] = []; - const auth = initializeAuth(app) as IntegrationTestAuth; + const auth = getAuth(app) as IntegrationTestAuth; auth.settings.appVerificationDisabledForTesting = true; const emulatorUrl = getEmulatorUrl(); From 64155d456f91cf445dcb3579717d71af6f321111 Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Mon, 1 Mar 2021 12:53:14 -0800 Subject: [PATCH 4/8] Add script for helping run node tests as they were getting out of hand --- packages-exp/auth-exp/package.json | 6 ++- .../auth-exp/scripts/run-node-tests.js | 1 + .../auth-exp/scripts/run-node-tests.ts | 49 +++++++++++++++++++ .../test/helpers/integration/helpers.ts | 2 - 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 packages-exp/auth-exp/scripts/run-node-tests.js create mode 100644 packages-exp/auth-exp/scripts/run-node-tests.ts diff --git a/packages-exp/auth-exp/package.json b/packages-exp/auth-exp/package.json index 75142bc8562..ba42a7e78c6 100644 --- a/packages-exp/auth-exp/package.json +++ b/packages-exp/auth-exp/package.json @@ -20,6 +20,7 @@ "build": "rollup -c && yarn api-report", "build:deps": "lerna run --scope @firebase/auth-exp --include-dependencies build", "build:release": "rollup -c rollup.config.release.js && yarn api-report && yarn typings:public", + "build:scripts": "tsc -moduleResolution node --module commonjs scripts/*.ts && ls scripts/*.js | xargs -I % sh -c 'terser % -o %'", "dev": "rollup -c -w", "test": "run-p lint test:all", "test:all": "run-p test:browser test:node", @@ -33,8 +34,9 @@ "test:cordova": "karma start --single-run --cordova", "test:cordova:debug": "karma start --auto-watch --cordova", "test:node": "run-s test:node:unit test:node:integration", - "test:node:unit": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'src/!(platform_browser|platform_react_native|platform_cordova)/**/*.test.ts' --file index.node.ts --config ../../config/mocharc.node.js", - "test:node:integration": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/integration/flows/{email,anonymous}.test.ts' --config ../../config/mocharc.node.js", + "test:node:unit": "node ./scripts/run-node-tests.js", + "test:node:integration": "node ./scripts/run-node-tests.js --integration", + "test:node:integration:local": "node ./scripts/run-node-tests.js --integration --local", "api-report": "api-extractor run --local --verbose", "predoc": "node ../../scripts/exp/remove-exp.js temp", "doc": "api-documenter markdown --input temp --output docs", diff --git a/packages-exp/auth-exp/scripts/run-node-tests.js b/packages-exp/auth-exp/scripts/run-node-tests.js new file mode 100644 index 00000000000..63901625f13 --- /dev/null +++ b/packages-exp/auth-exp/scripts/run-node-tests.js @@ -0,0 +1 @@ +"use strict";var __spreadArrays=this&&this.__spreadArrays||function(){for(var s=0,i=0,il=arguments.length;i childProcess.kill()); +process.once('SIGINT', () => childProcess.kill('SIGINT')); +process.once('SIGTERM', () => childProcess.kill('SIGTERM')); \ No newline at end of file diff --git a/packages-exp/auth-exp/test/helpers/integration/helpers.ts b/packages-exp/auth-exp/test/helpers/integration/helpers.ts index ef2eaefad54..b3bf11bb239 100644 --- a/packages-exp/auth-exp/test/helpers/integration/helpers.ts +++ b/packages-exp/auth-exp/test/helpers/integration/helpers.ts @@ -38,8 +38,6 @@ export function getTestInstance(): Auth { auth.settings.appVerificationDisabledForTesting = true; const emulatorUrl = getEmulatorUrl(); - console.log(emulatorUrl); - if (emulatorUrl) { useAuthEmulator(auth, emulatorUrl, {disableWarnings: true}); } From 5bf086a3b55af49b2b5adfd3ab2c88b3ff4dbded Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Mon, 1 Mar 2021 13:18:19 -0800 Subject: [PATCH 5/8] Fix lint issues --- packages-exp/auth-exp/.eslintrc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages-exp/auth-exp/.eslintrc.js b/packages-exp/auth-exp/.eslintrc.js index 674937d0c30..6cfe90711fc 100644 --- a/packages-exp/auth-exp/.eslintrc.js +++ b/packages-exp/auth-exp/.eslintrc.js @@ -17,7 +17,7 @@ module.exports = { extends: '../../config/.eslintrc.js', - ignorePatterns: ['demo/'], + ignorePatterns: ['demo/', 'scripts/'], parserOptions: { project: 'tsconfig.json', // to make vscode-eslint work with monorepo From 969949ef12b66f15a408272b8785eca9e7d2a635 Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Mon, 1 Mar 2021 13:22:18 -0800 Subject: [PATCH 6/8] Fix issue --- packages-exp/auth-exp/karma.conf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages-exp/auth-exp/karma.conf.js b/packages-exp/auth-exp/karma.conf.js index 024753e63a7..c279edb3a1f 100644 --- a/packages-exp/auth-exp/karma.conf.js +++ b/packages-exp/auth-exp/karma.conf.js @@ -58,7 +58,7 @@ function getClientConfig(argv) { return { authAppConfig: { apiKey: 'local-api-key', - projectId: 'hackweek-demo-mfa', + projectId: 'test-emulator', authDomain: 'local-auth-domain', }, authEmulatorPort: '9099', From 45822ce115d14beafde77dd8e15e84cc4a9db5ab Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Mon, 1 Mar 2021 13:22:34 -0800 Subject: [PATCH 7/8] Formatting, license --- packages-exp/auth-exp/karma.conf.js | 6 +- .../auth-exp/scripts/run-node-tests.js | 70 ++++++++++++++++++- .../auth-exp/scripts/run-node-tests.ts | 25 ++++++- .../test/helpers/integration/helpers.ts | 2 +- .../test/helpers/integration/settings.ts | 51 ++++++++++---- 5 files changed, 134 insertions(+), 20 deletions(-) diff --git a/packages-exp/auth-exp/karma.conf.js b/packages-exp/auth-exp/karma.conf.js index c279edb3a1f..3b6355af507 100644 --- a/packages-exp/auth-exp/karma.conf.js +++ b/packages-exp/auth-exp/karma.conf.js @@ -26,7 +26,7 @@ module.exports = function (config) { // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['mocha'], - client: Object.assign({}, karmaBase.client, getClientConfig(argv)), + client: Object.assign({}, karmaBase.client, getClientConfig(argv)) }); config.set(karmaConfig); @@ -59,9 +59,9 @@ function getClientConfig(argv) { authAppConfig: { apiKey: 'local-api-key', projectId: 'test-emulator', - authDomain: 'local-auth-domain', + authDomain: 'local-auth-domain' }, - authEmulatorPort: '9099', + authEmulatorPort: '9099' }; } diff --git a/packages-exp/auth-exp/scripts/run-node-tests.js b/packages-exp/auth-exp/scripts/run-node-tests.js index 63901625f13..c83ddee1a20 100644 --- a/packages-exp/auth-exp/scripts/run-node-tests.js +++ b/packages-exp/auth-exp/scripts/run-node-tests.js @@ -1 +1,69 @@ -"use strict";var __spreadArrays=this&&this.__spreadArrays||function(){for(var s=0,i=0,il=arguments.length;i childProcess.kill()); process.once('SIGINT', () => childProcess.kill('SIGINT')); -process.once('SIGTERM', () => childProcess.kill('SIGTERM')); \ No newline at end of file +process.once('SIGTERM', () => childProcess.kill('SIGTERM')); diff --git a/packages-exp/auth-exp/test/helpers/integration/helpers.ts b/packages-exp/auth-exp/test/helpers/integration/helpers.ts index b3bf11bb239..831179c58b5 100644 --- a/packages-exp/auth-exp/test/helpers/integration/helpers.ts +++ b/packages-exp/auth-exp/test/helpers/integration/helpers.ts @@ -39,7 +39,7 @@ export function getTestInstance(): Auth { const emulatorUrl = getEmulatorUrl(); if (emulatorUrl) { - useAuthEmulator(auth, emulatorUrl, {disableWarnings: true}); + useAuthEmulator(auth, emulatorUrl, { disableWarnings: true }); } auth.onAuthStateChanged(user => { diff --git a/packages-exp/auth-exp/test/helpers/integration/settings.ts b/packages-exp/auth-exp/test/helpers/integration/settings.ts index 8229cad9a19..f42421e534d 100644 --- a/packages-exp/auth-exp/test/helpers/integration/settings.ts +++ b/packages-exp/auth-exp/test/helpers/integration/settings.ts @@ -1,3 +1,20 @@ +/** + * @license + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + import { FirebaseOptions } from '@firebase/app-exp'; // __karma__ is an untyped global @@ -12,24 +29,32 @@ const EMULATOR_PROJECT_ID = process.env.AUTH_EMULATOR_PROJECT_ID; export const USE_EMULATOR = !!EMULATOR_PORT; -export const PROJECT_ID = USE_EMULATOR ? EMULATOR_PROJECT_ID : PROJECT_CONFIG.projectId; -export const AUTH_DOMAIN = USE_EMULATOR ? 'emulator-auth-domain' : PROJECT_CONFIG.authDomain; -export const API_KEY = USE_EMULATOR ? 'emulator-api-key' : PROJECT_CONFIG.apiKey; - - +export const PROJECT_ID = USE_EMULATOR + ? EMULATOR_PROJECT_ID + : PROJECT_CONFIG.projectId; +export const AUTH_DOMAIN = USE_EMULATOR + ? 'emulator-auth-domain' + : PROJECT_CONFIG.authDomain; +export const API_KEY = USE_EMULATOR + ? 'emulator-api-key' + : PROJECT_CONFIG.apiKey; export function getAppConfig(): FirebaseOptions { // Prefer the karma config, then fallback on node process.env stuff - return getKarma()?.config?.authAppConfig || { - apiKey: API_KEY, - projectId: PROJECT_ID, - authDomain: AUTH_DOMAIN - }; -}; + return ( + getKarma()?.config?.authAppConfig || { + apiKey: API_KEY, + projectId: PROJECT_ID, + authDomain: AUTH_DOMAIN + } + ); +} export function getEmulatorUrl(): string | null { // Check karma first, then fallback on node process - const emulatorPort: string | null = getKarma()?.config?.authEmulatorPort || (USE_EMULATOR ? EMULATOR_PORT : null); + const emulatorPort: string | null = + getKarma()?.config?.authEmulatorPort || + (USE_EMULATOR ? EMULATOR_PORT : null); return emulatorPort ? `http://localhost:${emulatorPort}` : null; } @@ -37,4 +62,4 @@ export function getEmulatorUrl(): string | null { // eslint-disable-next-line @typescript-eslint/no-explicit-any function getKarma(): any { return typeof __karma__ !== 'undefined' ? __karma__ : undefined; -} \ No newline at end of file +} From 653b9d02ec4700f991b25ee89292febc37f08a74 Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Mon, 1 Mar 2021 13:23:38 -0800 Subject: [PATCH 8/8] Fix command --- packages-exp/auth-exp/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages-exp/auth-exp/package.json b/packages-exp/auth-exp/package.json index ba42a7e78c6..0248a8f4e84 100644 --- a/packages-exp/auth-exp/package.json +++ b/packages-exp/auth-exp/package.json @@ -28,7 +28,7 @@ "test:browser": "karma start --single-run", "test:browser:unit": "karma start --single-run --unit", "test:browser:integration": "karma start --single-run --integration", - "test:browser:integration:emulator": "karma start --single-run --integration --local --auto-watch", + "test:browser:integration:local": "karma start --single-run --integration --local --auto-watch", "test:browser:debug": "karma start --auto-watch", "test:browser:unit:debug": "karma start --auto-watch --unit", "test:cordova": "karma start --single-run --cordova",