0
私はafterEachを使用していますが、まだ次のエラーが表示されています:既にラップされたsetTimeoutをラップしようとしました。間違っていますか?このユニットテストを行うにはavaを使用しています。作成しようとしているテストは非常に簡単です。それはレンダリングとクリックアクションをかなりチェックしています。 setTimeOut関数が正しい引数で呼び出されたかどうかを調べるテストもあります。Avaによる反復単位テスト:なぜ関数はすでにラップされていますか?
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));
})