Runtime Error Running Jest: in React

I cannot run Jest tests and have a very vague error message. I found a similar problem in StackOverflow, and I could not solve it by suggesting adding jestSupport to the response folder in node_module.

Q: How to use Jest with React Native

__tests__/profile-test.js ● Runtime Error TypeError: Cannot read property 'DEFINE_MANY' of undefined 

// Snippet from the package. Json

 "scripts": { "test": "jest" }, "jest": { "scriptPreprocessor": "<rootDir>/node_modules/babel-jest" }, 

// test file

 import React from 'react'; import ReactDOM from 'react-dom'; import TestUtils from 'react-addons-test-utils'; import Profile from '../components/profile'; jest.setMock('react', {}); // put this in after reading a troubleshooting article //jest.autoMockOff(Profile); describe('Profile'), () => { it("renders a form containing user information", function() { let Profile = TestUtils.renderIntoDocument(<Profile/>); let renderedDOM = () => React.findDOMNode(Profile); expect(renderedDOM.tagName).toBe('div'); expect(renderedDOM.classList).toEqual(['value', 'image', 'btn btn-primary', 'btn btn-danger' ]); var children = renderedDOM.querySelectorAll('fieldConfig.type'); //created a custom expect(children.length).toBe(3); expect(children[0]).toEqual({name: 'test mc nameyname', email: ' testmcnameyname@gmail.com '}); }); }; 

Has anyone encountered this problem before and had suggestions?

+7
unit-testing reactjs jestjs
source share
2 answers

Put this in my package.json at the same level as the "scripts" for me:

 "jest": { "unmockedModulePathPatterns": [ "react" ] } 

I did not have any jest.dontMock instructions at the top of the test file. I used jest.unmock only for the module under test.

+9
source share

As far as I can tell, you need mocking versions of all the modules.

Try to put

 jest.dontMock('react') jest.dontMock('react-dom') jest.dontMock('react-addons-test-utils') jest.dontMock('../components/profile') 

at the top of the file.

I would also get rid of:

 jest.setMock('react', {}); 

Try it all, and as far as I can tell, it should work.

+2
source share

All Articles