【发布时间】:2016-11-11 14:14:56
【问题描述】:
我有一个简单的 react 应用程序,它只包含一个 Hello World 反应组件,我想用 Jest 对其进行测试。
这是我的简单 hello world 组件
import React from 'react';
class HelloWorld extends React.Component {
render() {
return (
<div>
Hello world
</div>
);
}
}
export default HelloWorld;
这是我的测试
// helloWorldTest-spec.js
jest.unmock('../src/components/HelloWorld');
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import HelloWorld from '../src/components/HelloWorld';
describe('jest test', () => {
const HelloWorld = TestUtils.renderIntoDocument(
<HelloWorld />
);
it('should exist', () => {
expect(true).toEqual(true); // just want the test to pass
});
});
它会失败并返回这个
runtime Error
- TypeError: Cannot read property 'default' of undefined
at Object.<anonymous> (__tests__/helloWorldTest-spec.js:5:36)
at Runtime._execModule (node_modules/jest-runtime/build/index.js:375:17)
at Runtime.requireModule (node_modules/jest-runtime/build/index.js:210:14)
at jasmine2 (node_modules/jest-jasmine2/build/index.js:293:11)
at Test.run (node_modules/jest-cli/build/Test.js:50:12)
at promise.then.then.data (node_modules/jest-cli/build/TestRunner.js:264:62)
at process._tickCallback (internal/process/next_tick.js:103:7)
1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 1.002s)
有没有人遇到过类似的问题?
【问题讨论】:
-
在看到这个帖子后,我最终使用了
render():github.com/kentcdodds/react-testing-library/issues/116
标签: javascript reactjs react-redux jestjs