如何将 Firestore 模拟器与我的 Firebase 功能测试连接起来?

Bas*_*ein 9 firebase firebase-tools google-cloud-functions google-cloud-firestore

目前,我们正在在线模式下使用“firebase-functions-test”来测试我们的 firebase 功能(如此处所述 https://firebase.google.com/docs/functions/unit-testing),我们设置如下:

//setupTests.ts
import * as admin from 'firebase-admin';

const serviceAccount = require('./../test-service-account.json');

export const testEnv = require('firebase-functions-test')({
    projectId: 'projectId',
    credential: admin.credential.cert(serviceAccount),
    storageBucket: 'projectId.appspot.com'
});
const testConfig = {
    dropbox: {
        token: 'dropboxToken',
        working_dir: 'someFolder'
    }
};

testEnv.mockConfig(testConfig);

// ensure default firebase app exists:
try {
    admin.initializeApp();
} catch (e) {}
Run Code Online (Sandbox Code Playgroud)

我们希望在我们的测试中不再针对实际的 firestore 实例进行测试,而是使用模拟器。

我在网上找到的文档、问题和示例要么已经过时,要么描述了如何设置模拟器以测试安全规则或 Web 前端。

尝试使用 firebase.initializeAdminApp({ projectId: "my-test-project" });没有成功。

我也试过设置 FIRESTORE_EMULATOR_HOST=[::1]:8080,127.0.0.1:8080

所以问题是:如何在我的测试中初始化 firebaseApp,以便我的功能连接到 firestore 模拟器?

Bas*_*ein 6

一年多后的今天,我又一次破解了它,所以有些事情发生了变化,我无法全部列出。这是对我有用的:

1. 安装并运行最新版本的 firebase-tools 和模拟器:

$ npm i -g firebase-tools   // v9.2.0 as of now
$ firebase init emulators

# You will be asked which emulators you want to install. 
# For my purposes, I found the firestore and auth emulators to be sufficient

$ firebase -P <project-id> emulators:start --only firestore,auth
Run Code Online (Sandbox Code Playgroud)

记下您的模拟器可用的端口:

控制台输出

2. 测试设置

此文件的目的是作为依赖模拟器的测试的设置。这是我们让我们的应用程序知道在哪里可以找到模拟器的地方。

$ npm i -g firebase-tools   // v9.2.0 as of now
$ firebase init emulators

# You will be asked which emulators you want to install. 
# For my purposes, I found the firestore and auth emulators to be sufficient

$ firebase -P <project-id> emulators:start --only firestore,auth
Run Code Online (Sandbox Code Playgroud)

3. 测试一个简单的函数

为此,我们设置了一个简单的脚本,用于将文档写入 firestore。在测试中,只有在我们运行该函数之后,才断言该文档存在于模拟器中。

// setupFunctions.ts
import * as admin from 'firebase-admin';

// firebase automatically picks up on these environment variables:
process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8080';
process.env.FIREBASE_AUTH_EMULATOR_HOST = 'localhost:9099';

admin.initializeApp({
    projectId: 'project-id',
    credential: admin.credential.applicationDefault()
});

export const testEnv = require('firebase-functions-test')();
Run Code Online (Sandbox Code Playgroud)
// myFunction.ts
import * as functions from 'firebase-functions';
import {firestore} from 'firebase-admin';

export const myFunction = functions
    .region('europe-west1')
    .runWith({timeoutSeconds: 540, memory: '2GB'})
    .https.onCall(async () => {
        await firestore()
            .collection('myCollection')
            .doc('someDoc')
            .set({hello: 'world'});

        return {result: 'success'};
    });
Run Code Online (Sandbox Code Playgroud)

果然,在运行测试之后,我可以观察到数据存在于 firestore 模拟器中。在模拟器运行时访问 http://localhost:4000/firestore 以获取此视图。

显示已创建文档的 webui 屏幕截图