[英]Jest + Firebase: TypeError: Cannot read property 'defineProperties' of undefined
I'm currently working on an react native project and wanted to utilise the Firebase JS SDK. 我目前正在开发一个react本机项目,并希望使用Firebase JS SDK。 To get familiar with all it's API's I thought of writing some learning tests with Jest.
为了熟悉它的所有API,我想到了用Jest编写一些学习测试。 This is my test:
这是我的测试:
import Firebase from 'firebase';
describe('Firebase', function() {
it('should work', function() {
expect(null).toBeNull()
})
});
Unfortunately I'm getting this error: ● Test suite failed to run 不幸的是我收到了这个错误:●测试套件无法运行
TypeError: Cannot read property 'defineProperties' of undefined
at node_modules/firebase/app-node.js:18:234
at Object.<anonymous> (node_modules/firebase/app-node.js:23:260)
at Object.<anonymous> (node_modules/firebase/firebase-node.js:8:16)
at Object.<anonymous> (firebase.test.js:1:104)
If I replace the import state with import React from 'react'
everything works fine. 如果我用
import React from 'react'
替换import React from 'react'
的导入状态一切正常。 So why can't I use Firebase to test some API calls. 那么为什么我不能使用Firebase来测试一些API调用。
This is my package.json
: 这是我的
package.json
:
{
...,
"dependencies": {
"firebase": "^3.5.0",
"react": "15.3.2",
"react-native": "0.35.0"
},
"jest": {
"preset": "jest-react-native"
},
"devDependencies": {
"babel-jest": "16.0.0",
"babel-preset-react-native": "1.9.0",
"jest": "16.0.1",
"jest-react-native": "16.0.0",
"react-test-renderer": "15.3.2"
}
}
The problem is that installing and configuring the "jest-react-native" and "react-native" presets breaks our ability to "import" firebase! 问题是安装和配置“jest-react-native”和“react-native”预设会破坏我们“导入”firebase的能力! The fix is to
require
firebase ( not import
) in your test.js file, and add a line that imports react-native
. 修复是在test.js文件中
require
firebase( 而不是 import
),并添加一行导入react-native
。 Without import
ing react-native
in addition to require('firebase')
, firebase will not be included in the test file! 除了
require('firebase')
之外 ,如果没有import
react-native
,firebase将不会包含在测试文件中! Here is my github repo that walks you through the steps that reproduce the error, and how to fix it. 这是我的github repo ,它将引导您完成重现错误的步骤,以及如何解决它。
There may also be a stale jest cache which may require you to run 可能还有一个陈旧的jest缓存,可能需要您运行
./node_modules/jest/bin/jest.js --no-cach
./node_modules/jest/bin/jest.js --no-cach
This seems to happen everytime I change anything to do with a preprocessor option. 每当我改变与预处理器选项有关的任何事情时,这似乎都会发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.