Nest js 독학 삽질 일기 Day 10: 깃크라켄 사용법 익히기/ Validation / 테스트코드 짜기

Dorito·2022년 9월 25일
0

NestJS

목록 보기
10/10

PR머지함..

영머지 해서 main 브랜치를 미리 합쳐 줘서 충돌 파일 관리해줌..
(헷갈림..ㅎㅎ)

깃크라켄..

https://help.gitkraken.com/gitkraken-client/interface/

https://www.youtube.com/c/Gitkraken

https://velog.io/@danna-lee/개발-협업에서-깃-깃크라켄-사용하기

깃허브 삽질 휴..

머가 잘못됏는지 또 깃이 꼬엿는지 pr 다 하고 머지까지 다 끝냇는데 메인브랜치 보니까 환경변수랑 코드 썻던거 한참 전꺼로 돌아가잇다 .. ㅋㅋ

  1. env 환경변수 파일 설정 다시하기
  2. create api 수정하기

브랜치 따로 파야할듯

Validation

https://www.prisma.io/blog/nestjs-prisma-validation-7D056s1kOla1


Add validation rules to CreateBoardDto

참고 페이지
https://betterprogramming.pub/how-to-use-data-transfer-objects-dto-for-validation-in-nest-js-7ff95309f650

class-validator 목록들

  1. All elements should be string type.
  2. All elements can't be emty.
  3. description has to have a maximum length of 300.
import { IsNotEmpty, IsString, MaxLength } from 'class-validator';

export class CreateBoardDto {
  @IsNotEmpty()
  @IsString()
  readonly title: string;

  @IsNotEmpty()
  @IsString()
  @MaxLength(300)
  readonly description: string;

  @IsNotEmpty()
  @IsString()
  readonly body: string;

  @IsNotEmpty()
  @IsString()
  readonly author: string;
}

export class CreateBoardResponse {
  id: string;
}

whitelist: removes all properties of a request’s body which are not in the DTO
transform: this property would allow us to transform properties, for instance, an integer to a string. We do not cover this today.

import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));

  await app.listen(3000);
}
bootstrap();

깃허브 링크 (pr)

dotenv 재설정...

왜 파일이 날라갔는지는 모르겠지만~ 머지할 때 뭐 실수했나보지 뭐
복습겸 다시 브랜치 파서 만들고 있다 뚝딱뚝딱

내 깃헙 여기 참고하믄 댐... ㅎㅎ

테스트 코드 짜기...

Nestjs에서는 javascript 테스트 프레임워크인 jest를 기본 테스트 프레임워크로 지원
https://docs.nestjs.com/fundamentals/testing
https://circleci.com/blog/getting-started-with-nestjs-and-automatic-testing/
https://wikidocs.net/158681

참고해서 씀
설마 아직도 테스트 코드를 작성 안 하시나요?

spec 파일 이미 지웠는데... 되살리기

https://stackoverflow.com/questions/59090942/nestjs-how-to-generate-spec-ts-files-if-no-spec-used-to-disable-spec-files

걍 되살리는 법 없고 기본형 보고 내 상황에 맞게 따라서 적으면 됨

근데 귀차나서 더 찾아보니 이런게 있어서 깔아봄
https://marketplace.visualstudio.com/items?itemName=ashinzekene.nestjs

야호~ 짱편하다!

참고 페이지.. (내일 본격적으로 테스트 코드 써보기)

https://dev.to/tkssharma/unit-testing-and-integration-testing-nestjs-application-4d7a

Fakes: an object with limited capabilities (for the purposes of testing), e.g. a fake web service. Fake has business behavior. You can drive a fake to behave in different ways by giving it different data. Fakes can be used when you can’t use a real implementation in your test.

Mock: an object on which you set expectations. A mock has expectations about the way it should be called, and a test should fail if it’s not called that way. Mocks are used to test interactions between objects.

Stub: an object that provides predefined answers to method calls. A stub has no logic and only returns what you tell it to return.
In case you are interested, here is a good discussion on fake/mock/stub.

Spy: Spy, spies on the caller. Often used to make sure a particular method has been called.

그 외 내일 볼 페이지: tests for the AuthenticationService
노마드 코더 이분 영상에도 유닛 테스트 관련 영상이 있어서 내일 보자.
귀찮으니까 정리해두신 분 https://velog.io/@qmasem/TIL-노마드-코더-NestJS로-API-만들기-4 // 보면서 하기
https://wikidocs.net/158681
https://lab.cliel.com/entry/nestjs-spec%EA%B3%BC-testing
https://dailybook-with.tistory.com/entry/Nestjs-%EC%9D%98-%EC%9C%A0%EB%8B%9B-%ED%85%8C%EC%8A%A4%ED%8A%B8Unit-test

0개의 댓글