我有以下代码,但似乎无法正常工作。我究竟做错了什么?似乎包装承诺的异步导致了问题。无论我尝试什么,我总是会出错
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]
答案 0 :(得分:0)
doLogin
是返回异步函数的常规函数。 await
需要在 expect
调用之外使用,因为 resolves
使断言返回承诺。
应该是:
await expect(doLogin('', '')(dispatchMock, getStateMock)).resolves.toEqual(true);
此外,(dispatch, getState) => ...
函数也不会从 async
中受益,因为它只是返回一个 Promise 而不使用 await
语法。