我正在为谷歌云函数编写一个测试,它将向 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/