如何从客户端使用 Firestore 模拟器

Ant*_*MME 12 javascript firebase firebase-tools google-cloud-functions google-cloud-firestore

我想在本地测试我的 firebase 功能。这些函数进行 firestore 查询。

所以我启动模拟器firebase emulators:start并在我的客户端中使用firebase.functions().useFunctionsEmulator('http://localhost:5001').

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

问题 :

我想直接在我的客户端中读取 firestore 模拟器数据,例如:

firebase.firestore().collection('tests').get().then(tests => {
    console.log( tests.docs.map(test=>test.map) )
})
Run Code Online (Sandbox Code Playgroud)

但我找不到如何在我的客户端中设置 firestore 模拟器。

这是我尝试过的:

1) Firestore 设置

firebase.firestore().settings({
    host:'http://localhost:8080',
    ssl:false
})
Run Code Online (Sandbox Code Playgroud)

结果 :

@firebase/firestore: Firestore (6.3.5): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.进入我的客户端控制台。

http 请求返回“未找到”

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

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

firebase.initializeApp(firebaseConfig)

Run Code Online (Sandbox Code Playgroud)

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

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

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

或者

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

有人已经这样做了吗?

Cla*_*ity 11

安装测试库

npm i -D @firebase/testing
Run Code Online (Sandbox Code Playgroud)

在另一个终端中设置并启动模拟器:

firebase setup:emulators:firestore

firebase serve --only firestore
Run Code Online (Sandbox Code Playgroud)

设置测试

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()));
});
Run Code Online (Sandbox Code Playgroud)

运行测试

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'}));
});
Run Code Online (Sandbox Code Playgroud)

当然,您的特定设置和情况会有所不同,但这至少应该给您一个大致的概念。更多信息:https : //firebase.google.com/docs/rules/unit-tests

测试您的 Cloud Firestore 安全规则”中的注释

写入 Cloud Firestore 模拟器的数据会保留在内存中,直到模拟器停止。如果模拟器连续运行,这可能会影响测试隔离。为确保在一个测试中写入的数据不会在另一个测试中读取,请使用 明确清除您的数据 clearFirestoreData,或为每个独立测试分配不同的项目 ID:当您调用 firebase.initializeAdminApp 或 firebase.initializeTestApp 时,附加用户 ID、时间戳、或随机整数到项目ID。

编辑:我不久前写了一篇博客文章,其中详细介绍了该主题。


Ant*_*MME 2

好吧,我找到了怎么做:

1) 在本地启动函数模拟器:

set GOOGLE_APPLICATION_CREDENTIALS=./privatekey.json && firebase serve --only functions
Run Code Online (Sandbox Code Playgroud)

2)然后是客户端:

if (process.env.NODE_ENV === 'development') {
    firebase.functions().useFunctionsEmulator('http://localhost:5001')
}
Run Code Online (Sandbox Code Playgroud)

  • 那么Firestore模拟器和测试数据呢?您直接访问生产服务器获取数据库吗? (2认同)