How to test `functions.https.onCall` firebase cloud functions locally?

后端 未结 6 714
悲哀的现实
悲哀的现实 2020-12-01 07:42

I can\'t seem to find the solution for this in the Firebase Documentation.

I want to test my functions.https.onCall functions locally. Is it possible usi

相关标签:
6条回答
  • 2020-12-01 08:08

    you should first check for dev environment and then point your functions to local emulator.
    For JS:

    //after firebase init
    if (window.location.host.includes("localhost") ||
        window.location.host.includes("127.0.0.1")
    ) {
        firebase
          .app()
          .functions()  //add location here also if you're mentioning location while invoking function()
          .useFunctionsEmulator("http://localhost:5001");
      }
    

    or if you don't create instance of firebase then

    //after firebase init
    if (window.location.host.includes("localhost") || 
       window.location.host.includes("127.0.0.1")
    ) {
        firebase
          .functions()
          .useFunctionsEmulator("http://localhost:5001");
      }
    

    or when serving pages from backend (node.js):

    //after firebase init
    if (process.env.NODE_ENV === 'development') {
      firebase.functions().useFunctionsEmulator('http://localhost:5001');
    }
    
    0 讨论(0)
  • 2020-12-01 08:13

    For locally you must call (after firebase.initializeApp)

    firebase.functions().useFunctionsEmulator('http://localhost:5000') 
    
    0 讨论(0)
  • 2020-12-01 08:14

    Callables are just HTTPS functions with a specific format. You can test just like a HTTPS function, except you have to write code to deliver it the protocol as defined in the documentation.

    0 讨论(0)
  • 2020-12-01 08:21

    Although the official Firebase Cloud Function docs have not yet been updated, you can now use firebase-functions-test with onCall functions.

    You can see an example in their repository.

    I have managed to test my TypeScript functions using jest, here is a brief example. There are some peculiarities here, like import order, so make sure to read the docs :-)

    /* functions/src/test/index.test.js */
    /* dependencies: Jest and jest-ts */
    
    const admin = require("firebase-admin");
    jest.mock("firebase-admin");
    admin.initializeApp = jest.fn(); // stub the init (see docs)
    const fft = require("firebase-functions-test")();
    
    import * as funcs from "../index";
    
    // myFunc is an https.onCall function
    describe("test myFunc", () => {
      // helper function so I can easily test different context/auth scenarios
      const getContext = (uid = "test-uid", email_verified = true) => ({
        auth: {
          uid,
          token: {
            firebase: {
              email_verified
            }
          }
        }
      });
      const wrapped = fft.wrap(funcs.myFunc);
    
      test("returns data on success", async () => {
        const result = await wrapped(null, getContext());
        expect(result).toBeTruthy();
      });
    
      test("throws when no Auth context", async () => {
        await expect(wrapped(null, { auth: null })).rejects.toThrow(
          "No authentication context."
        );
      });
    });
    
    0 讨论(0)
  • 2020-12-01 08:31

    if you are using angularfire, add this to you app.module

    {
      provide: FirestoreSettingsToken,
      useValue: environment.production
        ? undefined
        : {
            host: "localhost:5002",
            ssl: false
          }
    }
    
    0 讨论(0)
  • 2020-12-01 08:32

    There is a simple trick, how you can simplify onCall -function testing. Just declare the onCall function callback as a local function and test that instead:

    const _myFunction = (data, context) => { // <= call this on your unit tests
      // Do something
    }
    
    exports.myFunction = functions.https.onCall(_myFunction);
    

    Now you can variate all cases with a normal function with the input you define on your function call.

    0 讨论(0)
提交回复
热议问题