Graf Graf - 1 month ago 31
Javascript Question

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. To get familiar with all it's API's I thought of writing some learning tests with 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. So why can't I use Firebase to test some API calls.

This is my
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"
}
}

Answer

The problem is that installing and configuring the "jest-react-native" and "react-native" presets breaks our ability to "import" firebase! The fix is to require firebase (not import) in your test.js file, and add a line that imports react-native. Without importing react-native in addition to require('firebase'), firebase will not be included in the test file! Here is my github repo that walks you through the steps that reproduce the error, and how to fix it.

There may also be a stale jest cache which may require you to run

./node_modules/jest/bin/jest.js --no-cach

This seems to happen everytime I change anything to do with a preprocessor option.

See jest documentation on caching issues.