Thursday, 23 September 2021

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



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

No comments:

Post a Comment