如何使用我的 firebase 功能测试连接 firestore 模拟器?

How do I wire up the firestore emulator with my firebase functions tests?

目前我们正在在线模式下使用 '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) {}

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

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

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

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

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

一年多后的今天,我再次尝试了它,所以有些事情发生了变化,我无法一一列举。这是对我有用的:

1。安装并 运行 最新版本的 firebase 工具和模拟器:

$ 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

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

2。测试设置

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

// 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')();

3。测试一个简单的函数

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

// 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'};
    });
// myTest.ts
// import testEnv first, to ensure that emulators are wired up
import {testEnv} from './setupFunctions';
import {myFunction} from './myFunction';
import * as admin from 'firebase-admin';

// wrap the function
const testee = testEnv.wrap(myFunction);

describe('myFunction', () => {
    it('should add hello world doc', async () => {
        // ensure doc does not exist before test
        await admin
          .firestore()
          .doc('myCollection/someDoc')
          .delete()

        // run the function under test
        const result = await testee();

        // assertions
        expect(result).toEqual({result: 'success'});

        const doc = await admin
            .firestore()
            .doc('myCollection/someDoc')
            .get();
        expect(doc.data()).toEqual({hello: 'world'});
    });
});

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