gpt4 book ai didi

javascript - ReactShallowRenderer render() : Shallow rendering works only with custom components, 但提供的元素类型是 `object` 。错误

转载 作者:行者123 更新时间:2023-12-05 04:47:58 24 4
gpt4 key购买 nike

我收到以下错误

ReactShallowRenderer render(): Shallow rendering works only with custom components, but the provided element type was object.

当我尝试模拟某些模块时遇到此错误我的 Main.test.js 文件

import React from 'react';
import { shallow } from 'enzyme';


jest.mock('../Elements', () =>
jest.fn().mockReturnValue({
SortIndicator: (props) => <div {...props}></div>,
ExchangeRate: (props) => <div {...props}></div>,
})
);

jest.mock('../../components/Default_Import', (props) => (
<div {...props}></div>
));

it('IT246 SearchListView is rendering without any error', async () => {
const MainContainer = await import('./MainContainer');

const wrapper = shallow(<MainContainer />)

})


我的 Main.js 文件

import { SortIndicator, ExchangeRate } from '../Elements';

export default class SearchListView extends React.Component {
render() {

return (

<>
<SortIndicator></SortIndicator>
<ExchangeRate></ExchangeRate>
</>

)

}
}

最佳答案

来自文档 Importing defaults

When importing a default export with dynamic imports, it works a bit differently. You need to destructure and rename the "default" key from the returned object.

因此,您需要像这样动态导入 ./MainContainer 模块:

describe('68279075', () => {
it('should pass', async () => {
const MainContainer = (await import('./main')).default;
const wrapper = shallow(<MainContainer />);
console.log(wrapper.debug());
});
});

一个完整的工作示例:

Main.jsx:

import React from 'react';
import { SortIndicator, ExchangeRate } from './Elements';

export default class SearchListView extends React.Component {
render() {
return (
<>
<SortIndicator></SortIndicator>
<ExchangeRate></ExchangeRate>
</>
);
}
}

Elements.jsx:

import React from 'react';

export function ExchangeRate() {
return <div>ExchangeRate</div>;
}

export function SortIndicator() {
return <div>SortIndicator</div>;
}

Main.test.jsx:

import React from 'react';
import { mount } from 'enzyme';

jest.mock('./Elements', () => ({
SortIndicator: (props) => <div {...props}>mocked SortIndicator</div>,
ExchangeRate: (props) => <div {...props}>mocked ExchangeRate</div>,
}));

describe('68279075', () => {
it('should pass', async () => {
const MainContainer = (await import('./main')).default;
const wrapper = mount(<MainContainer />);
console.log(wrapper.debug());
});
});

调试信息:

 PASS  examples/68279075/Main.test.jsx (12.861 s)
68279075
✓ should pass (49 ms)

console.log
<SearchListView>
<SortIndicator>
<div>
mocked SortIndicator
</div>
</SortIndicator>
<ExchangeRate>
<div>
mocked ExchangeRate
</div>
</ExchangeRate>
</SearchListView>

at examples/68279075/Main.test.jsx:13:13

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.51 s

关于javascript - ReactShallowRenderer render() : Shallow rendering works only with custom components, 但提供的元素类型是 `object` 。错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68279075/

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