簡體   English   中英

如何在本地測試`functions.https.onCall` firebase 雲功能?

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

我似乎無法在Firebase 文檔中找到解決方案。

我想在本地測試我的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 //
  });

對於本地,您必須調用(在 firebase.initializeApp 之后)

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

Callables 只是具有特定格式的 HTTPS 函數。 您可以像測試 HTTPS 功能一樣進行測試,不同之處在於您必須編寫代碼以向其提供 文檔中定義的協議。

盡管官方 Firebase Cloud Function 文檔尚未更新,但您現在可以將onCall -functions-testonCall函數一起使用。

您可以在他們的存儲庫中看到一個示例

我已經設法使用 jest 測試了我的 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."
    );
  });
});

有一個簡單的技巧,如何簡化onCall函數測試。 只需將 onCall 函數回調聲明為本地函數並進行測試:

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

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

現在,您可以使用在函數調用中定義的輸入,使用普通函數來改變所有情況。

您應該首先檢查開發環境,然后將您的功能指向本地模擬器。
對於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");
  }

或者,如果您不創建 firebase 實例,則

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

或從后端(node.js)提供頁面時:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM