2017-01-23 9 views
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)); 
}) 

答えて

2

AVAは、並行してテストを実行し、そうbeforeEachafterEachが実行される前にテストごとに実行されます。

このテスト作業を行う最も簡単な方法は、test.serial()のみをこのファイルに使用することです。そうすれば、すべてのテストが連続して実行され、afterEachは、次のbeforeEachが実行される前にクリーンアップする機会があります。

関連する問題