[英]I can't test firebase functions because of `TypeError: Cannot read properties of undefined (reading 'DataSnapshot')`
I would like to run unit tests on firebase functions.我想在 firebase 函数上运行单元测试。 I created functions/test/index.test.js with the intention of testing in offline mode, and the current file content is as follows:
我创建了functions/test/index.test.js,目的是离线测试,当前文件内容如下:
// The content is only one line.
const test = require("firebase-functions-test")();
When I run jest with the above file, I get the following error:当我使用上述文件运行 jest 时,出现以下错误:
$ jest
FAIL test/index.test.js
● Test suite failed to run
TypeError: Cannot read properties of undefined (reading 'DataSnapshot')
> 1 | const test = require("firebase-functions-test")();
| ^
2 |
at Object.<anonymous> (node_modules/firebase-functions-test/lib/cloudevent/generate.js:26:45)
at Object.<anonymous> (node_modules/firebase-functions-test/lib/v2.js:25:20)
at Object.<anonymous> (node_modules/firebase-functions-test/lib/main.js:26:14)
at Object.<anonymous> (node_modules/firebase-functions-test/lib/features.js:4:16)
at Object.<anonymous>.module.exports (node_modules/firebase-functions-test/lib/index.js:30:20)
at Object.<anonymous> (test/index.test.js:1:1)
Something does not seem to be set correctly before the test error.在测试错误之前似乎没有正确设置某些东西。 What should I do?
我应该怎么办?
I ran npm ls firebase-functions
and observed the following warning.我运行
npm ls firebase-functions
并观察到以下警告。
functions@ /path/to/project/functions
├─┬ firebase-functions-test@2.4.0
│ └── firebase-functions@3.20.0 deduped invalid: ">=3.23.0" from node_modules/firebase-functions-test
└── firebase-functions@3.20.0 invalid: ">=3.23.0" from node_modules/firebase-functions-test
npm ERR! code ELSPROBLEMS
npm ERR! invalid: firebase-functions@3.20.0 /path/to/project/functions/node_modules/firebase-functions
npm ERR! A complete log of this run can be found in:
npm ERR! /home/my_home/.npm/_logs/2022-09-18T09_24_27_208Z-debug-0.log
So I ran yarn add firebase-functions@3.23.0
.所以我跑了
yarn add firebase-functions@3.23.0
。 Then, when I run the test, I no longer get the error that I got when I asked the question.然后,当我运行测试时,我不再收到我提出问题时遇到的错误。 Apparently, it was a version compatibility issue.
显然,这是一个版本兼容性问题。 Thank you.
谢谢你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.