【问题标题】:React unit testing with Ava: Why is the function already wrapped?使用 Ava 进行 React 单元测试:为什么函数已经被包装了?
【发布时间】:2017-01-23 03:27:43
【问题描述】:

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

【问题讨论】:

    标签: javascript reactjs testing ava


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-11-12
      • 2020-01-06
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 2021-08-21
      • 2013-05-18
      相关资源
      最近更新 更多