├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── package-lock.json ├── package.json ├── src ├── app.module.ts ├── main.ts └── todo │ ├── dto │ ├── request │ │ ├── create-todo.dto.ts │ │ └── update-todo.dto.ts │ └── response │ │ ├── create-todo.dto.ts │ │ ├── find-todo.dto.ts │ │ └── update-todo.dto.ts │ ├── entities │ └── todo.entity.ts │ ├── todo.controller.spec.ts │ ├── todo.controller.ts │ ├── todo.module.ts │ ├── todo.repository.ts │ ├── todo.service.spec.ts │ └── todo.service.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | tsconfigRootDir: __dirname, 6 | sourceType: 'module', 7 | }, 8 | plugins: ['@typescript-eslint/eslint-plugin'], 9 | extends: [ 10 | 'plugin:@typescript-eslint/recommended', 11 | 'plugin:prettier/recommended', 12 | ], 13 | root: true, 14 | env: { 15 | node: true, 16 | jest: true, 17 | }, 18 | ignorePatterns: ['.eslintrc.js'], 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/explicit-module-boundary-types': 'off', 23 | '@typescript-eslint/no-explicit-any': 'off', 24 | }, 25 | }; 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json 36 | 37 | # Environment variables 38 | .env -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "semi": true, 4 | "useTabs": false, 5 | "tabWidth": 2, 6 | "trailingComma": "all", 7 | "printWidth": 80 8 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NESTJS-STUDY 2 | 3 | ### 전반적인 목표 4 | 5 | 주니어 멤버들이 듣는 스터디이므로 추후에 진행될 개인 프로젝트에 도움이 될 수 있도록, nestJS를 통해 API를 열어보는 것에 의의를 둠. 6 | 7 | ### 각 강의 회차 별 브랜치 8 | 9 | 각 강의 별로 그 날의 주요한 내용을 정리해두었음. 10 | 11 | - [10.06](https://github.com/woog2roid/nestjs-study/tree/10.06) 12 | - [10.27](https://github.com/woog2roid/nestjs-study/tree/10.27) 13 | - [11.10](https://github.com/woog2roid/nestjs-study/tree/11.10) 14 | - [11.17](https://github.com/woog2roid/nestjs-study/tree/11.17) 15 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/nest-cli", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src", 5 | "compilerOptions": { 6 | "deleteOutDir": true 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "todo-study", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "build": "nest build", 10 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 11 | "start": "nest start", 12 | "start:dev": "nest start --watch", 13 | "start:debug": "nest start --debug --watch", 14 | "start:prod": "node dist/main", 15 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 16 | "test": "jest", 17 | "test:watch": "jest --watch", 18 | "test:cov": "jest --coverage", 19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 20 | "test:e2e": "jest --config ./test/jest-e2e.json" 21 | }, 22 | "dependencies": { 23 | "@nestjs/common": "^9.0.0", 24 | "@nestjs/config": "^3.1.1", 25 | "@nestjs/core": "^9.0.0", 26 | "@nestjs/jwt": "^10.1.1", 27 | "@nestjs/mapped-types": "*", 28 | "@nestjs/passport": "^10.0.2", 29 | "@nestjs/platform-express": "^9.0.0", 30 | "@nestjs/swagger": "^7.1.12", 31 | "@nestjs/typeorm": "^10.0.0", 32 | "bcrypt": "^5.1.1", 33 | "class-transformer": "^0.5.1", 34 | "class-validator": "^0.14.0", 35 | "mysql2": "^3.6.1", 36 | "passport": "^0.6.0", 37 | "passport-jwt": "^4.0.1", 38 | "reflect-metadata": "^0.1.13", 39 | "rxjs": "^7.2.0", 40 | "swagger-ui-express": "^5.0.0", 41 | "typeorm": "^0.3.17" 42 | }, 43 | "devDependencies": { 44 | "@nestjs/cli": "^9.0.0", 45 | "@nestjs/schematics": "^9.0.0", 46 | "@nestjs/testing": "^9.0.0", 47 | "@types/express": "^4.17.13", 48 | "@types/jest": "29.2.4", 49 | "@types/node": "18.11.18", 50 | "@types/supertest": "^2.0.11", 51 | "@typescript-eslint/eslint-plugin": "^5.0.0", 52 | "@typescript-eslint/parser": "^5.0.0", 53 | "eslint": "^8.0.1", 54 | "eslint-config-prettier": "^8.3.0", 55 | "eslint-plugin-prettier": "^4.0.0", 56 | "jest": "29.3.1", 57 | "prettier": "^2.3.2", 58 | "source-map-support": "^0.5.20", 59 | "supertest": "^6.1.3", 60 | "ts-jest": "29.0.3", 61 | "ts-loader": "^9.2.3", 62 | "ts-node": "^10.0.0", 63 | "tsconfig-paths": "4.1.1", 64 | "typescript": "^4.7.4" 65 | }, 66 | "jest": { 67 | "moduleFileExtensions": [ 68 | "js", 69 | "json", 70 | "ts" 71 | ], 72 | "rootDir": "src", 73 | "testRegex": ".*\\.spec\\.ts$", 74 | "transform": { 75 | "^.+\\.(t|j)s$": "ts-jest" 76 | }, 77 | "collectCoverageFrom": [ 78 | "**/*.(t|j)s" 79 | ], 80 | "coverageDirectory": "../coverage", 81 | "testEnvironment": "node" 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { TodoModule } from './todo/todo.module'; 3 | import { ConfigModule } from '@nestjs/config'; 4 | import { TypeOrmModule } from '@nestjs/typeorm'; 5 | import { Todo } from './todo/entities/todo.entity'; 6 | 7 | @Module({ 8 | imports: [ 9 | ConfigModule.forRoot({ envFilePath: '.env', isGlobal: true }), 10 | TypeOrmModule.forRoot({ 11 | type: 'mysql', 12 | host: process.env.DB_HOST, 13 | port: +process.env.DB_PORT, 14 | username: process.env.DB_USERNAME, 15 | password: process.env.DB_PASSWORD, 16 | database: process.env.DB_DATABASE, 17 | entities: [Todo], 18 | migrations: [__dirname + '/src/migrations/*.ts'], 19 | autoLoadEntities: true, 20 | charset: 'utf8mb4', 21 | synchronize: process.env.NODE_ENV !== 'production', 22 | logging: process.env.NODE_ENV !== 'production', 23 | keepConnectionAlive: true, 24 | }), 25 | TodoModule, 26 | ], 27 | }) 28 | export class AppModule {} 29 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; 4 | import { ValidationPipe } from '@nestjs/common'; 5 | 6 | async function bootstrap() { 7 | const app = await NestFactory.create(AppModule); 8 | 9 | // Swagger 10 | const document_config = new DocumentBuilder() 11 | .setTitle('Blood-Mate Server') 12 | .setDescription('Blood-Mate API Description') 13 | .setVersion('1.0.0') 14 | .addBearerAuth() 15 | .build(); 16 | const document = SwaggerModule.createDocument(app, document_config); 17 | SwaggerModule.setup('docs', app, document); 18 | 19 | // Class Validator 20 | app.useGlobalPipes(new ValidationPipe()); 21 | 22 | await app.listen(3000); 23 | } 24 | bootstrap(); 25 | -------------------------------------------------------------------------------- /src/todo/dto/request/create-todo.dto.ts: -------------------------------------------------------------------------------- 1 | import { ApiProperty, PickType } from '@nestjs/swagger'; 2 | import { 3 | IsString, 4 | IsBoolean, 5 | MaxLength, 6 | MinLength, 7 | IsNotEmpty, 8 | } from 'class-validator'; 9 | import { Todo } from 'src/todo/entities/todo.entity'; 10 | 11 | export class CreateTodoRequestDto extends PickType(Todo, [ 12 | 'title', 13 | 'description', 14 | 'isDone', 15 | ]) { 16 | @IsNotEmpty() 17 | @IsString() 18 | @MinLength(1) 19 | @MaxLength(100) 20 | title!: string; 21 | 22 | @IsNotEmpty() 23 | @IsString() 24 | @MinLength(1) 25 | @MaxLength(100) 26 | description!: string; 27 | 28 | @IsNotEmpty() 29 | @IsBoolean() 30 | isDone!: boolean; 31 | } 32 | -------------------------------------------------------------------------------- /src/todo/dto/request/update-todo.dto.ts: -------------------------------------------------------------------------------- 1 | import { PartialType } from '@nestjs/swagger'; 2 | import { CreateTodoRequestDto } from './create-todo.dto'; 3 | import { BadRequestException } from '@nestjs/common'; 4 | 5 | export class UpdateTodoRequestDto extends PartialType(CreateTodoRequestDto) { 6 | static validateEmptyObject(dto: UpdateTodoRequestDto): void { 7 | if (Object.keys(dto).length === 0) 8 | throw new BadRequestException('빈 객체는 요청할 수 없습니다.'); 9 | return; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/todo/dto/response/create-todo.dto.ts: -------------------------------------------------------------------------------- 1 | import { OmitType } from '@nestjs/swagger'; 2 | import { Todo } from 'src/todo/entities/todo.entity'; 3 | 4 | export class CreateTodoResponseDto extends OmitType(Todo, ['deletedAt']) { 5 | static fromEntity(entity: Todo): CreateTodoResponseDto { 6 | const { deletedAt, ...properties } = entity; 7 | return { 8 | ...properties, 9 | }; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/todo/dto/response/find-todo.dto.ts: -------------------------------------------------------------------------------- 1 | import { OmitType } from '@nestjs/swagger'; 2 | import { Todo } from 'src/todo/entities/todo.entity'; 3 | 4 | export class FindTodoResponseDto extends OmitType(Todo, ['deletedAt']) { 5 | static fromEntity(entity: Todo): FindTodoResponseDto { 6 | const { deletedAt, ...properties } = entity; 7 | return { 8 | ...properties, 9 | }; 10 | } 11 | 12 | static fromEntities(entities: Todo[]): FindTodoResponseDto[] { 13 | return entities.map((entity) => FindTodoResponseDto.fromEntity(entity)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/todo/dto/response/update-todo.dto.ts: -------------------------------------------------------------------------------- 1 | import { OmitType } from '@nestjs/swagger'; 2 | import { Todo } from 'src/todo/entities/todo.entity'; 3 | 4 | export class UpdateTodoResponseDto extends OmitType(Todo, ['deletedAt']) { 5 | static fromEntity(entity: Todo): UpdateTodoResponseDto { 6 | const { deletedAt, ...properties } = entity; 7 | return { 8 | ...properties, 9 | }; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/todo/entities/todo.entity.ts: -------------------------------------------------------------------------------- 1 | import { ApiProperty } from '@nestjs/swagger'; 2 | import { 3 | Column, 4 | CreateDateColumn, 5 | DeleteDateColumn, 6 | Entity, 7 | JoinColumn, 8 | ManyToOne, 9 | PrimaryGeneratedColumn, 10 | UpdateDateColumn, 11 | } from 'typeorm'; 12 | 13 | @Entity({ schema: 'todo_study', name: 'todo' }) 14 | export class Todo { 15 | @ApiProperty({ example: 1, description: '할 일 고유 아이디' }) 16 | @PrimaryGeneratedColumn({ name: 'id', type: 'int' }) 17 | id: number; 18 | 19 | @ApiProperty({ example: '강의 자료 만들기', description: '할 일 제목' }) 20 | @Column({ name: 'title', type: 'varchar', length: 100 }) 21 | title: string; 22 | 23 | @ApiProperty({ 24 | example: 'GDSC KOREA UNIV. BackEnd STUDY', 25 | description: '할 일 설명', 26 | }) 27 | @Column({ name: 'description', type: 'varchar', length: 100 }) 28 | description: string; 29 | 30 | @ApiProperty({ example: false, description: '할 일 완료 여부' }) 31 | @Column({ name: 'is_done', type: 'boolean', default: false }) 32 | isDone: boolean; 33 | 34 | @ApiProperty({ example: '2021-10-10T00:00:00.000Z', description: '생성일' }) 35 | @CreateDateColumn() 36 | createdAt: Date; 37 | 38 | @ApiProperty({ example: '2021-10-10T00:00:00.000Z', description: '수정일' }) 39 | @UpdateDateColumn() 40 | updatedAt: Date; 41 | 42 | @ApiProperty({ example: null, description: '삭제일' }) 43 | @DeleteDateColumn() 44 | deletedAt: Date | null; 45 | } 46 | -------------------------------------------------------------------------------- /src/todo/todo.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { TodoController } from './todo.controller'; 3 | import { TodoService } from './todo.service'; 4 | 5 | describe('TodoController', () => { 6 | let controller: TodoController; 7 | 8 | beforeEach(async () => { 9 | const module: TestingModule = await Test.createTestingModule({ 10 | controllers: [TodoController], 11 | providers: [TodoService], 12 | }).compile(); 13 | 14 | controller = module.get(TodoController); 15 | }); 16 | 17 | it('should be defined', () => { 18 | expect(controller).toBeDefined(); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /src/todo/todo.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Controller, 3 | Get, 4 | Post, 5 | Body, 6 | Patch, 7 | Param, 8 | Delete, 9 | } from '@nestjs/common'; 10 | import { TodoService } from './todo.service'; 11 | import { CreateTodoRequestDto } from './dto/request/create-todo.dto'; 12 | import { UpdateTodoRequestDto } from './dto/request/update-todo.dto'; 13 | import { 14 | ApiCreatedResponse, 15 | ApiOkResponse, 16 | ApiOperation, 17 | ApiTags, 18 | } from '@nestjs/swagger'; 19 | import { CreateTodoResponseDto } from './dto/response/create-todo.dto'; 20 | import { FindTodoResponseDto } from './dto/response/find-todo.dto'; 21 | import { UpdateTodoResponseDto } from './dto/response/update-todo.dto'; 22 | import { Todo } from './entities/todo.entity'; 23 | 24 | @ApiTags('todo') 25 | @Controller('todo') 26 | export class TodoController { 27 | constructor(private readonly todoService: TodoService) {} 28 | 29 | @ApiOperation({ summary: '할 일 생성하기' }) 30 | @ApiCreatedResponse({ 31 | description: '생성된 할 일', 32 | type: CreateTodoResponseDto, 33 | }) 34 | @Post() 35 | async create( 36 | @Body() createRequestTodoDto: CreateTodoRequestDto, 37 | ): Promise { 38 | return CreateTodoResponseDto.fromEntity( 39 | await this.todoService.create(createRequestTodoDto), 40 | ); 41 | } 42 | 43 | @ApiOperation({ summary: '할 일 목록 조회하기' }) 44 | @ApiOkResponse({ 45 | description: '검색 요청한 할 일 목록', 46 | type: [FindTodoResponseDto], 47 | }) 48 | @Get() 49 | async findAll(): Promise { 50 | return FindTodoResponseDto.fromEntities(await this.todoService.findAll()); 51 | } 52 | 53 | @ApiOperation({ summary: 'id로 할 일 조회하기' }) 54 | @ApiOkResponse({ 55 | description: '검색 요청한 할 일', 56 | type: FindTodoResponseDto, 57 | }) 58 | @Get(':id') 59 | async findById(@Param('id') id: string): Promise { 60 | // await this.todoService.validateExistenceById(+id); 61 | const todo: Todo = await this.todoService.findById(+id); 62 | return FindTodoResponseDto.fromEntity(todo); 63 | } 64 | 65 | @ApiOperation({ summary: '할 일 수정하기' }) 66 | @ApiOkResponse({ 67 | description: '수정된 할 일 결과물', 68 | type: UpdateTodoResponseDto, 69 | }) 70 | @Patch(':id') 71 | async updateById( 72 | @Param('id') id: string, 73 | @Body() updateTodoRequestDto: UpdateTodoRequestDto, 74 | ): Promise { 75 | UpdateTodoRequestDto.validateEmptyObject(updateTodoRequestDto); 76 | // await this.todoService.validateExistenceById(+id); 77 | const updatedTodo: Todo = await this.todoService.updateById( 78 | +id, 79 | updateTodoRequestDto, 80 | ); 81 | return UpdateTodoResponseDto.fromEntity(updatedTodo); 82 | } 83 | 84 | @ApiOperation({ summary: '할 일 삭제하기' }) 85 | @ApiOkResponse() 86 | @Delete(':id') 87 | async deleteById(@Param('id') id: string): Promise { 88 | // await this.todoService.validateExistenceById(+id); 89 | return this.todoService.deleteById(+id); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/todo/todo.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { TodoService } from './todo.service'; 3 | import { TodoController } from './todo.controller'; 4 | import { TypeOrmModule } from '@nestjs/typeorm'; 5 | import { Todo } from './entities/todo.entity'; 6 | import { TodoRepository } from './todo.repository'; 7 | 8 | @Module({ 9 | imports: [TypeOrmModule.forFeature([Todo])], 10 | controllers: [TodoController], 11 | providers: [TodoService, TodoRepository], 12 | }) 13 | export class TodoModule {} 14 | -------------------------------------------------------------------------------- /src/todo/todo.repository.ts: -------------------------------------------------------------------------------- 1 | import { Repository } from 'typeorm'; 2 | import { Todo } from './entities/todo.entity'; 3 | import { InjectRepository } from '@nestjs/typeorm'; 4 | import { Injectable } from '@nestjs/common'; 5 | 6 | @Injectable() 7 | export class TodoRepository extends Repository { 8 | constructor( 9 | @InjectRepository(Todo) private readonly repository: Repository, 10 | ) { 11 | super(repository.target, repository.manager); 12 | } 13 | 14 | async findById(id: number): Promise { 15 | return await this.repository.findOne({ where: { id } }); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/todo/todo.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { TodoService } from './todo.service'; 3 | 4 | describe('TodoService', () => { 5 | let service: TodoService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [TodoService], 10 | }).compile(); 11 | 12 | service = module.get(TodoService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/todo/todo.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, NotFoundException } from '@nestjs/common'; 2 | import { CreateTodoRequestDto } from './dto/request/create-todo.dto'; 3 | import { UpdateTodoRequestDto } from './dto/request/update-todo.dto'; 4 | import { TodoRepository } from './todo.repository'; 5 | import { Todo } from './entities/todo.entity'; 6 | 7 | @Injectable() 8 | export class TodoService { 9 | constructor(private readonly todoRepository: TodoRepository) {} 10 | 11 | /* Todo를 생성 */ 12 | create(createTodoDto: CreateTodoRequestDto): Promise { 13 | const todo = new Todo(); 14 | todo.title = createTodoDto.title; 15 | todo.description = createTodoDto.description; 16 | todo.isDone = createTodoDto.isDone; 17 | return this.todoRepository.save(todo); 18 | } 19 | 20 | /* 모든 Todo 목록을 조회 */ 21 | findAll(): Promise { 22 | return this.todoRepository.find(); 23 | } 24 | 25 | /* id를 통해 특정 Todo를 조회 */ 26 | async findById(id: number): Promise { 27 | const todo: Todo = await this.todoRepository.findById(id); 28 | if (todo == null) { 29 | throw new NotFoundException(`id가 ${id}인 todo가 없습니다.`); 30 | } 31 | return todo; 32 | } 33 | 34 | /* id를 통해 특정 Todo를 수정 */ 35 | async updateById( 36 | id: number, 37 | updateTodoDto: UpdateTodoRequestDto, 38 | ): Promise { 39 | const { title, description, isDone } = updateTodoDto; 40 | 41 | const todo: Todo = await this.todoRepository.findById(id); 42 | if (todo == null) { 43 | throw new NotFoundException(`id가 ${id}인 todo가 없습니다.`); 44 | } 45 | 46 | todo.title = title ? title : todo.title; 47 | todo.description = description ? description : todo.description; 48 | todo.isDone = isDone ? isDone : todo.isDone; 49 | 50 | return this.todoRepository.save(todo); 51 | } 52 | 53 | /* id를 통해 특정 Todo를 (논리적) 삭제 */ 54 | async deleteById(id: number): Promise { 55 | const todo: Todo = await this.todoRepository.findById(id); 56 | if (todo == null) { 57 | throw new NotFoundException(`id가 ${id}인 todo가 없습니다.`); 58 | } 59 | 60 | this.todoRepository.softDelete(id); 61 | return; 62 | } 63 | 64 | /* 65 | // 특정 id가 실제로 존재하는지 검증 66 | async validateExistenceById(id: number): Promise { 67 | const todo: Todo = await this.todoRepository.findById(id); 68 | if (todo == null) { 69 | throw new NotFoundException(`id가 ${id}인 todo가 없습니다.`); 70 | } 71 | } 72 | */ 73 | } 74 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | --------------------------------------------------------------------------------