javascript - 运行 Firebase 函数测试时出现 "path"中的类型错误

标签 javascript node.js google-cloud-firestore google-cloud-functions

我正在为谷歌云函数编写一个测试,它将向 Firestore 数据库写入一些信息。该测试使用 firebase-functions-test 和 jest。我正在编写的函数在部署时可以成功运行,但是当我尝试运行测试时,我得到:

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

      at GrpcClient.loadProto (node_modules/google-gax/src/grpc.ts:182:23)
      at new FirestoreClient (node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:113:32)
      at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (node_modules/@google-cloud/firestore/build/src/index.js:319:26)
      at ClientPool.acquire (node_modules/@google-cloud/firestore/build/src/pool.js:81:35)
      at ClientPool.run (node_modules/@google-cloud/firestore/build/src/pool.js:155:29)
      at Firestore.request (node_modules/@google-cloud/firestore/build/src/index.js:885:33)
      at WriteBatch.commit_ (node_modules/@google-cloud/firestore/build/src/write-batch.js:450:14)

我的功能:

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

const saveCheckup = functions.pubsub.topic('save-test').onPublish((message) => {
  const {url, ...dataToSave} = message.attributes;

  let current = db.collection('current').doc(url);
  current.set(dataToSave, {merge: true})

  return true;
});

module.exports = saveCheckup;

我的测试:

import * as admin from 'firebase-admin';

const testEnv = require('firebase-functions-test')(
  {
    databaseURL: "https://my-project.firebaseio.com",
    projectId: 'my-project',
    storageBucket: 'my-project.appspot.com'
  }, "./my-project-firebase-adminsdk.json"
);


describe('saveCheckup', () => {
  let adminStub, saveCheckup;

  beforeAll(() => {
    adminStub = jest.spyOn(admin, "initializeApp");
    saveCheckup = require('../functions/save_checkup');
  });

  afterAll(() => {
    adminStub.mockRestore();
    testEnv.cleanup();
    admin.database().ref("current").remove();
  });

  it("should save the user", async () => {
    const wrapped = testEnv.wrap(saveCheckup);

    await wrapped({attributes: {
      date: "test date",
      url: "testurl",
      status: "200"
    }});

    const record = await admin.database().ref('/current/testurl').once('value');
    expect(record.val()).toHaveProperty("status", "200");
  })
});

更新:我们无法解决这个问题,最终只是为 firestore 编写离线测试。

最佳答案

您发布的错误输出显示该错误位于 Google Firebase Node 模块文件内。 它甚至显示了行和字符的位置:

at new FirestoreClient (node_modules/.../v1/firestore_client.js:113:32)
//                                             Error location here ^

如果您尝试在本地部署,请阅读以下内容:https://firebase.google.com/docs/hosting/deploying 并根据您的情况按照说明进行操作。

关于javascript - 运行 Firebase 函数测试时出现 "path"中的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59955660/

相关文章:

javascript - 为什么我在通过 mochai 和 chai 测试时没有通过 throwing-error 测试?

javascript - AJAX 请求已提交,但未收到 POST 值

node.js - 无法从 ionic-native 安装 ‘File’ - 依赖项相关?

google-cloud-platform - "Cloud Datastore indexes"和 "Cloud Firestore indexes"和有什么区别?

javascript - 如何在 Typescript 界面中强制使用分号

javascript - 无法从 Controller 检索数据

mysql - 在sequelize中创建具有循环依赖的表,

node.js - 如何从 express 、参数和查询中的 url 获取 id 似乎不起作用

swift - Firebase Firestore + Cloud Function 服务器端验证应用内购买收据(Swift + NodeJS)

firebase - 更新 cloud firestore : The operator '[]' isn't defined for the type 'Object' . 后尝试定义操作符 '[]'