import {ActualComponent} from "mycomponentlibrary";
//Below is my Mock component which I want to use as mock.
const MockComponent = props => {
const selectedUser = {
name: "User1",
};
return (
<div>
<div>{props.dialogTitle}</div>
<button onClick={() => props.onSelect(selectedUser)}>{"Select"}</button>
</div>
);
};
//below is how am trying to mock
jest.doMock("mycomponentlibrary", () => () => {
return {
__esModule: true,
ActualComponent: jest.fn(() => MockComponent),
};
});
但是我看到我的测试失败了,并且在日志中我看到使用的是实际组件而不是模拟组件。
有什么地方做错了吗?
注意:ActualComponent
不是默认导出,我不想模拟"mycomponentlibrary"
的其他组件
答案 0 :(得分:0)
jest.mock("mycomponentlibrary", () => {
return {
__esModule: true,
ActualComponent: () => MockComponent
};
};
在这种情况下,使用doock代替doMock就足够了。
此外,除非要监视它,否则不要使用jest.fn()包装模拟的组件。