Javascript Jest/Ezyme:如何测试嵌套异步函数的.then和.catch
我正在尝试对我的reactJS组件中的异步函数进行一些Ezyme/jest单元测试,该组件作为prop注入 我的问题是测试函数的Javascript Jest/Ezyme:如何测试嵌套异步函数的.then和.catch,javascript,reactjs,unit-testing,asynchronous,enzyme,Javascript,Reactjs,Unit Testing,Asynchronous,Enzyme,我正在尝试对我的reactJS组件中的异步函数进行一些Ezyme/jest单元测试,该组件作为prop注入 我的问题是测试函数的then()部分中的值,以及在发生错误时测试catch() 我的组件()的功能如下所示: _onSubmit = (event) => { event.preventDefault() const { username, password } = this.state this.props.createUserMutation({ varia
then()
部分中的值,以及在发生错误时测试catch()
我的组件(
)的功能如下所示:
_onSubmit = (event) => {
event.preventDefault()
const { username, password } = this.state
this.props.createUserMutation({
variables: { username, password }
}).then(response => {
const token = response.data.createUser.token
if (token) {
Cookies.set('auth-token', token, { expires: 1 })
}
}).catch(error => {
console.warn(error)
})
}
第一个测试应检查.catch(error=>{})
,因为数据未定义:
it('_onSubmit() should throw error if data is missing', () => {
const createUserMutation = () => {
return Promise.resolve({})
}
const wrapper = shallow(<CreateAccount createUserMutation={createUserMutation} />)
wrapper.update().find(Form).simulate('submit', {
preventDefault: () => {}
})
const state = wrapper.instance().state
expect(wrapper).toThrow() // <-- How to do it correctly?
})
为什么要使用
控制台。警告错误?使用控制台。错误
。你将需要模仿它的间谍以及测试它
第一次测试:
it('_onSubmit() should throw error if data is missing', (done) => {
const createUserMutation = () => new Promise();
const wrapper = shallow(<CreateAccount createUserMutation={createUserMutation} />)
wrapper.update().find(Form).simulate('submit', {
preventDefault: () => {}
})
const state = wrapper.instance().state
createUserMutation.resolve().then(() => {
expect(console.warn).toHaveBeenCalled();
done();
});
})
我通常要做的是,在测试中添加另一个then(),以查看间谍是否对捕获进行了操作。例如:
it('_onSubmit() should throw error if data is missing', () => {
const createUserMutation = jest.fn(() => Promise.reject(new Error()));
const spy = jest.spyOn(console,"warn");
const wrapper = shallow(<CreateAccount createUserMutation={createUserMutation} />)
wrapper.update().find(Form).simulate('submit', {
preventDefault: () => {}
});
return createUserMutation.catch(() => {
expect(wrapper).toMatchSnapshot();
})
.then(() => {
expect(spy).toHaveBeenCalledTimes(1);
});
})
it(“\u onSubmit()如果数据丢失,则应抛出错误,”()=>{
const createUserMutation=jest.fn(()=>Promise.reject(新错误());
const spy=jest.spyOn(控制台,“警告”);
常量包装器=浅()
wrapper.update().find(Form).simulate('submit'{
preventDefault:()=>{}
});
返回createUserMutation.catch(()=>{
expect(wrapper.toMatchSnapshot();
})
.然后(()=>{
期望(间谍)。被催促的时间(1);
});
})
我猜这与NodeJS如何在内部处理队列、承诺、滴答声等有关
这是拒绝/捕获分支。如果要测试If路径,只需使用Promise.resolve并返回Promise.then()而不是catch。我确实得到了错误TypeError:Promise resolver undefined不是该输入的两个teststhank的函数。美好的我会试试的。
it('_onSubmit() should get token', (done) => {
const createUserMutation = () => new Promise();
const wrapper = shallow(<CreateAccount createUserMutation={createUserMutation} />)
wrapper.find(Form).simulate('submit', {
preventDefault: () => {}
});
jest.spyOn(window.Cookies, 'set');
const response = {
data: {
createUser: { token: 'token' }
}
}
createUserMutation.resolve(response).then(() => {
expect(window.Cookies.set).toHaveBeenCalled();
done();
});
})
afterEach(() => {
// Reset the spies so that they don't leak to other tests
jest.restoreAllMocks();
});
it('_onSubmit() should throw error if data is missing', () => {
const createUserMutation = jest.fn(() => Promise.reject(new Error()));
const spy = jest.spyOn(console,"warn");
const wrapper = shallow(<CreateAccount createUserMutation={createUserMutation} />)
wrapper.update().find(Form).simulate('submit', {
preventDefault: () => {}
});
return createUserMutation.catch(() => {
expect(wrapper).toMatchSnapshot();
})
.then(() => {
expect(spy).toHaveBeenCalledTimes(1);
});
})