vendredi 25 septembre 2020

Sinon testing firebase cloud function

I have a cloud function that runs on firebase userCreate. I am trying to write a unit test using Sinon to assert that the region function is called.

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require("firebase-functions");

const region = functions.config().region || "europe-west2";

exports.userDocCreate = functions
  .region(region)
  .auth.user()
  .onCreate((user) => {

How can I assert that the region method of functions is called? If I stub the method it never gets called.

My unit test is as follows:

const chai = require("chai");
const assert = chai.assert;
const sinon = require("sinon");
const admin = require("firebase-admin");
const test = require("firebase-functions-test")();

describe("userDocCreate", () => {
  let adminInitStub;
  let regionStub;
  let myFunctions;

  before(() => {
    adminInitStub = sinon.stub(admin, "initializeApp");
    myFunctions = require("../userDocCreate");
  });

  beforeEach(() => {
    regionStub =  sinon.stub(functions, "region").fakes(() => {});
  });

  it("region should be called", () => {
    const wrapped = test.wrap(myFunctions.userDocCreate);
    wrapped();
    sinon.assert.called(regionStub)
  });


});

I have been working at this for a number of hours and have so far not found a solution. I can stub other firebase methods fine for example

sinon.stub(admin, "firestore").get(() => firestoreStub);

Works fine

Any help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire