Reactjs 异步内的React jest测试承诺
我有下面的代码,但我似乎无法让它工作。我做错了什么?似乎是承诺的异步包装导致了问题。无论我做什么,我总是会出错Reactjs 异步内的React jest测试承诺,reactjs,unit-testing,testing,jestjs,enzyme,Reactjs,Unit Testing,Testing,Jestjs,Enzyme,我有下面的代码,但我似乎无法让它工作。我做错了什么?似乎是承诺的异步包装导致了问题。无论我做什么,我总是会出错 export const doLogin = (email, password) => { return async (dispatch, getState) => { return new Promise((resolve, reject) => { resolve(true) }) }; }; and the test usi
export const doLogin = (email, password) => {
return async (dispatch, getState) => {
return new Promise((resolve, reject) => {
resolve(true)
})
};
};
and the test using jest and enzyme
expect(await doLogin('', '')).resolves.toEqual(true);
我得到的错误是
expect(received).resolves.toEqual()
Matcher error: received value must be a promise
Received has type: function
Received has value: [Function anonymous]
doLogin
是返回异步函数的常规函数await
需要在expect
调用之外使用,因为resolves
使断言返回承诺
应该是:
await expect(doLogin('', '')(dispatchMock, getStateMock)).resolves.toEqual(true);
另外,
(dispatch,getState)=>…
函数不能从异步中获益,因为它只返回一个承诺,而不使用wait
语法。doLogin
对我来说没有多大意义。为什么新承诺
?您可以只执行async()=>true
,但该函数似乎过于做作,失去了上下文。另外,我认为解析
匹配器已经在等待承诺,而等待
明确表示您正在将true
传递到expect
,因此您需要expect(wait doLogin()).toBe(true)
。