小编And*_*Law的帖子

Testing catch block via jest mock

I'm trying to test the 'catch' block of an async redux action via jest, but throwing a catch in the mock causes the test as a whole to fail.

My action is as follows:

export function loginUser(username, password) {
  return async dispatch => {
    dispatch({type: UPDATE_IN_PROGRESS});
    try {
      let response = await MyRequest.postAsync(
        '/login', {username: username, password: password}
      );
      dispatch({
        type: USER_AUTHENTICATED,
        username: response.username,
        token: response.token,
        role: response.role,
        id: response.id
      });
    } catch (error) {
      dispatch({type: USER_SIGNED_OUT});
      throw error;
    } …
Run Code Online (Sandbox Code Playgroud)

unit-testing jestjs redux

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

jestjs ×1

redux ×1

unit-testing ×1