我无法运行 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/