score:4

this is what i ended up using:

 it('should redirect if no rights', () => {

    // this way we mock only one method: redirectto
    jest.mock('lib/utils', () => {
      const original = require.requireactual('lib/utils');
      original.default.redirectto = jest.fn();
      return original;
    });

    const redirectto = require.requiremock('lib/utils').default.redirectto;

    mount(
      <somecomponent />,
    );

    expect(redirectto).tohavebeencalled();
  });

score:10

you have to mock the lib/utils module like this:

import utils from '../../lib/utils';
jest.mock('../../lib/utils', () => ({
  redirect: jest.fn()
}))

it('should redirect if no rights', () => {
  mount(
    <somecomponent />,
  );
  expect(utils.redirect).tohavebeencalledwith();
});

this will replace the module with a mock that just returns {redirect:jest.fn()}. this module is also imported into you test where you then can access the spy for redirect and test on this that it was called with the correct parameter.


Related Query

More Query from same tag