Jest renderIntoDocument无法正常工作

时间:2016-07-08 22:39:07

标签: javascript reactjs react-redux jestjs

我有一个简单的反应应用程序,只包含一个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)

有没有人遇到过类似的问题?

1 个答案:

答案 0 :(得分:0)

想出来了。这是一个命名问题。

const HelloWorld 

相同
import HelloWorld