Firebase 无法从云函数单元测试调用Firestore
在本地开发谷歌云功能。 尝试测试调用Firestore的函数。 这里是一个最小的例子Firebase 无法从云函数单元测试调用Firestore,firebase,google-cloud-firestore,jestjs,google-cloud-functions,Firebase,Google Cloud Firestore,Jestjs,Google Cloud Functions,在本地开发谷歌云功能。 尝试测试调用Firestore的函数。 这里是一个最小的例子 模拟器正在运行 从浏览器调用函数addMessage()时,该函数可以完全正常工作 函数失败,出现错误TypeError[ERR\u INVALID\u ARG\u TYPE]:“path”参数必须是string类型。从测试中调用时 问题:为什么会发生此错误?如何从测试中成功调用Firestore函数? 函数/index.js: require ('dotenv').config(); const fun
- 模拟器正在运行
- 从浏览器调用函数
时,该函数可以完全正常工作addMessage()
- 函数失败,出现错误
从测试中调用时TypeError[ERR\u INVALID\u ARG\u TYPE]:“path”参数必须是string类型。
- 问题:为什么会发生此错误?如何从测试中成功调用Firestore函数?
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.`);
});
函数/test/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);
});
});
从函数文件夹启动jest:
(除其他测试相关输出外):
通过将jest.config.js添加到函数/:
module.exports = {
testEnvironment: 'node'
};
基于的解决方案(但注意,我需要将jest.config.js放在functions/中,而不是放在项目根目录中)
他也试过,但似乎什么也没做
测试现在运行正常,但最后会出现一个笑话错误:
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提供的宝贵链接 通过将jest.config.js添加到函数/:
module.exports = {
testEnvironment: 'node'
};
基于的解决方案(但注意,我需要将jest.config.js放在functions/中,而不是放在项目根目录中)
他也试过,但似乎什么也没做
测试现在运行正常,但最后会出现一个笑话错误:
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提供的宝贵链接 考虑到堆栈跟踪的错误,我四处搜索,它似乎总是与
jest
版本相关。出于这个原因,你能按照上面提到的步骤尝试更新它的版本吗?如果它仍然不起作用,我相信查看另一篇文章可能会给你提供一些见解。感谢@gso_gabriel的帮助-第二个链接是赢家(见答案)。非常感谢:)你会考虑改变标题,指出你正试图从云函数(不直接从客户端)使用FixSt店。例如,“无法从云功能单元测试调用Firestore”。这与问题有关,当前的标题也吸引了不使用或不需要firebase-functions-test的人。考虑到堆栈跟踪的错误,我四处搜索,它似乎总是与jest
版本相关。出于这个原因,你能按照上面提到的步骤尝试更新它的版本吗?如果它仍然不起作用,我相信查看另一篇文章可能会给你提供一些见解。感谢@gso_gabriel的帮助-第二个链接是赢家(见答案)。非常感谢:)你会考虑改变标题,指出你正试图从云函数(不直接从客户端)使用FixSt店。例如,“无法从云功能单元测试调用Firestore”。这与问题有关,当前的标题也吸引了不使用或不需要firebase功能测试的人。