【发布时间】:2019-07-21 03:07:34
【问题描述】:
我正在开发一个 React 项目,并且正在使用 jest 为我的代码编写测试。
这是我要测试的代码。
const handleSubmit = (handleSuccess, handleErrors) => {
signupAPI(user)
.then(handleSuccess)
.catch(handleErrors);
};
这是测试代码:
test('should call handleSuccess', () => {
signupAPI.mockImplementation((user) => Promise.resolve(user));
const handleSuccess = jest.fn();
const handleErrors = jest.fn();
handleSubmit(handleSuccess, handleErrors);
expect(signupAPI).toHaveBeenCalled(); // test passes
expect(handleSuccess).toHaveBeenCalled(); // test fails
});
当我运行测试时,它永远不会移动到 promise 之后的“then”部分。如何测试 then 部分中的函数是否实际被调用?
【问题讨论】: