3
votes

I have an Express API using the Firebase Admin SDK.

Currently, my application is failing CI as it cannot initialise on the test stage, due to not being able to connect to Firebase.

I have a serviceAccountKey.json file in the root of my project, that I import as follows..

import * as fbseAdmin from 'firebase-admin'

const FIREBASE_DB_URI = process.env.FIREBASE_DB_URI
const serviceAccount = require('../serviceAccountKey.json')

fbseAdmin.initializeApp({
  credential: fbseAdmin.credential.cert(serviceAccount),
  databaseURL: FIREBASE_DB_URI
})

export default fbseAdmin

This file is excluded from source control as it contains sensitive information.

The first issue I have is when my CI build runs, the tests fail as Error: Cannot find module '../serviceAccountKey.json'

How is best to approach this? Should I mock the file? I'd prefer not to keep a mock file floating around my solution.

Secondly, I believe the app will fail to start if Firebase does not initialise correctly.

Should I setup a mock Firebase project for testing?

1

1 Answers

4
votes

You will require a valid instance of your Firebase cert if you need to initialise your app.

If you prefer not to check your cert file in to source control, which I agree is the best idea, you could then instead perhaps setup another application in the Firebase console, strictly for testing purposes.

Using this app you could Base64 encode the cert, setting it as an environment variable in your build.

Prior to running your app in the CI container, you then simply echo out the decoded BASE64 environment variable into a file name myServiceKey.json or something similar.

steps:
  - checkout
  - run: echo $FIREBASE_SERVICE_KEY | base64 -di > ./${FIREBASE_SERVICE_ACCOUNT_CERT_NAME}

This would produce the required file for testing purposes in the root of your project.