【问题标题】:How to mock a function in Jest如何在 Jest 中模拟一个函数
【发布时间】:2019-04-09 23:33:58
【问题描述】:

我有以下要在 Jest 中测试的打字稿类。

//MyClass.ts
import { foo } from './somewhere/FooFactory';
export class MyClass {
  private _state : number;
  constructor( arg : string ) {
    this._state = foo( arg );
  }

  public getState() : string {
    return this._state;
  }
}

这是我的测试:

//MyClass.spec.ts
import { MyClass } from './MyClass';
describe( 'test MyClass', () => {
  test( 'construct' => {
    const c = new MyClass( 'test' );
    expect( c ).toBeDefined();
    expect( c.getState() ).toEqual( 'TEST' );
  } );
} );

如何模拟 MyClass 中使用的 foo 函数以使该测试通过?

【问题讨论】:

标签: typescript mocking jestjs


【解决方案1】:

有几种不同的方法来处理它。


您只能使用jest.spyOnmockImplementation 之类的东西模拟foo

import { MyClass } from './MyClass';
import * as FooFactory from './somewhere/FooFactory';

describe('test MyClass', () => {
  test('construct', () => {
    const mock = jest.spyOn(FooFactory, 'foo');  // spy on foo
    mock.mockImplementation((arg: string) => 'TEST');  // replace implementation

    const c = new MyClass('test');
    expect(c).toBeDefined();
    expect(c.getState()).toEqual('TEST');  // SUCCESS

    mock.mockRestore();  // restore original implementation
  });
});

同样,您可以使用jest.mock 自动模拟FooFactory,然后为foo 提供一个实现:

import { MyClass } from './MyClass';
import * as FooFactory from './somewhere/FooFactory';

jest.mock('./somewhere/FooFactory');  // auto-mock FooFactory

describe('test MyClass', () => {
  test('construct', () => {
    const mockFooFactory = FooFactory as jest.Mocked<typeof FooFactory>;  // get correct type for mocked FooFactory
    mockFooFactory.foo.mockImplementation(() => 'TEST');  // provide implementation for foo

    const c = new MyClass('test');
    expect(c).toBeDefined();
    expect(c.getState()).toEqual('TEST');  // SUCCESS
  });
});

您还可以使用传递给jest.mock 的模块工厂模拟FooFactory

import { MyClass } from './MyClass';

jest.mock('./somewhere/FooFactory', () => ({
  foo: () => 'TEST'
}));

describe('test MyClass', () => {
  test('construct', () => {
    const c = new MyClass('test');
    expect(c).toBeDefined();
    expect(c.getState()).toEqual('TEST');  // SUCCESS
  });
});

最后,如果您打算在多个测试文件中使用相同的模拟,您可以通过在./somewhere/__mocks__/FooFactory.ts 创建模拟来mock the user module

export function foo(arg: string) {
  return 'TEST';
}

...然后调用jest.mock('./somewhere/FooFactory'); 在测试中使用模拟:

import { MyClass } from './MyClass';

jest.mock('./somewhere/FooFactory');  // use the mock

describe('test MyClass', () => {
  test('construct', () => {
    const c = new MyClass('test');
    expect(c).toBeDefined();
    expect(c.getState()).toEqual('TEST');  // SUCCESS
  });
});

【讨论】:

  • 我登录 stackoverflow 只是为了支持这个答案。非常感谢。
  • 这很有帮助!
猜你喜欢
  • 2020-02-15
  • 2021-04-19
  • 2019-01-06
  • 1970-01-01
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多