【问题标题】:Cannot call Firestore from Cloud Functions unit tests无法从 Cloud Functions 单元测试调用 Firestore
【发布时间】:2020-10-15 09:58:09
【问题描述】:

在本地开发谷歌云功能。 尝试测试调用 Firestore 的函数。 这是一个最小的例子。

  • 模拟器正在运行。
  • 函数addMessage() 从浏览器调用时完全正常。
  • 从测试中调用该函数失败并出现错误TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string.
  • 问题:为什么会出现此错误,如何从测试中成功调用 Firestore 函数?

函数/index.js:

require ('dotenv').config();

const functions = require ('firebase-functions');
const admin = require ('firebase-admin');
admin.initializeApp();

exports.addMessage = functions.https.onRequest (async (req, res) => {
  const original = req.query.text;
  const writeResult = await admin.firestore().collection ('messages').add ({text: original});
  const docSnap = await writeResult.get();
  const writtenText = docSnap.get ('text');
  res.send (`Message with text: ${writtenText} added.`);
});

函数/测试/index.test.js:

const admin = require ('firebase-admin');
const firebase_functions_test = require ('firebase-functions-test')({
  projectId: 'my-project-id'
}, '/path/to/google-application-credentials.json');
const testFunctions = require ('../index.js');

describe ('addMessage()', () => {
  it ('returns Message with text: Howdy added.', (done) => {
    const req = {query: {text: 'Howdy'} };
    const res = {
      send: (body) => {
        expect (body).toBe (`Message with text: Howdy added.`);
        done();
      }
    };
    testFunctions.addMessage (req, res);
  });
});

从函数文件夹开始开玩笑:

(在其他与测试相关的输出中):

FAIL  test/index.test.js
  ● addMessage() › returns Message with text: Howdy added.

    TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object

       7 | exports.addMessage = functions.https.onRequest (async (req, res) => {
       8 |   const original = req.query.text;
    >  9 |   const writeResult = await admin.firestore().collection ('messages').add ({text: original});
         |                                                                       ^
      10 |   const docSnap = await writeResult.get();
      11 |   const writtenText = docSnap.get ('text');
      12 |   res.send (`Message with text: ${writtenText} added.`);

      at GrpcClient.loadProto (node_modules/google-gax/src/grpc.ts:166:23)
      at new FirestoreClient (node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:118:38)
      at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (node_modules/@google-cloud/firestore/build/src/index.js:326:26)
      at ClientPool.acquire (node_modules/@google-cloud/firestore/build/src/pool.js:87:35)
      at ClientPool.run (node_modules/@google-cloud/firestore/build/src/pool.js:164:29)
      at Firestore.request (node_modules/@google-cloud/firestore/build/src/index.js:983:33)
      at WriteBatch.commit_ (node_modules/@google-cloud/firestore/build/src/write-batch.js:496:48)
      Caused by: Error:
      at WriteBatch.commit (node_modules/@google-cloud/firestore/build/src/write-batch.js:415:23)
      at DocumentReference.create (node_modules/@google-cloud/firestore/build/src/reference.js:283:14)
      at CollectionReference.add (node_modules/@google-cloud/firestore/build/src/reference.js:2011:28)
      at Object.<anonymous>.exports.addMessage.functions.https.onRequest (index.js:9:71)
      at Object.addMessage (node_modules/firebase-functions/lib/providers/https.js:50:16)
      at Object.done (test/index.test.js:17:19)

环境:

"node": "10"
"firebase-admin": "^8.12.1",
"firebase-functions": "^3.7.0"
"firebase-functions-test": "^0.2.1",
"jest": "^25.5.4"

【问题讨论】:

  • 考虑到堆栈跟踪的错误,我四处搜索,似乎总是与jest 版本有关。出于这个原因,您能否按照here 中提到的步骤尝试更新其版本?如果它仍然不起作用,我相信查看此其他帖子 here 可能会为您提供一些见解。
  • 感谢@gso_gabriel 的帮助 - 第二个链接是获胜者(见答案)。非常感谢:)
  • 您是否考虑更改标题以指出您正在尝试从 Cloud Functions 使用 Firestore(而不是直接从客户端)。例如。 “无法从 Cloud Function 单元测试调用 Firestore”。它与问题相关,当前标题也吸引了不使用或不需要 firebase-functions-test 的人。

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


【解决方案1】:

在functions/中加入jest.config.js解决:

module.exports = {
    testEnvironment: 'node'
};

基于this 的解决方案(但请注意,我需要将 jest.config.js 放在 functions/ 中,而不是项目根目录中)。

也尝试了this,但似乎什么也没做。

测试现在可以完美运行,只是它们以 Jest 错误结束:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

这似乎是无害的。

感谢@gso_gabriel 提供有价值的链接!

【讨论】:

    猜你喜欢
    • 2020-03-01
    • 2020-02-01
    • 1970-01-01
    • 2020-08-21
    • 2020-06-16
    • 2019-02-15
    • 2020-07-28
    • 2018-03-23
    • 2018-06-01
    相关资源
    最近更新 更多