最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

testing - How to mock some controller functions? - Stack Overflow

programmeradmin1浏览0评论

I need to mock some functions in my controller, and reading the docks, looks like I should use the useMocker function to do that, the problem is that useMocker is never called.

This is my current setup:

reEach(async () => {
  const moduleRef = await Test.createTestingModule({
    imports: [NestjsFormDataModule, ConfigModule],
    controllers: [SubscriptionController],
  })
    .useMocker((token) => {
      console.log('TOKEN = ', token);
      if (token === SubscriptionController) {
        const mock = new SubscriptionControllerMock();
        mock.createCustomerPortalSession.mockImplementation(() => {});
        mock.createConfiguration.mockImplementation(() => {});
        mock.getConfigurations.mockImplementation(() => {});
        mock.subscribe.mockImplementation(() => {});
        mock.getConfiguration.mockImplementation(() => {});
        return mock;
      }
    })
    pile();

  app = moduleRef.createNestApplication();

  ({ agent } = await setupTestAuth(app, { admin: true }));
});

And the SubscriptionControllerMock class is just a class that extends the SubscriptionController but overrides the functions that I want to mock with jest.fn(), like this:

export class SubscriptionControllerMock extends SubscriptionController {
  createConfiguration = jest.fn();

  getConfiguration = jest.fn();

  getConfigurations = jest.fn();

  updateConfiguration = jest.fn();

  subscribe = jest.fn();

  deleteConfiguration = jest.fn();

  createCheckoutSession = jest.fn();

  createCustomerPortalSession = jest.fn();

  webhooks = jest.fn();
}

For some reason the useMocker is never called and I even tried using overrideProvider with the controller but also doesn't work and I can't find anywhere an example of someone that have done this. Please, help!

发布评论

评论列表(0)

  1. 暂无评论