unit-testing - 运行时错误运行 Jest : in React

标签 unit-testing reactjs jestjs

我无法运行 Jest 测试,并且收到非常模糊的错误消息。 我在 StackOverflow 上发现了类似的问题,但我无法通过他们在 node_module 的 React 文件夹中添加 jestSupport 的建议来解决它。

问题引用: How to use Jest with React Native

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

//package.JSON 的片段

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

//测试文件

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'}); 
          });
        };

有人以前遇到过这个问题并有建议吗?

最佳答案

将其放入我的 package.json 中,与“脚本”处于同一级别,这对我有用:

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

我的测试文件顶部没有任何 jest.dontMock 语句。我仅将 jest.unmock 用于测试下的模块。

关于unit-testing - 运行时错误运行 Jest : in React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36993445/

相关文章:

unit-testing - 自动测试经典 ASP

babeljs - babel-jest 不处理模块内的 ES6

typescript - NestJS测试: decorator is not a function

css - 如何导入 Ant Design React UI 库和 css?

javascript - 测试组件快照,获取 json 错误

testing - 如何 Jest.mock redux-form 的字段

c# - 使用/单元测试表单验证 Restful 网络服务

iphone - 链接器错误: iPhone Unit Test Bundle referencing App classes

swift - 测试在抛出的函数上返回 "Type of expression is ambiguous without more context"

javascript - 根据我的数据对象的值在表上渲染自定义组件?