在本地運行函數

Firebase CLI 包含一個 Cloud Functions 模擬器,可以模擬下列函數類型:

  • HTTPS功能
  • 可呼叫函數
  • 從 Firebase 驗證、即時資料庫、Cloud Firestore、Cloud Storage 和 Cloud Pub/Sub 觸發的後台功能。

您可以在本機運行函數以在部署到生產環境之前對其進行測試。

安裝 Firebase CLI

若要使用 Cloud Functions 模擬器,請先安裝 Firebase CLI:

npm install -g firebase-tools

為了使用本機模擬器,您的 Cloud Functions 必須依賴:

  • firebase-admin版本8.0.0或更高版本。
  • firebase-functions版本3.0.0或更高版本。

設定管理員憑證(可選)

如果您希望函數測試透過Firebase Admin SDK與 Google API 或其他 Firebase API 交互,則可能需要設定管理員憑證。

  • Cloud Firestore 和即時資料庫觸發器已擁有足夠的憑證,並且不需要額外的設定。
  • 所有其他 API,包括 Firebase API(如身份驗證和 FCM)或 Google API(如 Cloud Translation 或 Cloud Speech),都需要執行本節中所述的設定步驟。無論您使用的是 Cloud Functions shell 還是firebase emulators:start這都適用。

若要為模擬功能設定管理員憑證:

  1. 開啟 Google Cloud 控制台的服務帳號窗格
  2. 確保選擇App Engine 預設服務帳戶,然後使用右側的選項選單選擇Create key
  3. 出現提示時,選擇JSON作為金鑰類型,然後按一下Create
  4. 將您的 Google 預設憑證設定為指向下載的金鑰:

    Unix

    export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json"
    firebase emulators:start
    

    視窗

    set GOOGLE_APPLICATION_CREDENTIALS=path\to\key.json
    firebase emulators:start
    

完成這些步驟後,您的功能測試可以使用Admin SDK存取 Firebase 和 Google API。例如,在測試身份驗證觸發器時,模擬函數可以呼叫admin.auth().getUserByEmail(email)

設定功能配置(選用)

如果您使用自訂函數配置變量,請先執行命令以在本機環境中取得自訂配置(在functions目錄中執行此命令):

firebase functions:config:get > .runtimeconfig.json
# If using Windows PowerShell, replace the above with:
# firebase functions:config:get | ac .runtimeconfig.json

運行模擬器套件

若要執行 Cloud Functions 模擬器,請使用emulators:start指令:

firebase emulators:start

emulators:start指令將根據您在本機專案中使用firebase init初始化的產品啟動 Cloud Functions、Cloud Firestore、即時資料庫和 Firebase 託管的模擬器。如果您想啟動特定的模擬器,請使用--only標誌:

firebase emulators:start --only functions

如果您想在模擬器啟動後執行測試套件或測試腳本,請使用emulators:exec命令:

firebase emulators:exec "./my-test.sh"

檢測您的應用程式以與模擬器對話

要使您的應用程式與模擬器交互,您可能需要進行一些額外的配置。

為您的應用程式偵測可呼叫函數

如果您的原型和測試活動涉及可呼叫後端函數,請配置與 Cloud Functions for Firebase 模擬器的交互,如下所示:

Kotlin+KTX
// 10.0.2.2 is the special IP address to connect to the 'localhost' of
// the host computer from an Android emulator.
val functions = Firebase.functions
functions.useEmulator("10.0.2.2", 5001)
Java
// 10.0.2.2 is the special IP address to connect to the 'localhost' of
// the host computer from an Android emulator.
FirebaseFunctions functions = FirebaseFunctions.getInstance();
functions.useEmulator("10.0.2.2", 5001);
迅速
Functions.functions().useFunctionsEmulator(origin: "http://127.0.0.1:5001")

Web modular API

import { getApp } from "firebase/app";
import { getFunctions, connectFunctionsEmulator } from "firebase/functions";

const functions = getFunctions(getApp());
connectFunctionsEmulator(functions, "127.0.0.1", 5001);

Web namespaced API

firebase.functions().useEmulator("127.0.0.1", 5001);

為您的應用程式偵測 HTTPS 功能模擬

程式碼中的每個 HTTPS 函數都將使用以下 URL 格式從本機模擬器提供服務:

http:// $HOST : $PORT / $PROJECT / $REGION / $NAME

例如,具有預設主機連接埠和區域的簡單helloWorld函數將在以下位置提供服務:

https://localhost:5001/ $PROJECT /us-central1/helloWorld

為您的應用程式配備後台觸發功能模擬

Cloud Functions 模擬器支援以下來源的後台觸發函數:

  • 即時資料庫模擬器
  • Cloud Firestore 模擬器
  • 認證模擬器
  • 發布/訂閱模擬器

若要觸發後台事件,請使用模擬器套件 UI 修改後端資源,或使用適用於您平台的 SDK 將應用程式或測試程式碼連接到模擬器。

測試擴充功能發出的自訂事件的處理程序

對於您使用 Cloud Functions v2 實作來處理 Firebase Extensions 自訂事件的函數,Cloud Functions 模擬器與 Eventarc 模擬器配對以支援Eventarc 觸發器

若要測試發出事件的擴充功能的自訂事件處理程序,您必須安裝 Cloud Functions 和 Eventarc 模擬器。

如果 Eventarc 模擬器正在執行,則 Cloud Functions 運行時會將目前進程中的EVENTARC_EMULATOR環境變數設為localhost:9299 。設定EVENTARC_EMULATOR環境變數後,Firebase Admin SDK 會自動連接到 Eventarc 模擬器。您可以按照配置本機模擬器套件中的討論修改預設連接埠。

正確配置環境變數後,Firebase Admin SDK 會自動將事件傳送到 Eventarc 模擬器。反過來,Eventarc 模擬器會回呼 Cloud Functions 模擬器以觸發任何已註冊的處理程序。

您可以檢查模擬器套件 UI 中的函數日誌,以了解有關處理程序執行的詳細資訊。

與其他服務的交互

模擬器套件包括多個模擬器,可以測試跨產品互動。

雲端Firestore

如果您有使用 Firebase Admin SDK 寫入 Cloud Firestore 的函數,則這些寫入將會傳送到 Cloud Firestore 模擬器(如果該模擬器正在執行)。如果這些寫入觸發更多函數,它們將在 Cloud Functions 模擬器中執行。

雲端儲存

如果您有使用 Firebase Admin SDK(版本 9.7.0 或更高版本)寫入 Cloud Storage 的函數,則這些寫入將傳送至 Cloud Storage 模擬器(如果該模擬器正在執行)。如果這些寫入觸發更多函數,它們將在 Cloud Functions 模擬器中執行。

Firebase 身份驗證

如果您有使用 Firebase Admin SDK(版本 9.3.0 或更高版本)寫入 Firebase 驗證的函數,則這些寫入將會傳送到正在執行的 Auth 模擬器。如果這些寫入觸發更多函數,它們將在 Cloud Functions 模擬器中執行。

Firebase 託管

如果您使用 Cloud Functions為 Firebase Hosting 產生動態內容firebase emulators:start使用您的本機 HTTP 函數作為託管代理。

記錄

模擬器將日誌從您的函數傳輸到它們運行的終端視窗。它顯示函數內console.log()console.info()console.error()console.warn()語句的所有輸出。

下一步

有關使用 Firebase 模擬器套件的完整範例,請參閱測試快速入門範例