如何通过Firebase功能测试连接Firestore模拟器?

时间:2019-07-24 21:28:07

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

当前,我们正在在线模式下使用“ 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 个答案:

答案 0 :(得分:1)

一年多以后,今天我又遇到了麻烦,所以有些事情已经改变,我无法一一列举。这是对我有用的东西:

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

$ npm i -g firebase-tools   // v8.16.2 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

记下仿真器可用的端口:

console_output

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

screenshot of webui showing the created document