gpt4 book ai didi

javascript - Jest renderIntoDocument 不起作用

转载 作者:行者123 更新时间:2023-11-28 05:48:38 25 4
gpt4 key购买 nike

我有一个简单的 React 应用程序,仅包含 Hello World React 组件,我想用 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)

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

最佳答案

明白了这一点。这是一个命名问题。

const HelloWorld 

相同
import HelloWorld

关于javascript - Jest renderIntoDocument 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38276331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com