【发布时间】:2021-03-01 11:22:17
【问题描述】:
我在使用 toHaveBeenCalled 时遇到错误,请纠正我哪里出错了
代码:
jsx
<item
onClick={ load ? undefined : onClick}
>
测试
test('render', () => {
const MockItems = jest.fn()
const prop = {
onClick: MockItems,
}
const onclickProp= output.find(item).props().onClick
onclickProp(undefined)
expect(props.onClick).toHaveBeenCalled()//error
}
错误
expect(props.onClick).toHaveBeenCalled()
Warning: An update to null inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
【问题讨论】:
-
你得到错误的原因是因为你试图改变反应调用堆栈之外的状态
标签: reactjs jestjs enzyme react-testing-library