如何使用云功能正确初始化 Firebase 功能测试? [英] how to properly initialize firebase functions test using cloud function?
问题描述
我正在使用
- 节点 14
- firebase 功能测试:0.2.3
- firebase 管理员:9.6.0
- firebase 功能:3.13.2
- firebase 工具:9.8.0
所以我想使用 firebase 云功能为我的 firestore 触发器功能执行单元测试,我阅读了
其实我是用intellisense来引导我到service-account.json路径的
service-account.json 文件如下图所示,我从 firebase 项目概述中得到它 -->项目设置 -->服务帐户 -->生成新的私钥
如果我想在 Firebase 模拟器中初始化 firebase 函数测试 SDK 应该怎么做?
说明
错误:../../../service-account.json 中的文件不存在
该错误表示在运行时(启动模拟器时)找不到 service-account.json
文件,因为相对路径不正确.
其实我是用intellisense来引导我到service-account.json路径
intellisense 建议的路径可能不是正确的relative 到代码正在执行的目录.这是因为 typescript 是在运行之前编译的,通常是从内部编译的lib
或 dist
文件夹,表示需要更改相对路径.
解决方案 1
您可以使用绝对路径/my/absolutepath/to/service-account.json
,这是解决此问题的快速方法,但不是很便携.
解决方案 2
用不同数量的 ../../
进行实验,以找到 service-account 的确切 相对 路径.json
.尝试使用:
../../service-account.json
../../../service-account.json
- 等等……(直到它起作用)
它很可能只有几个 ../
I am using
- Node 14
- firebase-functions-test: 0.2.3
- firebase-admin: 9.6.0
- firebase-functions: 3.13.2
- firebase tools: 9.8.0
so I want to perform unit testing for my firestore trigger function using firebase cloud function, I read the steps from the documentation in here.
I want to perform unit test using Firebase Emulator
. so I assume I will initialize the SDK in offline mode. the documentation said that
If you would like to write completely offline tests, you can initialize the SDK without any parameters:
so I initialize it like this
import * as firebase from "firebase-functions-test";
const test = firebase();
const wrapped = test.wrap(myFunctions.onCreate);
// rest of my test code
but when I run the test, I have this error:
Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
so even though I will use Firebase emulator (offline), it seems I need to provide the credential, which is the step if online mode is used, like explained from documentation in here
so I initialize the SDK like this
import * as firebase from "firebase-functions-test";
const test = firebase({
databaseURL: "https://xxx-b843e.firebaseio.com",
projectId: "xxx-b843e",
}, "../../../service-account.json");
const wrapped = test.wrap(myFunctions.onCreate);
// rest of my test code
but when I run the test, I have another error
{"severity":"WARNING","message":"Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail"}
Error: The file at ../../../service-account.json does not exist, or it is not a file. ENOENT: no such file or directory, lstat '/Users/xxx/Documents/service-account.json'
the service account json file doesn't exist? I believe I have set the path correctly like this
in fact, I use intellisense to guide me to service-account.json path
the service-account.json file is like the image below, i get it from firebase project overview --> project settings --> service accounts --> generate new private key
what should I do if I want to initialize firebase functions test SDK in Firebase emulator?
Explanation
Error: The file at ../../../service-account.json does not exist
That error means that the service-account.json
file could not be found at runtime (when you started the emulator), because the relative path is incorrect.
in fact, I use intellisense to guide me to service-account.json path
The path that intellisense suggests may not be the correct relative to the directory the code is executing in. This is because typescript is compiled before it's run, usually from within a lib
or dist
folder, which means the relative path needs to be changed.
Solution 1
You can use an absolute path /my/absolutepath/to/service-account.json
, this is a fast way to resolve this problem, but isn't very portable.
Solution 2
Experiment with different amounts of ../../
to find the exact relative path of the service-account.json
. Try using:
../../service-account.json
../../../service-account.json
- etc ... (until it works)
It's most likely only off by a few ../
这篇关于如何使用云功能正确初始化 Firebase 功能测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!