cypress-firebase
Cypress plugin and custom commands for testing Firebase projects
What?
0 dependency plugin which adds custom cypress commands for interactions with Firebase:
If you are interested in what drove the need for this checkout the why section
Usage
Pre-Setup
- If you do not already have it installed, install Cypress and firebase-admin and add them to your package file:
npm i --save-dev cypress firebase-admin
or yarn add -D cypress firebase-admin
- Make sure you have a
cypress
folder containing Cypress tests cypress-firebase
v3 uses firebase-admin v11 - if you want to use an earlier version of firebase-admin, use ^2
versions of cypress-firebase
Setup
-
Set the following config in your cypress.config.js
or cypress.config.ts
import { initializeApp } from 'firebase-admin/app';
import { defineConfig } from 'cypress';
import { plugin as cypressFirebasePlugin } from 'cypress-firebase';
initializeApp();
const cypressConfig = defineConfig({
e2e: {
baseUrl: 'http://localhost:3000',
supportFile: 'cypress/support/e2e/index.js',
setupNodeEvents(on, config) {
cypressFirebasePlugin(on, config, admin);
},
},
});
export default cypressConfig;
or if you are not using TS, then within cypress.config.js
:
const { defineConfig } = require('cypress');
const cypressFirebasePlugin = require('cypress-firebase').plugin;
const { initializeApp } = require('firebase-admin/app');
initializeApp();
module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost:3000',
supportFile: 'cypress/support/e2e/index.js',
setupNodeEvents(on, config) {
return cypressFirebasePlugin(on, config, admin);
},
},
});
-
Add the following your custom commands file (cypress/support/e2e.js
or cypress/support/e2e.ts
):
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/database';
import 'firebase/firestore';
import { attachCustomCommands } from 'cypress-firebase';
const fbConfig = {
};
firebase.initializeApp(fbConfig);
attachCustomCommands({ Cypress, cy, firebase });
With Firebase Web SDK version 9
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/database';
import 'firebase/compat/firestore';
import { attachCustomCommands } from 'cypress-firebase';
const fbConfig = {
};
firebase.initializeApp(fbConfig);
attachCustomCommands({ Cypress, cy, firebase });
-
To confirm things are working, create a new test file (cypress/integration/examples/test_hello_world.cy.js
) adding a test that uses the cypress-firebase custom command (cy.callFirestore
):
describe('Some Test', () => {
it('Adds document to test_hello_world collection of Firestore', () => {
cy.callFirestore('add', 'test_hello_world', { some: 'value' });
});
});
-
From the root of your project, start Cypress with the command $(npm bin)/cypress open
. In the Cypress window, click your new test (test_hello_world.js
) to run it.
-
Look in your Firestore instance and see the test_hello_world
collection to confirm that a document was added.
-
Pat yourself on the back, you are all setup to access Firebase/Firestore from within your tests!
Auth
-
Go to Authentication page of the Firebase Console and select an existing user to use as the testing account or create a new user. This will be the account which you use to login while running tests.
-
Get the UID of the account you have selected, we will call this UID TEST_UID
-
Set the UID of the user you created earlier to the Cypress environment. You can do this using a number of methods:
-
Adding CYPRESS_TEST_UID
to a .env
file which is gitignored
-
Adding TEST_UID
to cypress.env.json
(make sure you place this within your .gitignore
)
-
Adding as part of your npm script to run tests with a tool such as cross-env
here:
"test": "cross-env CYPRESS_TEST_UID=your-uid cypress open"
-
Call cy.login()
with the before
or beforeEach
sections of your tests
Running
- Start your local dev server (usually
npm start
) - for faster alternative checkout the test built version section - Open cypress test running by running
npm run test:open
in another terminal window
Considerations For CI
-
Add the following environment variables in your CI:
CYPRESS_TEST_UID
- UID of your test userSERVICE_ACCOUNT
- service account object
Named app support
When using a custom app name or running more than one firebase instance in your app:
const namedApp = firebase.initializeApp(fbConfig, 'app_name');
attachCustomCommands({ Cypress, cy, firebase, app: namedApp });
Docs
Custom Cypress Commands
Table of Contents
cy.login
Login to Firebase using custom auth token.
To specify a tenant ID, either pass the ID as a parameter to cy.login
, or set it as environment variable TEST_TENANT_ID
. Read more about Firebase multi-tenancy.
Examples
Loading TEST_UID
automatically from Cypress env:
cy.login();
Passing a UID
const uid = '123SomeUid';
cy.login(uid);
Passing a tenant ID
const uid = '123SomeUid';
const tenantId = '123SomeTenantId';
cy.login(uid, undefined, tenantId);
cy.logout
Log out of Firebase instance
Examples
cy.logout();
cy.callRtdb
Call Real Time Database path with some specified action such as set
, update
and remove
Parameters
action
String The action type to call with (set, push, update, remove)actionPath
String Path within RTDB that action should be appliedoptions
object Options
options.limitToFirst
number|boolean Limit to the first <num>
results. If true is passed than query is limited to last 1 item.options.limitToLast
number|boolean Limit to the last <num>
results. If true is passed than query is limited to last 1 item.options.orderByKey
boolean Order by key nameoptions.orderByValue
boolean Order by primitive valueoptions.orderByChild
string Select a child key by which to order resultsoptions.equalTo
string Restrict results to <val>
(based on specified ordering)options.startAt
string Start results at <val>
(based on specified ordering)options.endAt
string End results at <val>
(based on specified ordering)
Examples
Set data
const fakeProject = { some: 'data' };
cy.callRtdb('set', 'projects/ABC123', fakeProject);
Set Data With Meta
const fakeProject = { some: 'data' };
cy.callRtdb('set', 'projects/ABC123', fakeProject, { withMeta: true });
Set Data With Timestamps
import firebase from 'firebase/app';
import 'firebase/database';
const fakeProject = {
some: 'data',
createdAt: firebase.database.ServerValue.TIMESTAMP,
};
cy.callRtdb('set', 'projects/ABC123', fakeProject);
With Firebase Web SDK version 9
import firebase from 'firebase/compat/app';
import 'firebase/compat/database';
const fakeProject = {
some: 'data',
createdAt: firebase.database.ServerValue.TIMESTAMP,
};
cy.callRtdb('set', 'projects/ABC123', fakeProject);
Delete Data
cy.callRtdb('delete', 'projects/ABC123');
cy.callRtdb('delete', 'projects', { where: ['name', '==', 'Test Project'] });
cy.callRtdb('delete', 'projectsToDelete');
Get/Verify Data
cy.callRtdb('get', 'projects/ABC123').then((project) => {
cy.wrap(project).its('createdBy').should('equal', Cypress.env('TEST_UID'));
});
Other Args
const opts = { args: ['-d'] };
const fakeProject = { some: 'data' };
cy.callRtdb('update', 'project/test-project', fakeProject, opts);
cy.callFirestore
Call Firestore instance with some specified action. Authentication is through serviceAccount.json since it is at the base
level.
Parameters
action
String The action type to call with (set, push, update, delete)actionPath
String Path within Firestore that action should be applieddataOrOptions
String|Object Data for write actions or options for get actionoptions
Object Options
options.withMeta
boolean Whether or not to include createdAt
and createdBy
options.merge
boolean Merge data during setoptions.batchSize
number Size of batch to use while deletingoptions.where
Array Filter documents by the specified field and the value should satisfy
- the relation constraint provided
options.orderBy
string|Array Order documentsoptions.limit
number Limit to n number of documentsoptions.limitToLast
number Limit to last n number of documentsoptions.statics
admin.firestore Firestore statics (i.e. admin.firestore
). This should only be needed during testing due to @firebase/testing not containing statics
Examples
Basic
cy.callFirestore('set', 'project/test-project', 'fakeProject.json');
Set Data With Server Timestamps
import firebase from 'firebase/app';
import 'firebase/firestore';
const fakeProject = {
some: 'data',
createdAt: firebase.firestore.Timestamp.now(),
};
cy.callFirestore('set', 'projects/ABC123', fakeProject);
With Firebase Web SDK version 9
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
const fakeProject = {
some: 'data',
createdAt: firebase.firestore.Timestamp.now(),
};
cy.callFirestore('set', 'projects/ABC123', fakeProject);
Full
describe('Test firestore', () => {
const TEST_UID = Cypress.env('TEST_UID');
const mockAge = 8;
beforeEach(() => {
cy.visit('/');
cy.callFirestore('delete', 'testCollection');
});
it('read/write test', () => {
cy.log('Starting test');
cy.callFirestore('set', `testCollection/${TEST_UID}`, {
name: 'axa',
age: 8,
});
cy.callFirestore('get', `testCollection/${TEST_UID}`).then((r) => {
cy.log('get returned: ', r);
cy.wrap(r).its('data.age').should('equal', mockAge);
});
cy.log('Ended test');
});
});
Recipes
Using Emulators
-
Install cross-env for cross system environment variable support: npm i --save-dev cross-env
-
Add the following to the scripts
section of your package.json
:
"emulators": "firebase emulators:start --only database,firestore",
"test": "cypress run",
"test:open": "cypress open",
"test:emulate": "cross-env FIREBASE_AUTH_EMULATOR_HOST=\"localhost:$(cat firebase.json | jq .emulators.auth.port)\" FIREBASE_DATABASE_EMULATOR_HOST=\"localhost:$(cat firebase.json | jq .emulators.database.port)\" FIRESTORE_EMULATOR_HOST=\"localhost:$(cat firebase.json | jq .emulators.firestore.port)\" yarn test:open"
-
If not already set by firebase init
, add emulator ports to firebase.json
:
"emulators": {
"database": {
"port": 9000
},
"firestore": {
"port": 8080
},
"auth": {
"port": 9099
}
}
-
Modify your application code to connect to the emulators (where your code calls firebase.initializeApp(...)
), updating the localhost ports as appropriate from the emulators
values in the previous step:
const shouldUseEmulator = window.location.hostname === 'localhost';
if (shouldUseEmulator) {
fbConfig.databaseURL = `http://localhost:9000?ns=${fbConfig.projectId}`;
console.debug(`Using RTDB emulator: ${fbConfig.databaseURL}`);
}
firebase.initializeApp(fbConfig);
const firestoreSettings = {};
if (window.Cypress) {
firestoreSettings.experimentalForceLongPolling = true;
}
if (shouldUseEmulator) {
firestoreSettings.host = 'localhost:8080';
firestoreSettings.ssl = false;
console.debug(`Using Firestore emulator: ${firestoreSettings.host}`);
firebase.firestore().settings(firestoreSettings);
}
if (shouldUseEmulator) {
firebase.auth().useEmulator(`http://localhost:9099/`);
console.debug(`Using Auth emulator: http://localhost:9099/`);
}
-
Make sure you also have init logic in cypress/support/commands.js
or cypress/support/index.js
:
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/database';
import 'firebase/firestore';
import { attachCustomCommands } from 'cypress-firebase';
const fbConfig = {
};
const rtdbEmulatorHost = Cypress.env('FIREBASE_DATABASE_EMULATOR_HOST');
if (rtdbEmulatorHost) {
fbConfig.databaseURL = `http://${rtdbEmulatorHost}?ns=${fbConfig.projectId}`;
}
firebase.initializeApp(fbConfig);
const firestoreEmulatorHost = Cypress.env('FIRESTORE_EMULATOR_HOST');
if (firestoreEmulatorHost) {
firebase.firestore().settings({
host: firestoreEmulatorHost,
ssl: false,
});
}
const authEmulatorHost = Cypress.env('FIREBASE_AUTH_EMULATOR_HOST');
if (authEmulatorHost) {
firebase.auth().useEmulator(`http://${authEmulatorHost}/`);
console.debug(`Using Auth emulator: http://${authEmulatorHost}/`);
}
attachCustomCommands({ Cypress, cy, firebase });
With Firebase Web SDK version 9 in compat mode (same API as v8 with different import)
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/database';
import 'firebase/compat/firestore';
import { attachCustomCommands } from 'cypress-firebase';
const fbConfig = {
};
const rtdbEmulatorHost = Cypress.env('FIREBASE_DATABASE_EMULATOR_HOST');
if (rtdbEmulatorHost) {
fbConfig.databaseURL = `http://${rtdbEmulatorHost}?ns=${fbConfig.projectId}`;
}
firebase.initializeApp(fbConfig);
const firestoreEmulatorHost = Cypress.env('FIRESTORE_EMULATOR_HOST');
if (firestoreEmulatorHost) {
firebase.firestore().settings({
host: firestoreEmulatorHost,
ssl: false,
});
}
const authEmulatorHost = Cypress.env('FIREBASE_AUTH_EMULATOR_HOST');
if (authEmulatorHost) {
firebase.auth().useEmulator(`http://${authEmulatorHost}/`);
console.debug(`Using Auth emulator: http://${authEmulatorHost}/`);
}
attachCustomCommands({ Cypress, cy, firebase });
- Start emulators:
npm run emulators
- In another terminal window, start the application:
npm start
- In another terminal window, open test runner with emulator settings:
npm run test:emulate
NOTE: If you are using react-scripts
(from create-react-app) or other environment management, you can use environment variables to pass settings into your app:
const {
REACT_APP_FIREBASE_DATABASE_EMULATOR_HOST,
REACT_APP_FIRESTORE_EMULATOR_HOST,
} = process.env;
if (REACT_APP_FIREBASE_DATABASE_EMULATOR_HOST) {
console.debug(`Using RTDB emulator: ${fbConfig.databaseURL}`);
fbConfig.databaseURL = `http://${REACT_APP_FIREBASE_DATABASE_EMULATOR_HOST}?ns=${fbConfig.projectId}`;
}
firebase.initializeApp(fbConfig);
const firestoreSettings = {};
if (window.Cypress) {
firestoreSettings.experimentalForceLongPolling = true;
}
if (REACT_APP_FIRESTORE_EMULATOR_HOST) {
firestoreSettings.host = REACT_APP_FIRESTORE_EMULATOR_HOST;
firestoreSettings.ssl = false;
console.debug(`Using Firestore emulator: ${firestoreSettings.host}`);
firebase.firestore().settings(firestoreSettings);
}
Use Different RTDB Instance
Firebase instance config can be overriden by passing another argument to the cypress-firebase plugin. We can use this to override the databaseURL
:
-
Setup the config within plugin (cypress/plugins/index.js
):
const admin = require('firebase-admin');
const cypressFirebasePlugin = require('cypress-firebase').plugin;
module.exports = (on, config) => {
const overrideFirebaseConfig = {
databaseURL: 'http://localhost:9000?ns=my-other-namespace',
};
const extendedConfig = cypressFirebasePlugin(
on,
config,
admin,
overrideFirebaseConfig,
);
return extendedConfig;
};
-
Make sure you use the same databaseURL
when initializing the firebase instance within cypress (cypress/support/index.js
)
-
Make sure you use the same databaseURL
when initializing the firebase instance within your app code
Test Built Version
It is often required to run tests against the built version of your app instead of your dev version (with hot module reloading and other dev tools). You can do that by running a build script before spinning up the:
- Adding the following npm script:
"start:dist": "npm run build && firebase emulators:start --only hosting",
- Add the emulator port to
firebase.json
:
"emulators": {
"hosting": {
"port": 3000
}
}
- Run
npm run start:dist
to build your app and serve it with firebase - In another terminal window, run a test command such as
npm run test:open
NOTE: You can also use firebase serve
:
"start:dist": "npm run build && firebase serve --only hosting -p 3000",
Changing Custom Command Names
Pass commandNames
in the options
object to attachCustomCommands
:
const options = {
commandNames: {
login: 'newNameForLogin',
logout: 'newNameForLogout',
callRtdb: 'newNameForCallRtdb',
callFirestore: 'newNameForCallFirestore',
getAuthUser: 'newNameForGetAuthUser',
},
};
attachCustomCommands({ Cypress, cy, firebase }, options);
For more information about this feature, please see the original feature request.
Webpack File Preprocessing
If you are using a file preprocessor which is building for the browser environment, such as Webpack, you will need to make sure usage of fs
is handled since it is used within the cypress-firebase plugin. To do this with webpack, add the following to your config:
node: {
fs: 'empty';
}
See #120 for more info
Examples
Github Actions
Separate Install
name: Test Build
on: [pull_request]
jobs:
ui-tests:
name: UI Tests
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Cypress Run
uses: cypress-io/github-action@v2
with:
group: 'E2E Tests'
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_KEY }}
CYPRESS_TEST_UID: ${{ secrets.TEST_UID }}
SERVICE_ACCOUNT: ${{ secrets.SERVICE_ACCOUNT }}
GITHUB_HEAD_REF: ${{ github.head_ref }}
GITHUB_REF: ${{ github.ref }}
Using Start For Local
name: Test
on: [pull_request]
jobs:
ui-tests:
name: UI Tests
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Cypress Run
uses: cypress-io/github-action@v2
with:
group: 'E2E Tests'
start: npm start
wait-on: http://localhost:3000
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_KEY }}
CYPRESS_TEST_UID: ${{ secrets.TEST_UID }}
SERVICE_ACCOUNT: ${{ secrets.SERVICE_ACCOUNT }}
GITHUB_HEAD_REF: ${{ github.head_ref }}
GITHUB_REF: ${{ github.ref }}
Why?
When testing, tests should have admin read/write access to the database for seeding/verifying data. It isn't currently possible to use Firebase's firebase-admin
SDK directly within Cypress tests due to dependencies not being able to be loaded into the Browser environment. Since the admin SDK is necessary to generate custom tokens and interact with Real Time Database and Firestore with admin privileges, this library provides convenience methods (cy.callRtdb
, cy.callFirestore
, cy.login
, etc...) which call custom tasks which have access to the node environment.
Projects Using It
Troubleshooting
- An error is coming from cypress mentioning "Error converting circular structure to JSON"
The issue is most likely due to a circular object, such as a timestamp, being included in data you are attempting to write to Firestore. Instead of using firebase.firestore.FieldValue.serverTimestamp()
you should instead use firebase.firestore.Timestamp.now()
or you would like to specify a certain date firebase.firestore.Timestamp.fromDate(new Date('01/01/18'))
.
This comes from the fact that cypress stringifies values as it is passing them from the browser environment to the node environment through cy.task
.
- An error is causing tests to fail mentioning "firebaseinstallations.googleapis.com blocked by CORS policy"
This has to do with the Firebase JS SDK having problems calling a Google API - this issue has mostly been seen with older versions of Firebase SDK (pre v8) when being tested on Firebase Hosting (as opposed to a local server).
The following should help prevent the issue from failing tests:
Cypress.on('uncaught:exception', (err) => {
return err.message.includes('firebaseinstallations.googleapis.com');
});
If you experience this with an SDK version newer than v7 please create a new issue.
Future Plans
- firebase-admin v10 module support
- Drop support for service account file in favor of application default credentails env variable (path to file set in
GOOGLE_APPLICATION_CREDENTIALS
) - Support for Auth emulators (this will become the suggested method instead of needing a service account)