diff --git a/jest.config.ts b/jest.config.ts new file mode 100644 index 0000000..b54ba92 --- /dev/null +++ b/jest.config.ts @@ -0,0 +1,60 @@ +/** + * For a detailed explanation regarding each configuration property, visit: + * https://jestjs.io/docs/configuration + */ +import type { Config } from 'jest'; +import { pathsToModuleNameMapper } from 'ts-jest'; +import { compilerOptions } from './tsconfig.json'; + + +const config: Config = { + // An array of file extensions your modules use + moduleFileExtensions: [ + "js", + "ts", + "json", + ], + + // modulePaths: [compilerOptions.baseUrl], + + // @see https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping/ + // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module + moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths , { prefix: '/' } ), + + // The root directory that Jest should scan for tests and modules within + rootDir: "src", + + + // A list of paths to modules that run some code to configure or set up the testing framework before each test + setupFilesAfterEnv: [], + + + // The test environment that will be used for testing + "testEnvironment": "node", + + // The glob patterns Jest uses to detect test files + testMatch: [ + "**/__tests__/**/*.[jt]s?(x)", + "**/?(*.)+(spec|test).[tj]s?(x)" + ], + + // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped + testPathIgnorePatterns: [ + "/node_modules/", "example/", "dist/" + ], + + // The regexp pattern or array of patterns that Jest uses to detect test files + "testRegex": ".*\\.spec\\.ts$", + + // A map from regular expressions to paths to transformers + transform: { + "^.+\\.(t|j)s$": "ts-jest" + }, + + // Indicates whether each individual test should be reported during the run + verbose: false, + + silent: false, +}; + +export default config; diff --git a/package-lock.json b/package-lock.json index b4a0dcf..7b8a5f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@nestjs/platform-fastify": "^10.3.3", "class-transformer": "^0.5.1", "class-validator": "^0.14.1", + "jsonc-parser": "^3.2.1", "reflect-metadata": "^0.2.1", "rxjs": "^7.8.1" }, @@ -91,6 +92,12 @@ } } }, + "node_modules/@angular-devkit/core/node_modules/jsonc-parser": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz", + "integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==", + "dev": true + }, "node_modules/@angular-devkit/schematics": { "version": "17.1.2", "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-17.1.2.tgz", @@ -236,6 +243,12 @@ "node": ">=0.12.0" } }, + "node_modules/@angular-devkit/schematics/node_modules/jsonc-parser": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz", + "integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==", + "dev": true + }, "node_modules/@babel/code-frame": { "version": "7.23.5", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.23.5.tgz", @@ -1991,12 +2004,6 @@ "typescript": ">=4.8.2" } }, - "node_modules/@nestjs/schematics/node_modules/jsonc-parser": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.1.tgz", - "integrity": "sha512-AilxAyFOAcK5wA1+LeaySVBrHsGQvUFCDWXKpZjzaL0PqW+xfBOttn8GNtWKFWqneyMZj41MWF9Kl6iPWLwgOA==", - "dev": true - }, "node_modules/@nestjs/testing": { "version": "10.3.3", "resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-10.3.3.tgz", @@ -6499,10 +6506,9 @@ } }, "node_modules/jsonc-parser": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz", - "integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==", - "dev": true + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.1.tgz", + "integrity": "sha512-AilxAyFOAcK5wA1+LeaySVBrHsGQvUFCDWXKpZjzaL0PqW+xfBOttn8GNtWKFWqneyMZj41MWF9Kl6iPWLwgOA==" }, "node_modules/jsonfile": { "version": "6.1.0", diff --git a/package.json b/package.json index e706a5f..64975b8 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", - "test": "jest", + "test": "jest --config ./jest.config.js", "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", @@ -51,22 +51,5 @@ "ts-node": "^10.9.2", "tsconfig-paths": "^4.2.0", "typescript": "^5.3.3" - }, - "jest": { - "moduleFileExtensions": [ - "js", - "json", - "ts" - ], - "rootDir": "src", - "testRegex": ".*\\.spec\\.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - }, - "collectCoverageFrom": [ - "**/*.(t|j)s" - ], - "coverageDirectory": "../coverage", - "testEnvironment": "node" } } diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts deleted file mode 100644 index d22f389..0000000 --- a/src/app.controller.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; - -describe('AppController', () => { - let appController: AppController; - - beforeEach(async () => { - const app: TestingModule = await Test.createTestingModule({ - controllers: [AppController], - providers: [AppService], - }).compile(); - - appController = app.get(AppController); - }); - - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); -}); diff --git a/src/app.controller.ts b/src/app.controller.ts deleted file mode 100644 index cce879e..0000000 --- a/src/app.controller.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Controller, Get } from '@nestjs/common'; -import { AppService } from './app.service'; - -@Controller() -export class AppController { - constructor(private readonly appService: AppService) {} - - @Get() - getHello(): string { - return this.appService.getHello(); - } -} diff --git a/src/app.module.ts b/src/app.module.ts index 8662803..a31edb4 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,10 +1,8 @@ +import { ControllerModule } from '#controllers/controller.module'; +import { ProviderModule } from '#providers/provider.module'; import { Module } from '@nestjs/common'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; @Module({ - imports: [], - controllers: [AppController], - providers: [AppService], + imports: [ControllerModule, ProviderModule], }) export class AppModule {} diff --git a/src/app.service.ts b/src/app.service.ts deleted file mode 100644 index 927d7cc..0000000 --- a/src/app.service.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Injectable } from '@nestjs/common'; - -@Injectable() -export class AppService { - getHello(): string { - return 'Hello World!'; - } -} diff --git a/src/controllers/controller.module.ts b/src/controllers/controller.module.ts new file mode 100644 index 0000000..2a67b91 --- /dev/null +++ b/src/controllers/controller.module.ts @@ -0,0 +1,7 @@ +import { HealthControllerModule } from '#controllers/healths/health.controller.module'; +import { Module } from '@nestjs/common'; + +@Module({ + imports: [HealthControllerModule], +}) +export class ControllerModule {} diff --git a/src/controllers/healths/health.controller.module.ts b/src/controllers/healths/health.controller.module.ts new file mode 100644 index 0000000..54f52f3 --- /dev/null +++ b/src/controllers/healths/health.controller.module.ts @@ -0,0 +1,10 @@ +import { HealthController } from '#controllers/healths/health.controller'; +import { HealthService } from '#providers/healths/health.service'; +import { Module } from '@nestjs/common'; + +@Module({ + imports: [], + controllers: [HealthController], + providers: [HealthService], +}) +export class HealthControllerModule {} diff --git a/src/controllers/healths/health.controller.spec.ts b/src/controllers/healths/health.controller.spec.ts new file mode 100644 index 0000000..586903f --- /dev/null +++ b/src/controllers/healths/health.controller.spec.ts @@ -0,0 +1,24 @@ +import { HealthController } from '#controllers/healths/health.controller'; +import { ResHealthDto } from '#dtos/healths/ResHealthDto'; +import { Test, TestingModule } from '@nestjs/testing'; + + + +describe('HealthController', () => { + let controller: HealthController; + const now = new Date(); + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [HealthController], + }).compile(); + + jest.setSystemTime(now); + + controller = module.get(HealthController); + }); + + it('should be "ResHealthDto"', () => { + expect(controller.getHealth()).toBe(new ResHealthDto({runMode: 'local', 'timestamp': now.toISOString()})); + }); +}); diff --git a/src/controllers/healths/health.controller.ts b/src/controllers/healths/health.controller.ts new file mode 100644 index 0000000..ff3b347 --- /dev/null +++ b/src/controllers/healths/health.controller.ts @@ -0,0 +1,13 @@ +import { ResHealthDto } from '#dtos/healths/ResHealthDto'; +import { HealthService } from '#providers/healths/health.service'; +import { Controller, Get } from '@nestjs/common'; + +@Controller(['/', 'healths']) +export class HealthController { + constructor(private readonly healthService: HealthService) {} + + @Get('/') + getHealth(): ResHealthDto { + return this.healthService.read(); + } +} diff --git a/src/dtos/healths/ResHealthDto.ts b/src/dtos/healths/ResHealthDto.ts new file mode 100644 index 0000000..449828d --- /dev/null +++ b/src/dtos/healths/ResHealthDto.ts @@ -0,0 +1,15 @@ +import { IResHealthDto } from '#dtos/healths/interfaces/IResHealthDto'; +import { IsISO8601, IsString } from 'class-validator'; + +export class ResHealthDto implements IResHealthDto { + @IsString() + runMode: string; + + @IsISO8601() + timestamp: string; + + constructor(args: IResHealthDto) { + this.runMode = args.runMode; + this.timestamp = new Date(args.timestamp).toISOString(); + } +} diff --git a/src/dtos/healths/interfaces/IResHealthDto.ts b/src/dtos/healths/interfaces/IResHealthDto.ts new file mode 100644 index 0000000..f4ae9de --- /dev/null +++ b/src/dtos/healths/interfaces/IResHealthDto.ts @@ -0,0 +1,4 @@ +export interface IResHealthDto { + runMode: string; + timestamp: string; +} diff --git a/src/main.ts b/src/main.ts index 5e1a4bb..f31e36a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ +import { AppModule } from '#app.module'; import { ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify'; -import { AppModule } from './app.module'; /** * 포트 번호를 가져오는 함수 diff --git a/src/providers/healths/health.service.module.ts b/src/providers/healths/health.service.module.ts new file mode 100644 index 0000000..115c266 --- /dev/null +++ b/src/providers/healths/health.service.module.ts @@ -0,0 +1,7 @@ +import { HealthService } from '#providers/healths/health.service'; +import { Module } from '@nestjs/common'; + +@Module({ + providers: [HealthService], +}) +export class HealthServiceModule {} diff --git a/src/providers/healths/health.service.spec.ts b/src/providers/healths/health.service.spec.ts new file mode 100644 index 0000000..5ddb3b3 --- /dev/null +++ b/src/providers/healths/health.service.spec.ts @@ -0,0 +1,25 @@ +import { IResHealthDto } from '#dtos/healths/interfaces/IResHealthDto'; +import { Test, TestingModule } from '@nestjs/testing'; +import { HealthService } from './health.service'; + +describe('HealthService', () => { + let service: HealthService; + const now = new Date(); + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [HealthService], + }).compile(); + + jest.setSystemTime(now); + + service = module.get(HealthService); + }); + + it('should be "IResHealthDto"', () => { + expect(service.read()).toBe({ + runMode: 'local', + timestamp: now.toISOString(), + } satisfies IResHealthDto); + }); +}); diff --git a/src/providers/healths/health.service.ts b/src/providers/healths/health.service.ts new file mode 100644 index 0000000..1cd3a58 --- /dev/null +++ b/src/providers/healths/health.service.ts @@ -0,0 +1,12 @@ +import { IResHealthDto } from '#dtos/healths/interfaces/IResHealthDto'; +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class HealthService { + read() { + return { + runMode: 'local', + timestamp: new Date().toISOString(), + } satisfies IResHealthDto; + } +} diff --git a/src/providers/provider.module.ts b/src/providers/provider.module.ts new file mode 100644 index 0000000..4fcfc66 --- /dev/null +++ b/src/providers/provider.module.ts @@ -0,0 +1,7 @@ +import { Module } from '@nestjs/common'; +import { HealthServiceModule } from './healths/health.service.module'; + +@Module({ + imports: [HealthServiceModule], +}) +export class ProviderModule {} diff --git a/tsconfig.json b/tsconfig.json index d5b292c..16e9083 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,7 @@ "compilerOptions": { // "module": "commonjs", // 사용할 라이브러리 추가 - "lib": ["dom.iterable"], + "lib": ["ES2022"], // 선언 파일 생성 "declaration": true, // 주석 제거 @@ -59,8 +59,16 @@ "noImplicitReturns": true, // JSON 모듈 해석 활성화 "resolveJsonModule": true, - // 경로 별칭 설정 - "paths": {}, + // path alias 설정 + // #을 prefix 로 설정한 이유는 아래 내용을 참조하였습니다 + // NOTE: https://github.com/nodejs/node/issues/49182 + "paths": { + "#app.module": ["src/app.module"], + // + "#controllers/*": ["src/controllers/*"], + "#dtos/*": ["src/dtos/*"], + "#providers/*": ["src/providers/*"], + }, }, // 포함할 파일 경로 설정 "include": ["src/**/*"],