0

即使我使用 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));
})
4

1 回答 1

2

AVA 并行运行测试,因此在运行之前beforeEach为每个测试运行。 afterEach

使这个测试工作的最快方法是只test.serial()在这个文件中使用。这样,所有测试都按顺序执行,并且afterEach有机会在下一次运行之前进行清理beforeEach

于 2017-01-24T10:32:38.657 回答