【问题标题】:Jest renderIntoDocument not working开玩笑 renderIntoDocument 不起作用
【发布时间】: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)

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

【问题讨论】:

标签: javascript reactjs react-redux jestjs


【解决方案1】:

想通了。这是一个命名问题。

const HelloWorld 

一样
import HelloWorld

【讨论】:

  • 你能继续前进吗?我遇到了几乎类似的问题,但“const Helloworld”始终为 null ,我无法继续前进。对此有何想法?
猜你喜欢
  • 1970-01-01
  • 2018-10-13
  • 2018-08-25
  • 2023-01-07
  • 1970-01-01
  • 2018-04-29
  • 2021-06-11
  • 2021-07-13
  • 2017-09-26
相关资源
最近更新 更多