【问题标题】:How do I wire up the firestore emulator with my firebase functions tests?如何将 firestore 模拟器与我的 firebase 功能测试连接起来?
【发布时间】:2019-12-03 02:44:03
【问题描述】:

目前我们在在线模式下使用“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 模拟器?

【问题讨论】:

  • 严格来说,如果一个单元测试访问任何外部资源,甚至是一个模拟器,它就不再是一个单元测试——它是一个集成测试。通常,您将希望模拟任何外部依赖项,以便测试真正隔离(也称为密封)。如果您想使用模拟器执行集成测试,您可能不想将它们与单元测试结合起来,单元测试应该是封闭的。
  • 如果您对完整的仿真器体验感兴趣(不涉及单元测试或 firebase-functions-test),您应该研究新的仿真器套件。 firebase.google.com/docs/functions/local-emulator
  • 我编辑了这个问题,不再提及单元测试,因为你说的是​​正确的。您链接的模拟器正是我们想要开始工作的,因此问题在于如何将它与我们的测试联系起来。
  • @BastianStein 你有没有想过如何做到这一点

标签: firebase google-cloud-firestore google-cloud-functions firebase-tools


【解决方案1】:

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

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

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

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 以获取此视图。

【讨论】:

  • 哇,好痛苦。我可以确认这是有效的
  • 仅供参考,无需包含任何身份验证或凭据设置即可使其正常工作。
  • 感谢您的逐步说明。 -P &lt;project-id&gt; 对我有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 2020-09-05
  • 2021-06-10
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多