Utilities to help testing firebase projects with cypress.
Switch branches/tags
Clone or download
prescottprue 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`)
Latest commit bc3f6e0 Dec 1, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin Initial commit. Oct 19, 2018
cmds v0.0.7 (#3) Dec 1, 2018
examples v0.0.2 Oct 20, 2018
src v0.0.8 (#4) Dec 1, 2018
.babelrc v0.0.4 (#1) Nov 16, 2018
.eslintignore v0.0.7 (#3) Dec 1, 2018
.eslintrc.js v0.0.7 (#3) Dec 1, 2018
.gitignore v0.0.2 Oct 20, 2018
.npmignore v0.0.4 (#1) Nov 16, 2018
.travis.yml v0.0.2 Oct 20, 2018
LICENSE v0.0.2 Oct 20, 2018
README.md v0.0.8 (#4) Dec 1, 2018
package-lock.json v0.0.7 (#3) Dec 1, 2018
package.json v0.0.8 (#4) Dec 1, 2018

README.md

cypress-firebase

NPM version Build Status Dependency Status License Code Style

Utilities and cli to help testing Firebase projects with Cypress

What?

If you are intereted in what drove the need for this checkout the why section

Usage

Pre-Setup

Note: Skip cypress install if it already exists within your project

  1. Log into your app for the first time
  2. Go to the Auth tab of Firebase and get your UID. This will be the account which you use to login while running tests (we will call this UID TEST_UID)
  3. Generate a service account -> save it as serviceAccount.json
  4. Install Cypress and add it to your package file: npm i --save-dev cypress
  5. Add cypress folder by calling cypress open

Setup

Note: These instructions assume your tests are in the cypress folder (cypress' default). See the folders section below for more info about other supported folders.

  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.

  2. Install using npm i cypress-firebase --save-dev

  3. Add the following to the scripts section of your package.json:

    "build:testConfig": "cypress-firebase createTestEnvFile",
    "test": "npm run build:testConfig cypress run",
    "test:open": "npm run build:testConfig cypress open",
    "test:stage": "npm run test -- --env envName=stage",
    "test:open:stage": "npm run test:open -- --env envName=stage"

    Environment variables can be passed through --env. envName points to the firebase project within the projects section of .firebaserc.

  4. Add your config info to cypress/config.json

    {
      "TEST_UID": "<- uid of the user you want to test as ->",
      "FIREBASE_PROJECT_ID": "<- projectId of your project ->"
    }
  5. Add the following your custom commands file (cypress/support/commands.js):

    import firebase from 'firebase/app';
    import 'firebase/auth';
    import 'firebase/database';
    import 'firebase/firestore';
    import { attachCustomCommands } from 'cypress-firebase';
    
    const fbConfig = {
      // Your config from Firebase Console
    };
    
    window.fbInstance = firebase.initializeApp(fbConfig);
    
    attachCustomCommands({ Cypress, cy, firebase })
  6. Setup plugin adding following your plugins file (cypress/plugins/index.js):

    const cypressFirebasePlugin = require('cypress-firebase').plugin
    
    module.exports = (on, config) => {
      // `on` is used to hook into various events Cypress emits
      // `config` is the resolved Cypress config
    
      // Return extended config (with settings from .firebaserc)
      return cypressFirebasePlugin(config)
    }

    The plugin sets baseUrl and loads config from .firebaserc

Running

  1. Start your local dev server (usually npm start) - for faster alternative checkout the test built version section
  2. Open cypress test running by running npm run test:open in another terminal window

Test Built Version

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:

  1. Adding the following npm script:

    "start:dist": "npm run build && firebase serve --only hosting -p 3000",
  2. Run npm run start:dist to build your app and serve it with firebase

  3. In another terminal window, run a test command such as npm run test:open

CI

  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)
  2. Set FIREBASE_TOKEN within CI environment variables

Folders

cypress is the default folder where config is loaded from, but these other common folders are supported:

test/ui
test/e2e

Docs

CLI Commands

createTestEnvFile {#createTestEnvFile}

Create test environment file (cypress.env.json) which contains custom auth token generated using firebase-admin SDK and serviceAccount.json.

Examples
cypress-firebase createTestEnvFile

Custom Cypress Commands

Table of Contents

cy.login

Login to Firebase auth using FIREBASE_AUTH_JWT environment variable which is generated using firebase-admin authenticated with serviceAccount during build:testConfig phase.

Examples
cy.login()

cy.logout

Log out of Firebase instance

Examples
cy.logout()

cy.callRtdb

Call Real Time Database path with some specified action. Authentication is through FIREBASE_TOKEN since firebase-tools is used (instead of firebaseExtra).

Parameters
  • action String The action type to call with (set, push, update, remove)
  • actionPath String Path within RTDB that action should be applied
  • opts Object Options
    • opts.args Array Command line args to be passed
Examples

Set data

const fakeProject = { some: 'data' }
cy.callRtdb('set', 'projects/ABC123', fakeProject)

Set Data With Meta

const fakeProject = { some: 'data' }
// Adds createdAt and createdBy (current user's uid) on data
cy.callRtdb('set', 'projects/ABC123', fakeProject, { withMeta: true })

Get/Verify Data

cy.callRtdb('get', 'projects/ABC123')
  .then((project) => {
    // Confirm new data has users uid
    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. If using delete, auth is through FIREBASE_TOKEN since firebase-tools is used (instead of firebaseExtra).

Parameters
  • action String The action type to call with (set, push, update, remove)
  • actionPath String Path within RTDB that action should be applied
  • opts Object Options
    • opts.args Array Command line args to be passed
Examples

Basic

cy.callFirestore('add', 'project/test-project', 'fakeProject.json')

Recursive Delete

const opts = { recursive: true }
cy.callFirestore('delete', 'project/test-project', opts)

Other Args

const opts = { args: ['-r'] }
cy.callFirestore('delete', 'project/test-project', opts)

Why?

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.

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!

Projects Using It

fireadmin.io - A Firebase project management tool (here is the source)

Roadmap

  • Fix issue where auth token goes bad after test suite has been open a long time