简体   繁体   English

如何设置用于单元测试的测试套件 Firebase 与模拟器连接的功能,特别是 Firestore 模拟器?

[英]How to setup a test suit for unit testing Firebase Functions that connect with emulators, specifically the Firestore emulator?

I want to set up a test suit to write a unit test for my Firebase application which connects to the Firestore emulator that is running locally on port 8080我想设置一个测试套件来为我的 Firebase 应用程序编写一个单元测试,该应用程序连接到在端口 8080 上本地运行的 Firestore 模拟器

Here is my firebase configuration in index.js这是我在index.js中的 firebase 配置

    const functions = require("firebase-functions");
    const dotenv = require('dotenv');
    dotenv.config();

    const emailFormatter = require("./emailFormatter");

    const admin = require("firebase-admin");

    admin.initializeApp();
    const db = admin.firestore();

And the following is my functions.test.js以下是我的functions.test.js

    const admin = require('firebase-admin');
    const serviceAccount = require('../../../fir-react-mailer-firebase-adminsdk-wfei1-0fff68d44e.json');

    const testConfig = {
       credential: admin.credential.cert(serviceAccount),
       databaseURL: 'http://localhost:8080'
    }

    const testApp = admin.initializeApp(testConfig, 'test');

    const projectConfig = {
       projectId: 'fir-react-mailer',
       databaseURL: 'http://localhost:8080',
    }

    const testF = require('firebase-functions-test')(projectConfig, serviceAccount);

    const myFinctions = require('../index')

    let db = admin.firestore(testApp)

Here is my package.json这是我的package.json

    {
      "name": "functions",
      "description": "Cloud Functions for Firebase",
      "scripts": {
        "serve": "firebase emulators:start --only functions",
        "shell": "firebase functions:shell",
        "start": "npm run shell",
        "deploy": "firebase deploy --only functions",
        "logs": "firebase functions:log",
        "test": "jest"
      },
      "engines": {
        "node": "12"
      },
      "dependencies": {
        "chalk": "^4.1.0",
        "dotenv": "^8.2.0",
        "firebase-admin": "^8.9.0",
        "firebase-functions": "^3.3.0",
        "nodemailer": "^6.4.6"
      },
      "devDependencies": {
        "@types/jest": "^26.0.20",
        "firebase-functions-test": "^0.1.7",
        "jest": "^26.6.3"
      },
      "private": true
    }

When running the command npm run test, I am getting the following error on the console.运行命令 npm 运行测试时,我在控制台上收到以下错误。

在此处输入图像描述

Please suggest to me, If I am doing something wrong.请向我建议,如果我做错了什么。 I am a newbie to Firebase, so this whole setup is confusing.我是 Firebase 的新手,所以整个设置令人困惑。

I was having this issue as well and it was because I had to provide the location of the serviceAccount json file in relation to the functions folder.我也遇到了这个问题,这是因为我必须提供与函数文件夹相关的 serviceAccount json 文件的位置。 I also didn't import it and instead provided the string.我也没有导入它,而是提供了字符串。 So if my project is like so:所以如果我的项目是这样的:

functions
 |
 +-- src
 |    
 +-- test
 |  |  
 |  \-- functions.test.js
 |    
 +-- package.json
 +-- service-account.json

I would change your functions-test variable to:我会将您的函数测试变量更改为:

const testF = require('firebase-functions-test')(projectConfig, "./service-account.json");

You might also try calling initializeApp only once.您也可以尝试只调用一次 initializeApp。 It looks like you're calling initializeApp() twice.看起来您正在调用 initializeApp() 两次。 The first one is in your test code at line 9 and then again in your actual code.第一个在第 9 行的测试代码中,然后在您的实际代码中。 The one from the test code looks right, but then your actual code is calling it again without the testConfig.测试代码中的那个看起来是对的,但是你的实际代码在没有 testConfig 的情况下再次调用它。

Maybe try wrapping admin.initializeApp() in an if statement so it doesn't get initialized unless it has to (which will occur in production).也许尝试将admin.initializeApp()包装在 if 语句中,这样它就不会被初始化,除非它必须这样做(这将在生产中发生)。

if (admin.apps.length === 0) {
  admin.initializeApp();
}

As per the documentation :根据文档

After creating your service account, you need to download the service account key to your machine(s) where your application runs.创建服务帐户后,您需要将服务帐户密钥下载到运行应用程序的计算机上。 You can either use the GOOGLE_APPLICATION_CREDENTIALS environment variable or write code to pass the service account key to the client library.您可以使用GOOGLE_APPLICATION_CREDENTIALS环境变量或编写代码将服务帐户密钥传递给客户端库。

You need to set the Windows environment variables like this:您需要像这样设置 Windows 环境变量:

With PowerShell:使用 PowerShell:

$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\my-key.json"

For example:例如:

$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\my-key.json"

With command prompt:使用命令提示符:

set GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\my-key.json"

Try to avoid hard coding the location of your credentials file if you can to reduce the amount of code you write and it means your code is reusable.如果可以的话,尽量避免对凭据文件的位置进行硬编码,以减少编写的代码量,这意味着您的代码是可重用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将测试数据从生产中导入 Firebase Firestore 模拟器以便于测试? - How to import test data into Firebase Firestore emulator from production for easy testing? 测试时连接firebase模拟器 - Connect to firebase emulator during test 如何将依赖项注入 Firebase/Google Cloud Functions? (单元和集成测试) - How to inject dependencies into Firebase/Google Cloud Functions? (unit & integration testing) 如何连接到 Firebase Auth 模拟器 - How to connect to Firebase Auth emulator Firebase Firestore 模拟器如何工作? - How does Firebase Firestore emulator work? 在本地功能 firebase 仿真器中使用实时/在线 Firestore - Use live/online firestore in local Functions firebase emulator 当我使用 firebase 模拟器和 next.js 时如何使用 firebase-admin 获取 Firestore 数据 - how to get firestore data with firebase-admin when I use firebase emulators and next.js 如何在Firebase Functions中与Firestore + pdfmake一起使用Promise - How to work with promise with firestore + pdfmake in Firebase Functions 如何使用 Firebase 模拟器测试云功能 - How to test cloud function using firebase emulator 如何在Docker中为nodejs应用程序设置单元测试? - How to setup unit test in Docker for nodejs application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM