如何在本地测试`functions.https.onCall` firebase云功能?

时间:2018-06-16 02:41:32

标签: firebase google-cloud-functions

我无法在Firebase Document找到解决方法。

我想在本地测试我的functions.https.onCall函数。是否可以使用shell或以某种方式将我的客户端(启用firebase SDK)连接到本地服务器?

我希望每次只是为了测试对onCall函数的更改而进行部署。

我的代码

功能:

exports.myFunction = functions.https.onCall((data, context) => {
  // Do something
});

客户端:

const message = { message: 'Hello.' };

firebase.functions().httpsCallable('myFunction')(message)
  .then(result => {
    // Do something //
  })
  .catch(error => {
    // Error handler //
  });

6 个答案:

答案 0 :(得分:14)

对于本地,您必须致电(在firebase.initializeApp之后)

firebase.functions().useFunctionsEmulator('http://localhost:5000') 

答案 1 :(得分:4)

Callables只是具有特定格式的HTTPS函数。您可以test just like a HTTPS function,除非您必须编写代码以将协议作为defined in the documentation提供。

答案 2 :(得分:1)

有一个简单的技巧,如何简化onCall-功能测试。只需将onCall函数回调声明为本地函数,然后对其进行测试:

const _myFunction = (data, context) => { // <= call this on your unit tests
  // Do something
}

exports.myFunction = functions.https.onCall(_myFunction);

现在,您可以使用函数调用中定义的输入来对所有具有普通函数的情况进行变通。

答案 3 :(得分:0)

尽管Firebase Cloud Function官方文档尚未更新,但您现在可以将firebase-functions-testonCall函数一起使用。

您可以看到一个example in their repository

我设法用玩笑来测试我的TypeScript函数,这是一个简单的示例。这里有一些特殊性,例如导入顺序,因此请务必阅读文档:-)

/* 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."
    );
  });
});

答案 4 :(得分:0)

您应该首先检查开发环境,然后将功能指向本地仿真器。
对于JS:

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

或者,如果您不创建firebase实例,则

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

或从后端(node.js)提供页面时:

//after firebase init
if (process.env.NODE_ENV === 'development') {
  firebase.functions().useFunctionsEmulator('http://localhost:5001');
}

答案 5 :(得分:0)

如果您使用的是angularfire,请将其添加到您的app.module

{
  provide: FirestoreSettingsToken,
  useValue: environment.production
    ? undefined
    : {
        host: "localhost:5002",
        ssl: false
      }
}
相关问题