From b0c24d1c7d67ec07a277e0ddd262dea022567aa6 Mon Sep 17 00:00:00 2001 From: geunyoungno Date: Sat, 2 Mar 2024 14:21:52 +0900 Subject: [PATCH] =?UTF-8?q?=20*=20change=20jest=20setting=20(jest=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20=EB=B3=80=EA=B2=BD)=20=20=20=20*=20@see=20?= =?UTF-8?q?https://github.com/kulshekhar/ts-jest/issues/4198?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierrc | 11 ++--- .vscode/launch.json | 2 +- jest.config.ts | 4 +- package.json | 2 +- .../healths/health.controller.spec.ts | 8 +-- src/controllers/healths/health.controller.ts | 2 +- src/providers/healths/health.service.spec.ts | 3 +- tsconfig.json | 49 +++---------------- 8 files changed, 21 insertions(+), 60 deletions(-) diff --git a/.prettierrc b/.prettierrc index 208631e..0675e4f 100644 --- a/.prettierrc +++ b/.prettierrc @@ -12,15 +12,10 @@ } }, { - "files": [ - ".vscode/launch.json", - ".vscode/settings.json", - "tsconfig.prod.json", - "tsconfig.json", - ".prettierrc" - ], + "files": [".vscode/launch.json", ".vscode/settings.json", "tsconfig.prod.json", "tsconfig.json", ".prettierrc"], "options": { - "parser": "jsonc" + "parser": "json", + "trailingComma": "none" } } ] diff --git a/.vscode/launch.json b/.vscode/launch.json index 44c88a8..01866b8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,6 +9,6 @@ "request": "attach", "name": "Attach", "port": 9229 - }, + } ] } diff --git a/jest.config.ts b/jest.config.ts index b54ba92..7b08c6b 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -22,7 +22,7 @@ const config: Config = { moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths , { prefix: '/' } ), // The root directory that Jest should scan for tests and modules within - rootDir: "src", + // rootDir: "src", // A list of paths to modules that run some code to configure or set up the testing framework before each test @@ -44,7 +44,7 @@ const config: Config = { ], // The regexp pattern or array of patterns that Jest uses to detect test files - "testRegex": ".*\\.spec\\.ts$", + // "testRegex": ".*\\.spec\\.ts$", // A map from regular expressions to paths to transformers transform: { diff --git a/package.json b/package.json index 64975b8..ebd3cfc 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 --config ./jest.config.js", + "test": "jest --config ./jest.config.ts", "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", diff --git a/src/controllers/healths/health.controller.spec.ts b/src/controllers/healths/health.controller.spec.ts index 586903f..5afdb38 100644 --- a/src/controllers/healths/health.controller.spec.ts +++ b/src/controllers/healths/health.controller.spec.ts @@ -1,24 +1,24 @@ +import { AppModule } from '#app.module'; 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], + imports: [AppModule] }).compile(); + jest.useFakeTimers(); jest.setSystemTime(now); controller = module.get(HealthController); }); it('should be "ResHealthDto"', () => { - expect(controller.getHealth()).toBe(new ResHealthDto({runMode: 'local', 'timestamp': now.toISOString()})); + expect(controller.getHealth()).toStrictEqual(new ResHealthDto({runMode: 'local', 'timestamp': now.toISOString()})); }); }); diff --git a/src/controllers/healths/health.controller.ts b/src/controllers/healths/health.controller.ts index ff3b347..4df5cb5 100644 --- a/src/controllers/healths/health.controller.ts +++ b/src/controllers/healths/health.controller.ts @@ -8,6 +8,6 @@ export class HealthController { @Get('/') getHealth(): ResHealthDto { - return this.healthService.read(); + return new ResHealthDto(this.healthService.read()); } } diff --git a/src/providers/healths/health.service.spec.ts b/src/providers/healths/health.service.spec.ts index 5ddb3b3..b08d6f6 100644 --- a/src/providers/healths/health.service.spec.ts +++ b/src/providers/healths/health.service.spec.ts @@ -11,13 +11,14 @@ describe('HealthService', () => { providers: [HealthService], }).compile(); + jest.useFakeTimers(); jest.setSystemTime(now); service = module.get(HealthService); }); it('should be "IResHealthDto"', () => { - expect(service.read()).toBe({ + expect(service.read()).toStrictEqual({ runMode: 'local', timestamp: now.toISOString(), } satisfies IResHealthDto); diff --git a/tsconfig.json b/tsconfig.json index 16e9083..9db85cf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,77 +1,42 @@ { - // 확장 설정: Node.js 20 환경을 기반으로 하는 tsconfig.json 확장 "extends": "@tsconfig/node20/tsconfig.json", - // ts-node 설정: 타입 체크 생략하여 실행 속도 향상 "ts-node": { - // 타입 체크 생략 "transpileOnly": true, - // 파일 사용 여부 "files": true, - "compilerOptions": { - // 여기에 지정된 compilerOptions는 아래에 선언된 옵션을 재정의하지만, - // 오직 ts-node에서만 적용됩니다. - // 하나의 tsconfig.json으로 ts-node와 tsc가 서로 다른 옵션을 사용하도록 하려면 유용합니다. - }, + "compilerOptions": {} }, - // 컴파일러 옵션 설정 "compilerOptions": { - // "module": "commonjs", - // 사용할 라이브러리 추가 + "module": "ESNext", + "moduleResolution": "Node", "lib": ["ES2022"], - // 선언 파일 생성 "declaration": true, - // 주석 제거 "removeComments": true, - // 데코레이터 메타데이터 출력 "emitDecoratorMetadata": true, - // 실험적 데코레이터 활성화 "experimentalDecorators": true, - // 합성된 기본 가져오기 허용 "allowSyntheticDefaultImports": true, - // "target": "ES2021", - // 소스 맵 생성 "sourceMap": true, - // 출력 디렉터리 지정 "outDir": "./dist", - // baseUrl 설정 "baseUrl": "./", - // 증분 컴파일 활성화 "incremental": true, - // 라이브러리 검사 생략 "skipLibCheck": true, - // 엄격한 모드 활성화 "strict": true, - // null 체크 엄격화 "strictNullChecks": true, - // 암시적 any 금지 "noImplicitAny": false, - // 엄격한 함수 호출/바인딩/적용 규칙 미적용 "strictBindCallApply": false, - // 파일 이름에서 일관된 캐싱 강제 적용 "forceConsistentCasingInFileNames": true, - // switch 문에서 fallthrough 금지 "noFallthroughCasesInSwitch": true, - // 사용되지 않는 로컬 변수 사용 금지 "noUnusedLocals": true, - // 사용되지 않는 매개변수 사용 금지 "noUnusedParameters": true, - // 암시적 반환 금지 "noImplicitReturns": true, - // JSON 모듈 해석 활성화 "resolveJsonModule": true, - // 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/*"], - }, + "#providers/*": ["src/providers/*"] + } }, - // 포함할 파일 경로 설정 "include": ["src/**/*"], - // 제외할 파일 경로 설정 - "exclude": ["dist/**", "test/**"], + "exclude": ["dist/**", "test/**"] }