【问题标题】:TypeError in "path" when running Firebase Functions Test运行 Firebase 函数测试时出现“路径”中的 TypeError
【发布时间】:2020-01-28 19:20:51
【问题描述】:

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

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 编写离线测试。

【问题讨论】:

  • 您的代码似乎不完整或不正确。请编辑问题以明确docUrl 是什么,因为该变量没有定义。你的意思是doc?它的价值是什么?
  • docUrl 是 url,这是我们在 message 中收到的属性之一。我已经更新了代码以反映这一点。

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


【解决方案1】:

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

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

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

【讨论】:

  • 我没有尝试在本地部署,我正在尝试使用 firebase-functions-test 编写测试。它调用的 firestore_client 中的错误是 const protos = gaxGrpc.loadProto(opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath);
  • 我的意思不一定是在本地部署,而是在本地测试然后部署。
【解决方案2】:

在根目录下添加“jest.config.js”,将以下代码保存到文件中

module.exports = {
    testPathIgnorePatterns: ['lib/', 'node_modules/'],
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    testEnvironment: 'node'
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    相关资源
    最近更新 更多