import { Test, TestingModule } from '@nestjs/testing'; import { TrackingService } from '@/tracking/tracking.service'; import { TrackingDto } from '@/dtos/tracking.dto'; import { TrackingController } from '@/tracking/tracking.controller'; describe('TrackingController', () => { let trackingInfo: TrackingDto; let controller: TrackingController; let trackingService: TrackingService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [TrackingController], }) .useMocker((token) => { if (token === TrackingService) { return { track: jest.fn().mockResolvedValue(true) }; } }) .compile(); controller = module.get(TrackingController); trackingService = module.get(TrackingService); trackingInfo = { carrierName: 'Evergreen', destinationUnLoCode: 'CNNBO', departureDate: new Date('2023-05-14T11:00:00.000Z'), arrivalDateEstimate: new Date('2023-02-14T15:00:00.000Z'), containerNumber: 'EGLU866139144', }; }); it('should be defined', () => { expect(controller).toBeDefined(); }); it('should have a method track', () => { expect(controller.track).toBeDefined(); }); it('should return an object with message: ok', async () => { await expect(controller.track(trackingInfo)).resolves.toEqual({ message: 'Ok', }); }); it('should pass on any exception thrown from tracking service', async () => { jest .spyOn(trackingService, 'track') .mockRejectedValue(new Error('Test Error')); await expect(controller.track(trackingInfo)).rejects.toThrow('Test Error'); }); });