Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
  }
}
like image 218
dennis-tra Avatar asked Oct 19 '16 20:10

dennis-tra


People also ask

What is cannot read property of undefined in JavaScript?

The TypeError: Cannot read property of undefined is one of the most common type errors in JavaScript. It occurs when a property is read or a function is called on an undefined variable. TypeError: Cannot read properties of undefined (reading x) Undefined means that a variable has been declared but has not been assigned a value.

What is the meaning of undefined in JavaScript?

Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined.

Is dotnetify defined or undefined in jest?

That works fine, but Jest says, that dotnetify is undefined. If i change to const dotnetify = require ("dotnetify");, Jest passes test, but this is silly workaround.

Is Firebase initialize asynchronous?

firebase.initialize is asynchronous, which is why this.app is undefined by the time you reference it. Mind if I ask why if (!firebase.apps.length) was needed there?


1 Answers

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.

like image 167
Benjamin H Boruff Avatar answered Oct 20 '22 14:10

Benjamin H Boruff