即使我使用 afterEach 我仍然收到以下错误:尝试包装已经包装的 setTimeout。我做错了什么?我正在使用 ava 进行此单元测试。我要创建的测试非常简单。它几乎是检查渲染和点击动作。还有一个测试来检查是否使用正确的参数调用了 setTimeOut 函数。
import test from 'ava';
import React from 'react';
import { CartMessage } from 'components/Cart/CartMessage';
import { shallow, mount } from 'enzyme';
import { spy, stub } from 'sinon';
let props;
let popError;
let cartmessage;
let timeoutSpy;
test.beforeEach(() => {
props = {
message: 'testing',
count: 2,
popError: stub().returns(Promise.resolve()),
};
cartmessage = shallow(<CartMessage {...props}/>)
timeoutSpy = spy(window, 'setTimeout');
});
test.afterEach(()=>{
timeoutSpy.restore()
})
test('renders okay?', (t) => {
t.truthy(Cartmessage)
});
test('componentDidMount calls setTimeout with proper args', t => {
t.true(timeoutSpy.calledWithExactly(() => this.setState({ MessageOpen: true }), 1))
})
test('onClose called?', t => {
const wrapper = shallow(<CartMessage {...props} />);
wrapper.find('i').simulate('click');
t.true(timeoutSpy.calledWithExactly(this.props.popError, 1))
})
test('timeout i called with the right args', (t) => {
t.true(timeoutSpy.calledWithExactly(this.props.popError, 1));
})