Javascript 如何用笑话刺探/模拟小竞赛

Javascript 如何用笑话刺探/模拟小竞赛,javascript,typescript,unit-testing,mocking,jestjs,Javascript,Typescript,Unit Testing,Mocking,Jestjs,我知道该怎么做,但这件事却萦绕在我的脑海里: my module.ts 从“minimatch”导入minimatch; 导出函数foo(模式:string,str:string):布尔值{ 返回最小匹配(模式,str); } test.ts: describe('minimatch', () => { it('should call minimatch', () => { const mock = jest.fn().mockReturnValue(true);

我知道该怎么做,但这件事却萦绕在我的脑海里:

my module.ts

从“minimatch”导入minimatch;
导出函数foo(模式:string,str:string):布尔值{
返回最小匹配(模式,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));

我的测试由于以上所有模拟方法而失败。

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

例如。
my module.ts

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

    foo('*', 'hello');

    expect(mock).toHaveBeenCalled();
  });
});
从“minimatch”导入minimatch;
导出函数foo(模式:string,str:string):布尔值{
返回最小匹配(模式,str);
}
my module.test.ts

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

    foo('*', 'hello');

    expect(mock).toHaveBeenCalled();
  });
});
从“/my module”导入{foo};
从“minimatch”导入minimatch;
jest.mock('minimatch',()=>jest.fn());
描述('minimatch',()=>{
它('应该调用minimatch',()=>{
foo('*','hello');
expect(minimatch).tohavebeincall();
});
});
100%覆盖率的单元测试结果:

PASS stackoverflow/60350522/my-module.test.ts
最小匹配
✓ 应呼叫minimatch(6ms)
--------------|---------|----------|---------|---------|-------------------
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s
--------------|---------|----------|---------|---------|-------------------
所有文件| 100 | 100 | 100 | 100 |
my-module.ts | 100 | 100 | 100 | 100 |
--------------|---------|----------|---------|---------|-------------------
测试套件:1个通过,共1个
测试:1项通过,共1项
快照:共0个
时间:4.304s,估计6s
如果您想在测试用例中模拟模块,应该使用

例如

my module.test.ts

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

    foo('*', 'hello');

    expect(mock).toHaveBeenCalled();
  });
});
description('minimatch',()=>{
它('应该调用minimatch',()=>{
jest.doMock('minimatch',()=>jest.fn());
const{foo}=require('./mymodule');
const minimatch=require('minimatch');
foo('*','hello');
expect(minimatch).tohavebeincall();
});
});

这些示例会发生什么?测试总是失败。我认为主要的问题是
minimatch
如何从其模块导出。它们确实是
module.exports=minimatch
,而不是经典的
module.exports={default:…}
导出默认minimatch
@jonrsharpe我编辑了我的帖子以使其更清晰。好点了吗?很清楚。范围是个问题,可能应该在文档中更加强调。使用
doMock
的第二种方法在语法上是奇怪的,但最好知道,因为有时您不希望文件中的其他测试用例使用真正的实现。有没有办法
spyOn
这个模块?