繁体   English   中英

Mocking TypeDI 服务与 Jest

[英]Mocking TypeDI service with Jest

我正在使用带有 TypeScript、TypeDI 和 Jest 的 Node。 我正在创建相互依赖的服务,比如说:

@Service()
export class MainService{
constructor(private secondService: SecondService){}
public someMethod(someString: string) // implementation depends on secondService
}

@Service()
export class SecondService{
constructor(private thirdService: ThirdService){}
}

@Service()
export class ThirdService{
constructor(){}
}

我想测试 MainService,但要实例化它,我需要传递依赖项,并且该依赖项需要另一个依赖项。 我试着这样做,它有效,但很丑:

const secondService = new SecondService(new ThirdService());
jest
    .spyOn(secondService, "someMethod")
    .mockImplementation((someString: string) => {
        // do something
        return something;
    });
const mainService = new MainService(secondService);
// use mainService in tests

当然,创建新的依赖实例并不总是一种选择,当它有很多依赖时,它肯定不是一种选择。

我认为它应该看起来更像:

const secondService = SomeMockFactory.create(SecondService);

但是我在切断依赖关系的同时找不到任何方法来创建模拟。 我尝试使用

const secondService = jest.genMockFromModule("path/to/second/service");

但是在尝试监视 secondService 方法后,TS 抛出错误,“someMethod”不是 function。 我错过了什么/做错了什么? 除了 Jest,我还需要其他库吗?

过了一会儿,我发现了如何使用默认的玩笑行为来做到这一点。

首先,您需要在path/to/second/service/__mocks__中创建 SecondService 的模拟,例如:

// path/to/second/service/__mocks__/SecondService.ts
const mock = jest.fn().mockImplementation(() => ({
  async thingSecondServiceDoInFirstService(
    param: number
  ): number {
    return 1;

}));
export default mock;

SecondService 必须是默认导出,例如:

// path/to/second/service/SecondService.ts
    @Service()
export default class SecondService {
constructor(private thirdService: ThirdService) {}
  async thingSecondServiceDoInFirstService(
    param: number
  ): number {
    return this.thirdService.thingThirdServiceDoInSecond(param);
  }
}

在测试文件中,您必须在导入 SecondService之前使用 jest.mock,然后从 mock 创建 SecondService 实例:

jest.mock("path/to/second/service/SecondService");
import SecondService from "path/to/second/service/SecondService";
import MainService from "path/to/main/service/MainService";

describe("Test main service", () => {

  const SecondServiceMock = <jest.Mock<SecondService>>SecondService;
  let secondService = new SecondServiceMock();

  beforeEach(() => {
    mainService = new MainService(secondService);
  });

// test logic...
}

根据要求,不再需要 ThirdService。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM