Skip to content

Commit bc3f6e0

Browse files
authored
v0.0.8 (#4)
* feat(core): add support for `test/ui` fallback folder * feat(docs): add note about fallback folders for config (`test/e2e` and `test/ui`)
1 parent 864904e commit bc3f6e0

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
> Utilities and cli to help testing Firebase projects with Cypress
99
1010
## What?
11-
* Test environment config generation (including custom auth token) with [`createTestEnvFile`](#createTestEnvFile):
12-
13-
`cypress-firebase createTestEnvFile`
11+
12+
* Test environment config generation (including custom auth token) with [`createTestEnvFile`](#createTestEnvFile)
1413
* [Custom cypress commands](https://docs.cypress.io/api/cypress-api/custom-commands.html#Syntax) for auth and database interactions:
15-
* [`cy.login`][1]
14+
* [cy.login][1]
1615
* [cy.logout][4]
1716
* [cy.callRtdb][6]
1817
* [cy.callFirestore][9]
@@ -33,6 +32,8 @@ If you are intereted in what drove the need for this checkout [the why section](
3332

3433
### Setup
3534

35+
**Note:** These instructions assume your tests are in the `cypress` folder (cypress' default). See the [folders section below](#folders) for more info about other supported folders.
36+
3637
1. Make sure you have `firebase-tools` installed (globally or within project). It is used to call to database when using `cy.callRtdb` and `cy.callFirestore`.
3738
1. Install using `npm i cypress-firebase --save-dev`
3839
1. Add the following to the `scripts` section of your `package.json`:
@@ -53,6 +54,7 @@ If you are intereted in what drove the need for this checkout [the why section](
5354
"FIREBASE_PROJECT_ID": "<- projectId of your project ->"
5455
}
5556
```
57+
5658
1. Add the following your custom commands file (`cypress/support/commands.js`):
5759

5860
```js
@@ -91,11 +93,6 @@ If you are intereted in what drove the need for this checkout [the why section](
9193
1. Start your local dev server (usually `npm start`) - for faster alternative checkout the [test built version section](#test-built-version)
9294
1. Open cypress test running by running `npm run test:open` in another terminal window
9395

94-
### CI
95-
96-
1. Run `firebase login:ci` to generate a CI token for `firebase-tools` (this will give your `cy.callRtdb` and `cy.callFirestore` commands admin access to the DB)
97-
1. Set `FIREBASE_TOKEN` within CI environment variables
98-
9996
#### Test Built Version
10097

10198
Tests will run faster locally if you tests against the build version of your app instead of your dev version (with hot module reloading and other dev tools). You can do that by:
@@ -108,11 +105,26 @@ Tests will run faster locally if you tests against the build version of your app
108105
1. Run `npm run start:dist` to build your app and serve it with firebase
109106
1. In another terminal window, run a test command such as `npm run test:open`
110107

108+
### CI
109+
110+
1. Run `firebase login:ci` to generate a CI token for `firebase-tools` (this will give your `cy.callRtdb` and `cy.callFirestore` commands admin access to the DB)
111+
1. Set `FIREBASE_TOKEN` within CI environment variables
112+
113+
### Folders
114+
115+
`cypress` is the default folder where config is loaded from, but these other common folders are supported:
116+
117+
```
118+
test/ui
119+
test/e2e
120+
```
121+
111122
## Docs
112123
113124
### CLI Commands
114125
115126
#### createTestEnvFile {#createTestEnvFile}
127+
116128
Create test environment file (`cypress.env.json`) which contains custom auth token generated using `firebase-admin` SDK and `serviceAccount.json`.
117129
118130
##### Examples
@@ -244,6 +256,8 @@ cy.callFirestore('delete', 'project/test-project', opts)
244256

245257
It isn't currenlty possible to use Firebase's `firebase-admin` SDK directly within Cypress due to dependencies not being able to be loaded into the Browser environment. Since `firebase-admin` is nessesary to generate custom token needed to login to Firebase, the usage of it happens outside of Cypress (through `cypress-firebase createTestEnvFile`) before booting up.
246258

259+
Instead of a cli tool, the plugin that is include could maybe use `firebase-admin` (since cypress plugins is a node environment) - when investigating this, I found it frustrating to get the values back into the test. That said, always open to better ways of solving this, so please reach out with your ideas!
260+
247261
## Projects Using It
248262

249263
[fireadmin.io][fireadmin-url] - A Firebase project management tool ([here is the source][fireadmin-source])

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cypress-firebase",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "Utilities to help testing firebase projects with cypress.",
55
"main": "lib/index.js",
66
"module": "lib/index.js",

src/constants.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
export const DEFAULT_TEST_FOLDER_PATH = 'test/e2e';
2-
export const FALLBACK_TEST_FOLDER_PATH = 'cypress';
1+
export const DEFAULT_TEST_FOLDER_PATH = 'cypress';
2+
export const FALLBACK_TEST_FOLDER_PATH = 'test/ui';
3+
export const SECOND_FALLBACK_TEST_FOLDER_PATH = 'test/e2e';
34
export const DEFAULT_SERVICE_ACCOUNT_PATH = 'serviceAccount.json';
45
export const DEFAULT_TEST_ENV_FILE_NAME = 'cypress.env.json';
56
export const DEFAULT_CONFIG_FILE_NAME = 'config.json';
67
export const FIREBASE_TOOLS_BASE_COMMAND = 'npx firebase';
7-
// Path to Firebase Extra Command Line tool (wrapper for firebase-tools)
8+
// Path to firebase-tools-extra Command Line tool (wrapper for firebase-tools)
89
export const FIREBASE_EXTRA_PATH = '$(npm bin)/firebase-extra';
910
export const FIREBASE_TOOLS_YES_ARGUMENT = '-y';
1011
export const DEFAULT_BASE_PATH = process.cwd();

src/utils.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
DEFAULT_BASE_PATH,
66
DEFAULT_TEST_FOLDER_PATH,
77
FIREBASE_TOOLS_YES_ARGUMENT,
8-
FALLBACK_TEST_FOLDER_PATH
8+
FALLBACK_TEST_FOLDER_PATH,
9+
SECOND_FALLBACK_TEST_FOLDER_PATH
910
} from './constants';
1011

1112
/**
@@ -74,27 +75,45 @@ export function envVarBasedOnCIEnv(varNameRoot) {
7475
const prefix = getEnvPrefix();
7576
const combined = `${prefix}${varNameRoot}`;
7677

77-
// Config file used for environment (local, containers)
78+
// Config file used for environment (local, containers) from main test path (cypress/config.json)
7879
const localTestConfigPath = path.join(
79-
process.cwd(),
80+
DEFAULT_BASE_PATH,
8081
DEFAULT_TEST_FOLDER_PATH,
8182
'config.json'
8283
);
84+
85+
// Load test config from main test path (cypress/config.json)
8386
if (fs.existsSync(localTestConfigPath)) {
8487
const configObj = require(localTestConfigPath); // eslint-disable-line global-require, import/no-dynamic-require
8588
return configObj[combined] || configObj[varNameRoot];
8689
}
90+
91+
// Fallback Attempt for Config file (test/ui)
8792
const fallbackConfigPath = path.join(
88-
process.cwd(),
93+
DEFAULT_BASE_PATH,
8994
FALLBACK_TEST_FOLDER_PATH,
9095
'config.json'
9196
);
9297

98+
// Load config from the fallback path if it exists (test/ui)
9399
if (fs.existsSync(fallbackConfigPath)) {
94100
const configObj = require(fallbackConfigPath); // eslint-disable-line global-require, import/no-dynamic-require
95101
return configObj[combined] || configObj[varNameRoot];
96102
}
97103

104+
// Second Fallback Attempt for Config file (test/e2e)
105+
const fallback2ConfigPath = path.join(
106+
DEFAULT_BASE_PATH,
107+
SECOND_FALLBACK_TEST_FOLDER_PATH,
108+
'config.json'
109+
);
110+
111+
// Load config from the fallback path if it exists (test/e2e)
112+
if (fs.existsSync(fallback2ConfigPath)) {
113+
const configObj = require(fallback2ConfigPath); // eslint-disable-line global-require, import/no-dynamic-require
114+
return configObj[combined] || configObj[varNameRoot];
115+
}
116+
98117
// CI Environment (environment variables loaded directly)
99118
return process.env[combined] || process.env[varNameRoot];
100119
}
@@ -217,7 +236,9 @@ export function addDefaultArgs(Cypress, args, opts = {}) {
217236
// TODO: Load this in a way that understands environment. Currently this will
218237
// go to the first project id that is defined, not which one should be used
219238
// for the specified environment
220-
const projectId = Cypress.env('firebaseProjectId') || Cypress.env('FIREBASE_PROJECT_ID') || Cypress.env('STAGE_FIREBASE_PROJECT_ID');
239+
const projectId = Cypress.env('firebaseProjectId')
240+
|| Cypress.env('FIREBASE_PROJECT_ID')
241+
|| Cypress.env('STAGE_FIREBASE_PROJECT_ID');
221242
// Include project id command so command runs on the current project
222243
if (!newArgs.includes('-P') || !newArgs.includes(projectId)) {
223244
newArgs.push('-P');

0 commit comments

Comments
 (0)