如何模拟外部模块方法?

时间:2017-03-29 01:04:17

标签: javascript reactjs unit-testing jestjs

我的模块中有一个函数:

const utils = {
  redirectTo(url) {
    if (url) {
      window.location = url;
    }
  },
};

export default utils;

它在React组件中的某处使用,如下所示:

import utils from '../../lib/utils';

componentWillUpdate() {
  this.redirectTo('foo')
}

现在我想检查redirectTo的值是否等于foo

  it('should redirect if no rights', () => {
    const mockRedirectFn = jest.fn();
    utils.redirectTo = mockRedirectFn;

    mount(
      <SomeComponent />,
    );

    expect(mockRedirectFn).toBeCalled();
    expect(mockRedirectFn).toBeCalledWith('foo');
    console.log(mockRedirectFn.mock);
    // { calls: [], instances: [] }
  });

这就是我得到的东西,它不起作用。我该怎么做?

2 个答案:

答案 0 :(得分:3)

你必须像这样嘲笑 var opts; var initOpts = function() { opts = {} if (scope.options) { if (scope.options.types) { opts.types = [] opts.types.push(scope.options.types) } if (scope.options.bounds) { opts.bounds = scope.options.bounds } if (scope.options.country) { opts.componentRestrictions = { country: 'IN' } } } } 模块:

lib/utils

这将使用只返回import utils from '../../lib/utils'; jest.mock('../../lib/utils', () = > ({ redirect: jest.fn() })) it('should redirect if no rights', () = > { mount( <SomeComponent />, ); expect(utils.redirect).toHaveBeenCalledWith(); }); 的模拟替换模块。此模块也会导入到您的测试中,然后您可以访问{redirect:jest.fn()}的间谍并测试它是否使用正确的参数调用它。

答案 1 :(得分:1)

这是我最终使用的:

 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();
  });