Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime error Running Jest : in React

I'm unable to run Jest tests and have a very vague error message. I found a similar issue on StackOverflow, and I wasn't able to solve it with their suggestion of adding jestSupport in the react folder in the node_module.

issue referenced: How to use Jest with React Native

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

// Snippet from 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: '[email protected]'}); 
          });
        };

Has anyone run into this issue before, & have suggestions?

like image 835
Christina Mitchell Avatar asked May 02 '16 23:05

Christina Mitchell


People also ask

How do I set Jest timeout?

For Jest 24.9+, you can also set the timeout from the command line by adding --testTimeout . Default timeout of a test in milliseconds. Default value: 5000.

Can you use Jest with React?

Jest is a JavaScript testing framework that allows developers to run tests on JavaScript and TypeScript code and can be easily integrated with React JS.


2 Answers

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

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

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

like image 197
Noah Avatar answered Oct 05 '22 00:10

Noah


As far as I can tell, you're requiring the mocked versions of all the modules.

Try putting

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 all of that and as far I can tell it should work.

like image 28
Naman Goel Avatar answered Oct 05 '22 02:10

Naman Goel