Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

350
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda