如何使用云函数正确初始化firebase函数测试? [英] how to properly initialize firebase functions test using cloud function?

查看:45
本文介绍了如何使用云函数正确初始化firebase函数测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

  • 节点14
  • firebase-functions-test:0.2.3
  • firebase-admin:9.6.0
  • firebase-functions: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

使用不同数量的 ../../进行实验,以找到服务帐户的确切 相对 路径.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆