Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

345
Views
How to call a firebase functions from a unit test using Firebase Emulators?

I have the firebase emulators running.

My application can call a firebase cloud function that can create a custom user claim if the user is allowed to (depending on a value in the database)

I'm trying to invoke that function:


import * as firebase from '@firebase/testing';
import * as fs from 'fs';

const app = firebase.initializeTestApp({
  databaseName: 'mydb',
  auth: {
    uid: 'useruid'
  },
});

describe('Firebase Functions', () => {
  beforeEach(async () => {
    await firebase.loadDatabaseRules({
      databaseName: 'mydb',
      rules: fs.readFileSync('database.rules.json', 'utf8'),
    });
  });

  afterAll(async () => {
    await Promise.all(firebase.apps().map((app) => app.delete()));
  });

  const cloudfunction = app.functions().httpsCallable('cloudfunction')

  it('throws error if user is not authenticated', async () => {
    await firebase.assertFail(cloudfunction())
  })
});


This is throwing an error:

Error: Error: Response for preflight has invalid HTTP status code 404

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In my opinion, you don't need to call firebase from your unit test, because it is not your business is firebase working correctly or not, you need to test your business logic so that means that you will mock the method which calls to the firebase and test expectable returning values of this method, for example, you can write 2 cases when notification will be sent without error and second if firebase service will return failure, for those 2 cases your code should have appropriate behavior. Testing firebase service will be done by the team working on the firebase

about 4 years ago · Juan Pablo Isaza Report

0

There exists firebase-functions-test for this. This allows you to wrap your cloud functions and call them directly, as well as create stubs for document updates. When running in the context of the emulator, the firebase-admin SDK still interacts with the emulator. A basic setup might look like the following (assuming typescript):

// functions/src/test/functions.test.ts
import {expect} from "chai";
import "mocha";

import firebaseFunctionTest from "firebase-functions-test";
import * as cf from "../index";

const projectId = "demo-project";
const test = firebaseFunctionTest({
  projectId,
});

after(() => test.cleanup());

describe("cloud functions", () => {
  afterEach(async () => {
    await test.firestore.clearFirestoreData(projectId);
  });

  describe("myFunction", () => {
      const myFunction = test.wrap(cf.myFunction);
      // Create mock data with the admin SDK and test function.
  }
}

In your package.json you can the put a script like.

"scripts": {
  "test:unit": "mocha -r ts-node/register --timeout 5000 --exit src/**/*.test.ts",
  "test": "npm run build && npx firebase -P demo-project emulators:exec 'npm run test:unit'",
  ...
}  

You might also want to have a look at https://github.com/firebase/quickstart-testing/tree/master/unit-test-cloud-functions and https://firebase.google.com/docs/functions/unit-testing.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!