diff --git a/package.json b/package.json index f5c3bc1..126906b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "license": "UNLICENSED", "scripts": { "build": "nest build", - "format": "prettier --write \"src/**/*.ts\" \"e2e-tests/**/*.ts\"", + "format": "prettier --write \"src/**/*.ts\" \"e2e-tests/**/*.ts\" \"tests/**/*.ts\"", "start": "nest start", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", @@ -67,7 +67,9 @@ "json", "ts" ], - "rootDir": "src", + "rootDir": ".", + "roots": ["/tests"], + "testPathIgnorePatterns": ["./database"], "testRegex": ".*\\.spec\\.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" @@ -78,7 +80,8 @@ "coverageDirectory": "../coverage", "testEnvironment": "node", "moduleNameMapper": { - "@/(.*)": "/$1" + "@/(.*)": "/src/$1", + "@tests/(.*)": "/tests/$1" } }, "lint-staged": { diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts deleted file mode 100644 index 4ac71e3..0000000 --- a/src/app.controller.spec.ts +++ /dev/null @@ -1 +0,0 @@ -describe('AppController', () => {}); diff --git a/src/tracking/notifications.service.spec.ts b/src/tracking/notifications.service.spec.ts deleted file mode 100644 index 4711910..0000000 --- a/src/tracking/notifications.service.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { NotificationsService } from './notifications.service'; - -describe('NotificationsService', () => { - let service: NotificationsService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [NotificationsService], - }).compile(); - - service = module.get(NotificationsService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/tests/data/tracking.mock.ts b/tests/data/tracking.mock.ts new file mode 100644 index 0000000..e4d8770 --- /dev/null +++ b/tests/data/tracking.mock.ts @@ -0,0 +1,30 @@ +import { TrackingDto } from '@/dtos/tracking.dto'; +import { ShipmentEntity } from '@/entities/shipment.entity'; +import * as uuid from 'uuid'; +import { ContainerEntity } from '@/entities/container.entity'; + +export const mockTrackingInfo: TrackingDto = { + carrierName: 'Evergreen', + destinationUnLoCode: 'NLAMS', + departureDate: new Date('2023-05-14T11:00:00.000Z'), + arrivalDateEstimate: new Date('2023-02-14T15:00:00.000Z'), + containerNumber: 'AAAA000000000', +}; + +export const mockShipment: ShipmentEntity = { + id: uuid.NIL, + createdAt: new Date('2023-05-01T11:00:00.000Z'), + carrierName: 'Evergreen', + originPort: 'NLAMS', + destinationPort: 'NLAMS', + departureDate: new Date('2023-05-14T11:00:00.000Z'), + arrivalDateOriginal: new Date('2023-02-14T15:00:00.000Z'), + arrivalDateEstimate: new Date('2023-02-14T15:00:00.000Z'), +}; + +export const mockContainerEntity: ContainerEntity = { + id: uuid.NIL, + shipmentId: uuid.NIL, + containerNumber: 'AAAA000000000', + shipment: mockShipment, +}; diff --git a/tests/tracking/notifications.service.spec.ts b/tests/tracking/notifications.service.spec.ts new file mode 100644 index 0000000..9e04bcd --- /dev/null +++ b/tests/tracking/notifications.service.spec.ts @@ -0,0 +1,34 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { NotificationsService } from '@/tracking/notifications.service'; +import { mockShipment, mockTrackingInfo } from '@tests/data/tracking.mock'; + +describe('NotificationsService', () => { + let service: NotificationsService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [NotificationsService], + }) + .useMocker((token) => { + switch (token) { + case 'NOTIFIER_FACTORY': + return () => ({ + send: jest.fn().mockResolvedValue(true), + }); + } + }) + .compile(); + + service = module.get(NotificationsService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); + + it('should have a method send that resolves', async () => { + await expect( + service.send(mockShipment, mockTrackingInfo), + ).resolves.toBeDefined(); + }); +}); diff --git a/src/tracking/tracking.controller.spec.ts b/tests/tracking/tracking.controller.spec.ts similarity index 95% rename from src/tracking/tracking.controller.spec.ts rename to tests/tracking/tracking.controller.spec.ts index b4bc653..d8d2c33 100644 --- a/src/tracking/tracking.controller.spec.ts +++ b/tests/tracking/tracking.controller.spec.ts @@ -1,7 +1,7 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { TrackingController } from './tracking.controller'; import { TrackingService } from '@/tracking/tracking.service'; import { TrackingDto } from '@/dtos/tracking.dto'; +import { TrackingController } from '@/tracking/tracking.controller'; describe('TrackingController', () => { let trackingInfo: TrackingDto; diff --git a/src/tracking/tracking.service.spec.ts b/tests/tracking/tracking.service.spec.ts similarity index 77% rename from src/tracking/tracking.service.spec.ts rename to tests/tracking/tracking.service.spec.ts index 5ff5887..aaf385c 100644 --- a/src/tracking/tracking.service.spec.ts +++ b/tests/tracking/tracking.service.spec.ts @@ -1,40 +1,17 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { TrackingService } from './tracking.service'; import { Repository } from 'typeorm'; import { ContainerEntity } from '@/entities/container.entity'; import { ShipmentsService } from '@/shipments/shipments.service'; import { NotificationsService } from '@/tracking/notifications.service'; import { getRepositoryToken } from '@nestjs/typeorm'; -import * as uuid from 'uuid'; -import { TrackingDto } from '@/dtos/tracking.dto'; -import { ShipmentEntity } from '@/entities/shipment.entity'; +import { TrackingService } from '@/tracking/tracking.service'; +import { + mockContainerEntity, + mockShipment, + mockTrackingInfo, +} from '@tests/data/tracking.mock'; + describe('TrackingService', () => { - const mockTrackingInfo: TrackingDto = { - carrierName: 'Evergreen', - destinationUnLoCode: 'NLAMS', - departureDate: new Date('2023-05-14T11:00:00.000Z'), - arrivalDateEstimate: new Date('2023-02-14T15:00:00.000Z'), - containerNumber: 'AAAA000000000', - }; - - const mockShipment: ShipmentEntity = { - id: uuid.NIL, - createdAt: new Date('2023-05-01T11:00:00.000Z'), - carrierName: 'Evergreen', - originPort: 'NLAMS', - destinationPort: 'NLAMS', - departureDate: new Date('2023-05-14T11:00:00.000Z'), - arrivalDateOriginal: new Date('2023-02-14T15:00:00.000Z'), - arrivalDateEstimate: new Date('2023-02-14T15:00:00.000Z'), - }; - - const mockContainerEntity: ContainerEntity = { - id: uuid.NIL, - shipmentId: uuid.NIL, - containerNumber: 'AAAA000000000', - shipment: mockShipment, - }; - const containerEntityToken = getRepositoryToken(ContainerEntity); let trackingService: TrackingService; let shipmentsService: ShipmentsService; diff --git a/src/utils/fakeDate.spec.ts b/tests/utils/fakeDate.spec.ts similarity index 100% rename from src/utils/fakeDate.spec.ts rename to tests/utils/fakeDate.spec.ts diff --git a/src/utils/uuidTransformer.spec.ts b/tests/utils/uuidTransformer.spec.ts similarity index 100% rename from src/utils/uuidTransformer.spec.ts rename to tests/utils/uuidTransformer.spec.ts diff --git a/tsconfig.json b/tsconfig.json index 16526c0..2bb343d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,7 @@ "lib": [ "es2022"], "sourceMap": true, "outDir": "./dist", - "baseUrl": "./src", + "baseUrl": "./", "incremental": true, "skipLibCheck": true, "strictNullChecks": true, @@ -22,10 +22,13 @@ "noUncheckedIndexedAccess": true, "paths": { "@/*": [ - "./*" + "./src/*" + ], + "@tests/*": [ + "./tests/*" ], "@": [ - "./main.ts" + "./src/main.ts" ] }