为什么在运行 redux 操作测试时不从文件夹中调用__mock__模拟?



我在 redux 操作中调用 react-navigation NavigationService。测试我需要模拟导航函数的动作。

/app/utils/NavigationService.js

import { NavigationActions } from 'react-navigation';
let navigator;
function setTopLevelNavigator(navigatorRef) {
  navigator = navigatorRef;
}
function navigate(routeName, params) {
  navigator.dispatch(NavigationActions.navigate({
    type: NavigationActions.NAVIGATE,
    routeName,
    params,
  }));
}
// add other navigation functions that you need and export them
export default {
  navigate,
  setTopLevelNavigator,
};

我创建了一个紧邻导航服务.js文件的__mock__文件夹。

app/utils/__mocks__/NavigationService.js更新

const navigate = jest.fn();
const setTopLevelNavigator = jest.fn();
export default {
    navigate,
    setTopLevelNavigator,
};

为什么 jest 在测试运行时不自动模拟 navigate 函数?https://jestjs.io/docs/en/manual-mocks

__tests__/actions/AuthActions.test.js更新

jest.mock('../../app/utils/NavigationService'); //at the top directly behind other imports
it('should call firebase on signIn', () => {
    const user = {
      email: 'test@test.com',
      password: 'sign',
    };
    const expected = [
      { type: types.LOGIN_USER },
      { payload: 1, type: types.DB_VERSION },
      { payload: 'prod', type: types.USER_TYPE },
      { payload: { name: 'data' }, type: types.WEEKPLAN_FETCH_SUCCESS },
      { payload: { name: 'data' }, type: types.RECIPELIBRARY_FETCH_SUCCESS },
      {
        payload: { user: { name: 'user' }, userVersionAndType: { dbVersion: 1, userType: 'prod' } },
        type: types.LOGIN_USER_SUCCESS,
      },
    ];
    return store.dispatch(actions.loginUser(user)).then(() => {
      expect(store.getActions()).toEqual(expected);
    });
  });

app/actions/AuthActions.js

export const loginUser = ({ email, password }) => (dispatch) => {
  dispatch({ type: LOGIN_USER });
  return firebase
    .auth()
    .signInWithEmailAndPassword(email, password)
    .catch((signInError) => {
      dispatch({ type: CREATE_USER, payload: signInError.message });
      return firebase
        .auth()
        .createUserWithEmailAndPassword(email, password)
        .then(async (user) => {
          const userVersionAndType = await dispatch(initUser());
          await dispatch(initWeekplan(userVersionAndType));
          await dispatch(initRecipeLibrary(userVersionAndType));
          return user;
        });
    })
    .then(async (user) => {
      saveCredentials(email, password);
      const userVersionAndType = await dispatch(getUserVersionAndType());
      await dispatch(weekplanFetch(userVersionAndType));
      await dispatch(recipeLibraryFetch(userVersionAndType));
      dispatch(loginUserSuccess({ user, userVersionAndType }));
      NavigationService.navigate('Home');
    })
    .catch(error => dispatch(loginUserFail(error.message)));
};

您已经为用户模块创建了一个手动模拟。

为特定测试文件激活用户模块的手动模拟需要调用 jest.mock

对于这种特殊情况,将此行添加到__tests__/actions/AuthActions.test.js的顶部,模拟将用于该测试文件中的所有测试:

jest.mock('../../app/utils/NavigationService');  // use the manual mock in this test file

请注意,用户模块和 Node 核心模块(如 fspathutil 等(的手动模拟都必须通过调用 jest.mock 来激活特定的测试文件,并且此行为不同于自动应用于所有测试的 Node 模块的手动模拟。

最新更新