简体   繁体   中英

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM