DEV Community


TDD in Cloud Functions for Firebase using rescue-fire

・3 min read

The deployment of Cloud Functions is very slow. Deployment is normally completed in 30 seconds, but it sometimes take more than 10 minutes.
It is a waste of time to wait a few minutes just by rewriting one line.

Cloud Functions Emulator is very useful. However, it is hard to create the test data json, and it is not possible to write tests.

Let's emulate functions locally with rescue-fire and do TDD.

logo\.png \(733×207\)

How to

Cloud Functions starts with a variable called event.

exports.updateUser = functions.firestore.document('users/{userId}')
  .onCreate(event => {
    console.log('old name',
    return{name: 'new name'})
Enter fullscreen mode Exit fullscreen mode

If you can create this event, you can use Admin SDK to run almost the same code as Cloud Functions.
rescue-fire makes this event.

1. Installation

npm install rescue-fire --only=dev
yarn add --dev rescue-fire
Enter fullscreen mode Exit fullscreen mode

2. Prepare Google Cloud Account Credentials

Download the service account key json file.

This json file is sensitive, be careful.

Important: This file contains sensitive information, including your service account's private encryption key. Keep it confidential and never store it in a public repository.

3. Install testing library

Please use your favorite testing library.

For example, in the case of Jest:

npm install jest --only=dev
yarn add --dev jest
Enter fullscreen mode Exit fullscreen mode

4. Write a test

Let's create a function to update name when user is created. The code of the function is as follows.

This sample is written in TypeScript.

const changeName = (event: functions.Event<DeltaDocumentSnapshot>) => {
  console.log('old name',
  return{ name: 'new name' })
Enter fullscreen mode Exit fullscreen mode

The test will be like this.

import 'jest'
import * as admin from 'firebase-admin'
import * as functions from 'firebase-functions'
import * as Rescue from 'rescue-fire'

// Set up to run firebase in local.
beforeAll(() => {
  const serviceAccount = require('./your-firebase-adminsdk.json')
    credential: admin.credential.cert(serviceAccount)

test('update name', async () => {
  // prepare
  const data = {name: 'name'}
  const user = await admin.firestore().collection('user').add(data)
  const event = Rescue.event(user, data)

  // start Cloud Functions
  await changeName(event)

  // expect name changed
  const updatedUser = await admin.firestore().collection('user').doc(
  expect(!.name).toBe('new name')
Enter fullscreen mode Exit fullscreen mode

Cloud Functions can be developed with TDD. (strictly not TDD 🙃)

This is a small function, but orderable.test.ts is testing huge functions with rescue-fire.

Optional Parameters defenitions is here.

4. Finally, create Functions

exports.updateUser = functions.firestore
  .onCreate(event => {
    return changeName(event)
Enter fullscreen mode Exit fullscreen mode



The event created by rescue-fire is not complete. I think that it is enough to write tests, but keep in mind that it is different from the actual event.

Please use starhoshi/rescue-fire: A test helper for Cloud Functions.!

Discussion (0)

Forem Open with the Forem app