Home > Back-end >  How to equal / validate return statusCode jest testing in nest js
How to equal / validate return statusCode jest testing in nest js

Time:08-12

This is the first time of mine to write a test case. I am writing a test case for my delete controller method. and Thia is my remove controller method.

@Delete(':ccAdminId')
  @HttpCode(200)
  async removeCcAdmin(@Param('ccAdminId') ccAdminId: string) {
    await this.ccAdminService.remove(ccAdminId);
  }
}

In this method, nothing returns except statusCode. So I just need to write a test cast to verify this statusCode. and This is my Test case.

describe('CcAdminController Unit Tests', () => {
  let ccAdminController: CcAdminController;
  let spyService: CcAdminService;

  beforeAll(async () => {
    const ApiServiceProvider = {
      provide: CcAdminService,
      useFactory: () => ({
        remove: jest.fn().mockImplementation((ccAdminId: string) => Promise.resolve(ccAdminId)),
      }),
    };
    const app: TestingModule = await Test.createTestingModule({
      controllers: [CcAdminController],
      providers: [CcAdminService, ApiServiceProvider],
    }).compile();

    ccAdminController = app.get<CcAdminController>(CcAdminController);
    spyService = app.get<CcAdminService>(CcAdminService);
  });

  it('calling removeCcAdmin method', async () => {
    const ccAdminId = '0001';
    await ccAdminController.removeCcAdmin(ccAdminId).then((result) => {
      expect(result).toEqual(HttpCode(200));
    });
    expect(spyService.remove).toHaveBeenCalled();
  });
});

But My Test Case is not working. Can anyone help me with this..? Thank you.

  • Related