0

Even though I am using the afterEach I still get the following error:Attempted to wrap setTimeout which is already wrapped.What am I doing wrong?I am using ava to do this unit test. The test I am trying to create is very simple. It is pretty much check the render and a click action. There is also a test to check if the setTimeOut function has been called with the right arguments.

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));
})
zeid10
  • 511
  • 8
  • 28

1 Answers1

2

AVA runs tests in parallel, so the beforeEach is run for each of the tests before afterEach is run.

The quickest way to make this test work is to only use test.serial() in this file. That way all tests execute serially and the afterEach has a chance to clean up before the next beforeEach is run.

Mark Wubben
  • 3,329
  • 1
  • 19
  • 16