How to mock third party library using jest

半城伤御伤魂 提交于 2020-02-25 09:48:30

问题


I am developing a node.js application using nestjs I have a class called LoggerService as below

export class LoggerService {

    private logger: Rollbar;

    constructor() {
        this.logger = this.setupLogger();
    }

    critical(...args: Array<string | Error | object | Date | any[]>) {
        this.logger.error(...args);
    }

    private setupLogger(): Rollbar {
        if (this.logger == null) {

            this.logger = new Rollbar({
                accessToken: 'token',
                environment: 'dev',
                captureUncaught: true,
                captureUnhandledRejections: true,
            });

        }

        return this.logger;
    }

Now I am writing unit test for this class using jest as below.

describe('LoggerService.log', () => {
  let service: LoggerService;

  beforeEach(async () => {

    const module: TestingModule = await Test.createTestingModule({
      providers: [LoggerService],
    }).compile();

    service = module.get<LoggerService>(LoggerService);
  });

  it('critical', () => {
    service.critical('error','message');
    expect(???).toHaveBeenCalledWith('error', 'message')
  })

);

My question is how to check(expect) if logger.error is called, or how to mock Rollbar in this class.


回答1:


1) Provide your external dependency/library as an injectable token in your module

@Module({
  providers: [
    {
      provide: 'Rollbar',
      useFactory: async (configService: ConfigService) => new Rollbar({
                accessToken: configService.accessToken,
                environment: configService.environment,
                captureUncaught: true,
                captureUnhandledRejections: true,
            }),
      inject: [ConfigService],
    },
  ]

2) Inject it in your LoggerService instead of creating it

export class LoggerService {
    constructor(@Inject('Rollbar') private logger: Rollbar) {
    }

3) Now you can mock your dependency in your test

const module: TestingModule = await Test.createTestingModule({
  providers: [
    LoggerService,
    { provide: 'Rollbar', useFactory: rollbarMockFactory },
  ],
}).compile();


来源:https://stackoverflow.com/questions/57380314/how-to-mock-third-party-library-using-jest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!