Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Ava进行React单元测试:为什么功能已经包装?_Javascript_Reactjs_Testing_Ava - Fatal编程技术网

Javascript 使用Ava进行React单元测试:为什么功能已经包装?

Javascript 使用Ava进行React单元测试:为什么功能已经包装?,javascript,reactjs,testing,ava,Javascript,Reactjs,Testing,Ava,即使我使用afterEach,我仍然会出现以下错误:尝试包装已包装的setTimeout。我做错了什么?我正在使用ava进行此单元测试。我尝试创建的测试非常简单。这几乎是检查渲染和单击操作。还有一个测试,用于检查是否使用正确的参数调用了setTimeOut函数 import test from 'ava'; import React from 'react'; import { CartMessage } from 'components/Cart/CartMessage'; import {

即使我使用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));
})
从“ava”导入测试;
从“React”导入React;
从“components/Cart/CartMessage”导入{CartMessage};
从“酶”导入{shall,mount};
从'sinon'导入{spy,stub};
让道具;
让我们犯错误;
让我们传递信息;
让时光飞逝;
每次测试之前(()=>{
道具={
消息:“正在测试”,
计数:2,
popError:stub().returns(Promise.resolve()),
};
cartmessage=shallow()
timeoutSpy=spy(窗口“setTimeout”);
});
每次测试后(()=>{
timeoutSpy.restore()
})
测试(‘渲染正常?’,(t)=>{
t、 truthy(Cartmessage)
});
测试('componentDidMount使用正确的参数调用setTimeout',t=>{
t、 true(timeoutSpy.calledWithjustice(()=>this.setState({MessageOpen:true}),1))
})
test('onClose called?',t=>{
常量包装器=浅();
find('i').simulate('click');
t、 true(timeoutSpy.calledWithjustice(this.props.popError,1))
})
test('我用正确的参数调用超时',(t)=>{
t、 true(timeoutSpy.calledWithjustice(this.props.popError,1));
})

AVA并行运行测试,因此每次运行
之前的
测试将在每次运行
之后的
测试中运行


使此测试工作的最快方法是在此文件中仅使用
test.serial()
。通过这种方式,所有测试都会连续执行,并且在运行下一个
beforeach
之前,每个
之后的
都有机会在运行下一个
beforeach
之前进行清理。

AVA并行运行测试,因此每个
之后的
之前的每个测试都会运行
beforeach

使此测试工作的最快方法是在此文件中仅使用
test.serial()
。这样,所有测试都会连续执行,并且每次运行后的
有机会在每次运行前的下一次
之前进行清理