Skip to content

Commit

Permalink
* add directory (directory 추가)
Browse files Browse the repository at this point in the history
 * setting path alias (path alias 설정)
 * add health api (health api 추가)
  • Loading branch information
geunyoungno committed Mar 4, 2024
1 parent 4fa61e6 commit f55e665
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 79 deletions.
60 changes: 60 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/' } ),

// 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;
26 changes: 16 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 1 addition & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
22 changes: 0 additions & 22 deletions src/app.controller.spec.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/app.controller.ts

This file was deleted.

8 changes: 3 additions & 5 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
8 changes: 0 additions & 8 deletions src/app.service.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/controllers/controller.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HealthControllerModule } from '#controllers/healths/health.controller.module';
import { Module } from '@nestjs/common';

@Module({
imports: [HealthControllerModule],
})
export class ControllerModule {}
10 changes: 10 additions & 0 deletions src/controllers/healths/health.controller.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
24 changes: 24 additions & 0 deletions src/controllers/healths/health.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(HealthController);
});

it('should be "ResHealthDto"', () => {
expect(controller.getHealth()).toBe(new ResHealthDto({runMode: 'local', 'timestamp': now.toISOString()}));
});
});
13 changes: 13 additions & 0 deletions src/controllers/healths/health.controller.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
15 changes: 15 additions & 0 deletions src/dtos/healths/ResHealthDto.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 4 additions & 0 deletions src/dtos/healths/interfaces/IResHealthDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IResHealthDto {
runMode: string;
timestamp: string;
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
* 포트 번호를 가져오는 함수
Expand Down
7 changes: 7 additions & 0 deletions src/providers/healths/health.service.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HealthService } from '#providers/healths/health.service';
import { Module } from '@nestjs/common';

@Module({
providers: [HealthService],
})
export class HealthServiceModule {}
25 changes: 25 additions & 0 deletions src/providers/healths/health.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(HealthService);
});

it('should be "IResHealthDto"', () => {
expect(service.read()).toBe({
runMode: 'local',
timestamp: now.toISOString(),
} satisfies IResHealthDto);
});
});
12 changes: 12 additions & 0 deletions src/providers/healths/health.service.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions src/providers/provider.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { HealthServiceModule } from './healths/health.service.module';

@Module({
imports: [HealthServiceModule],
})
export class ProviderModule {}
14 changes: 11 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"compilerOptions": {
// "module": "commonjs",
// 사용할 라이브러리 추가
"lib": ["dom.iterable"],
"lib": ["ES2022"],
// 선언 파일 생성
"declaration": true,
// 주석 제거
Expand Down Expand Up @@ -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/**/*"],
Expand Down

0 comments on commit f55e665

Please sign in to comment.