我有一个React组件,它有两个子元素,代码如下:
import {Child1} from './child1';
import {Child2} from './child2';
...
return (
<>
<Child1 />
<Child2 />
</>
)
我正在使用react的testing-library
,并且这个应用是由create react app
创建而成的,没有被弹出。 我想在我的单元测试中对它们进行模拟,因为它们有自己的测试,所以我正在尝试:
jest.mock('./child1', () => 'some mocked string');
jest.mock('./child1', () => 'some mocked string');
但是当我使用
import { render } from '@testing-library/react';
来渲染它时,我看到下面的警告:Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined
。这是为什么,我该如何模拟这些组件?
describe
外部声明模拟对象。现在它能正常工作了。 关于为什么当我将jest.mock(...)
放在describe
块内时会失败(即无法模拟并呈现真实组件),请问是否有任何解释? - Fazwelington