【问题标题】:how to properly initialize firebase functions test using cloud function?如何使用云功能正确初始化 Firebase 功能测试?
【发布时间】:2021-07-01 23:54:54
【问题描述】:

我正在使用

  • 节点 14
  • firebase 功能测试:0.2.3
  • firebase 管理员:9.6.0
  • firebase 功能:3.13.2
  • firebase 工具:9.8.0

所以我想使用 Firebase 云功能对我的 Firestore 触发功能执行单元测试,我阅读了来自 the documentation in here 的步骤。

我想使用Firebase Emulator 执行单元测试。所以我假设我会在离线模式下初始化 SDK。 the documentation

如果您想编写完全离线的测试,您可以 不带任何参数初始化SDK:

所以我这样初始化它

import * as firebase from "firebase-functions-test";

const test = firebase();
const wrapped = test.wrap(myFunctions.onCreate);

// rest of my test code

但是当我运行测试时,我有这个错误:

错误:无法加载默认凭据。浏览至 https://cloud.google.com/docs/authentication/getting-started 了解更多 信息。

所以即使我将使用 Firebase 模拟器(离线),我似乎也需要提供凭据,如果使用在线模式,这就是步骤,就像 documentation in here 中解释的那样

所以我像这样初始化 SDK

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

但是当我运行测试时,我又遇到了一个错误

{"severity":"WARNING","message":"警告,FIREBASE_CONFIG 和 GCLOUD_PROJECT 环境变量丢失。初始化 firebase-admin 将失败"}

错误:../../../service-account.json 中的文件不存在,或者 不是文件。 ENOENT:没有这样的文件或目录,lstat '/Users/xxx/Documents/service-account.json'

服务帐户 json 文件不存在?我相信我已经像这样正确设置了路径

其实我是用intellisense来引导我到service-account.json路径的

service-account.json 文件如下图所示,我从 firebase 项目概述 --> 项目设置 --> 服务帐户 --> 生成新私钥中获取它

如果我想在 Firebase 模拟器中初始化 firebase 函数测试 SDK 应该怎么做?

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    要使用离线模式,您需要存根 API 调用,例如 admin.initializeApp()。请参阅https://firebase.google.com/docs/functions/unit-testing#importing_your_functions中给出的示例

    // If index.js calls admin.initializeApp at the top of the file,
    // we need to stub it out before requiring index.js. This is because the
    // functions will be executed as a part of the require process.
    // Here we stub admin.initializeApp to be a dummy function that doesn't do anything.
    adminInitStub = sinon.stub(admin, 'initializeApp');
    // Now we can require index.js and save the exports inside a namespace called myFunctions.
    myFunctions = require('../index');
    

    要针对模拟器进行测试,您需要使用模拟器 URL 初始化测试 SDK。您的文件未找到错误可能是由于解析相对路径的问题。尝试提供绝对路径,看看是否可行。

    【讨论】:

      【解决方案2】:

      说明

      Error: The file at ../../../service-account.json does not exist
      

      该错误表示在运行时(启动模拟器时)找不到service-account.json文件,因为相对路径不正确。

      其实我是用intellisense来引导我到service-account.json路径

      intellisense 建议的路径可能不是正确的relative 到代码正在执行的目录。这是因为 typescript 是在运行之前编译的,通常是从内部编译的libdist 文件夹,这意味着需要更改相对路径。

      解决方案 1

      您可以使用绝对路径/my/absolutepath/to/service-account.json,这是解决此问题的快速方法,但不是很便携。

      解决方案 2

      用不同数量的../../ 进行实验,以找到service-account.json 的确切相对 路径。尝试使用:

      • ../../service-account.json
      • ../../../service-account.json
      • 等等...(直到它起作用)

      很可能只差几个../

      【讨论】:

      • 感谢本。我可以使用./service-account.json 解决它,这真的让我很困惑。也许我需要先了解更多关于相对路径和绝对路径的信息
      【解决方案3】:

      摆脱警告应该很简单,只需将以下内容添加到您的jest.preset.js(或在您的单元测试运行之前运行的任何代码)。

      const test = require('firebase-functions-test')();
      

      https://firebase.google.com/docs/functions/unit-testing#offline-mode

      【讨论】:

        猜你喜欢
        • 2021-01-23
        • 2020-11-29
        • 2017-12-21
        • 2020-01-05
        • 1970-01-01
        • 2018-06-23
        • 2018-11-25
        • 2018-08-09
        相关资源
        最近更新 更多