如何从客户端使用Firestore模拟器 [英] How to use firestore emulator from client

查看:116
本文介绍了如何从客户端使用Firestore模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在本地测试我的Firebase函数. 这些功能可以进行Firestore查询.

I want to test locally my firebase functions. These functions make firestore queries.

因此,我启动了仿真器firebase emulators:start,在我的客户端中,我使用了firebase.functions().useFunctionsEmulator('http://localhost:5001').

So i start the emulator firebase emulators:start and in my client i use firebase.functions().useFunctionsEmulator('http://localhost:5001').

当我在客户端中调用它们时,我的函数运行良好.我可以在Firestore模拟器中读取/写入数据.

My functions work well when i call them in my client. I can read/write data inside the firestore emulator.

问题:

我想直接在客户端内部读取Firestore模拟器数据,例如:

I want to read the firestore emulator data directly inside my client, like :

firebase.firestore().collection('tests').get().then(tests => {
    console.log( tests.docs.map(test=>test.map) )
})

但是我找不到如何在客户端中设置Firestore模拟器.

but i can't find how to set the firestore emulator inside my client.

这是我尝试过的:

1)Firestore设置

1) Firestore setting

firebase.firestore().settings({
    host:'http://localhost:8080',
    ssl:false
})

结果:

我在客户端控制台中获得了@firebase/firestore: Firestore (6.3.5): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds..

i get @firebase/firestore: Firestore (6.3.5): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds. inside my client console.

http请求返回未找到"

The http request returns 'Not found'

2)在我的firebaseConfig中设置模拟器网址

2) Set the emulator url inside my firebaseConfig

var firebaseConfig = {
    // ...
    databaseURL: "http://localhost:8080",
    // ...
}

firebase.initializeApp(firebaseConfig)

在这种情况下,远程服务器( https://firestore.googleapis.com..)请求.

in this case, the remote server (https://firestore.googleapis.com..) is requested.

所以我想设置以下两种情况之一:

So i want to setup one of these two cases :

1)在我的函数模拟器中使用远程Firestore

1) Using the remote firestore inside my functions emulators

OR

2)在我的客户端代码中使用本地Firestore模拟器.

2) Using the local firestore emulator inside my client code.

任何人都已经做到了吗?

Anyone has already done this ?

推荐答案

安装测试库

npm i -D @firebase/testing

在另一个终端上设置并启动仿真器:

firebase setup:emulators:firestore

firebase serve --only firestore

设置测试

const firebase = require("@firebase/testing");

// Helper function to setup test db
function authedApp(auth) {
  return firebase
    .initializeTestApp({ projectId: FIRESTORE_PROJECT_ID, auth })
    .firestore();
}

// Setup methods
beforeEach(async () => {
  // Clear the database between tests
  await firebase.clearFirestoreData({ projectId: FIRESTORE_PROJECT_ID });
});

// Clean up apps between tests.
afterEach(async () => {
  await Promise.all(firebase.apps().map(app => app.delete()));
});

运行测试

it("should retrieve correct item", async () => {
  // Init test db
  const db = authedApp(null);

  // Manually add item to collection
  const ref = await db.collection(COLLECTION_NAME).add({name: 'test item'});

  // Fetch item by id 
  const resp = await db.collection(COLLECTION_NAME).doc(ref.id).get();

  // test the output
  expect(resp).toBeDefined();
  expect(resp).toEqual(expect.objectContaining({name: 'test item'}));
});

当然,您的特定设置和情况会有所不同,但这至少应该为您提供一个总体思路.更多信息: https://firebase.google.com/docs/rules/unit-tests

Of course your particular setup and circumstances will differ, but this at least should give you a general idea. More info: https://firebase.google.com/docs/rules/unit-tests

来自"测试您的Cloud Firestore安全规则的注释'

写入Cloud Firestore模拟器的数据将保留在内存中,直到 仿真器已停止.如果仿真器连续运行,则可能 对测试隔离有影响.确保将数据写入一个 测试不在另一个中读取,或者使用以下命令显式清除您的数据 clearFirestoreData,或为每个分配不同的项目ID 独立测试:当您调用firebase.initializeAdminApp或 firebase.initializeTestApp,附加用户ID,时间戳或随机 projectID的整数.

Data written to the Cloud Firestore emulator is held in memory until the emulator is stopped. If the emulator is run continuously, this may have an impact on test isolation. To ensure that data written in one test is not read in another, either explicitly clear your data with clearFirestoreData, or assign a different project ID for each independent test: when you call firebase.initializeAdminApp or firebase.initializeTestApp, append a user ID, timestamp, or random integer to the projectID.

编辑:我写了一个博客帖子前一阵子,其中提供了有关该主题的更多详细信息.

I wrote a blog post a while back, which goes into more detail about the subject.

这篇关于如何从客户端使用Firestore模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆