【发布时间】:2019-05-05 06:33:40
【问题描述】:
在一个测试文件中,我需要渲染一个组件,同时模拟它的一些子组件。文件结构大致如下所示。
文件 1
import {A, B} from 'a-module';
export function MyComponent() {
return (
<div>
<A /> // I need to mock
<B /> // these components out
</div>
);
}
文件 2
import {MyComponent} from 'File 1';
/*
* In this file I would like to render MyComponent but
* have components A and B be replaced by mocks
*/
我尝试过jest.mock('a-module', () => 'Blah');,但这并没有成功地模拟组件。但是,当在文件 1 中使用默认导入时,此方法有效。
如果在文件 2 中渲染 MyComponent 时模拟出组件 A 和 B,将不胜感激!
【问题讨论】:
标签: javascript reactjs unit-testing webpack jestjs