首页 > 解决方案 > 如何用玩笑来监视/模拟 minimatch

问题描述

我知道如何用 jest 模拟/监视 ES6 导入,但是这个让我头疼:

my-module.ts

import minimatch from 'minimatch';

export function foo(pattern: string, str: string): boolean {
  return minimatch(pattern, str);
}

test.ts

describe('minimatch', () => {
  it('should call minimatch', () => {
    const mock = jest.fn().mockReturnValue(true);
    jest.mock('minimatch', mock);

    foo('*', 'hello');

    expect(mock).toHaveBeenCalled();
  });
});

我也尝试过以不同的方式模拟:

import * as minimatch from 'minimatch';
// ...
const mock = jest.fn().mockReturnValue(true);
(minimatch as any).default = mock;

甚至

import {mockModule} from '../../../../../../test/ts/utils/jest-utils';
// ...
const mock = jest.fn().mockReturnValue(true);
const originalModule = jest.requireActual('minimatch');
jest.mock('minimatch', () => Object.assign({}, originalModule, mockModule));

我的测试因上述所有模拟方式而失败。

标签: javascripttypescriptunit-testingmockingjestjs

解决方案


您不能jest.mock()在测试用例功能范围内使用。您应该在模块范围内使用它。

例如 my-module.ts

import minimatch from 'minimatch';

export function foo(pattern: string, str: string): boolean {
  return minimatch(pattern, str);
}

my-module.test.ts

import { foo } from './my-module';
import minimatch from 'minimatch';

jest.mock('minimatch', () => jest.fn());

describe('minimatch', () => {
  it('should call minimatch', () => {
    foo('*', 'hello');
    expect(minimatch).toHaveBeenCalled();
  });
});

覆盖率 100% 的单元测试结果:

 PASS  stackoverflow/60350522/my-module.test.ts
  minimatch
    ✓ should call minimatch (6ms)

--------------|---------|----------|---------|---------|-------------------
File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
--------------|---------|----------|---------|---------|-------------------
All files     |     100 |      100 |     100 |     100 |                   
 my-module.ts |     100 |      100 |     100 |     100 |                   
--------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        4.304s, estimated 6s

如果你想在测试用例中模拟模块,你应该使用jest.doMock(moduleName, factory, options)

例如

my-module.test.ts

describe('minimatch', () => {
  it('should call minimatch', () => {
    jest.doMock('minimatch', () => jest.fn());
    const { foo } = require('./my-module');
    const minimatch = require('minimatch');
    foo('*', 'hello');
    expect(minimatch).toHaveBeenCalled();
  });
});

推荐阅读