Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 그래픽
- asynchronization
- 이용현황분석
- await
- R 그래프
- 10172
- 버스분석
- react #회원가입 #비밀번호비교
- 값추가
- R데이터형태
- 배열추가
- 광명시버스분석
- barplot in r
- setstate
- 배열삭제
- 값삭제
- DataFrame
- plot in r
- 백준 10172
- getline
- vetor
- 데이터분석
- 이스케이프시퀀스
- 백준 11718
- 그대로 출력하기
- useState
- React
- barplot
- 백준
- 탈출문자
Archives
- Today
- Total
devlog_zz
[NestJS] #3 Unit Testing 본문
728x90
3.0 Introduction to Testing in Nest
package.json
...
"test": "jest",
"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",
"test:e2e": "jest --config ./test/jest-e2e.json"
...
Jest : 자바스크립트를 쉽게 테스팅하는 npm 패키지이다.
~.spec.ts 는 테스트를 포함한 파일이다.
테스팅하고 싶은 파일명에 .spec.ts 붙이면 된다.
- 유닛 테스팅서비스에서 분리된 유닛을 테스트하는 것
- function 하나를 따로 따로 테스트하는 것
- E2E 테스팅사용자가 취할만한 액션들을 처음부터 끝까지 테스트하는 것
- ex ) 이페이지 가면 특정 페이지가 나와야 하는 경우
- 모든 시스템을 테스팅하는 것
3.1 Your first Unit Test
movies.service.spec.ts
...
it("should be 4",() => {
expect(2+2).toEqual(4)
})
...
it : individual test ( 개별 테스트 )
테스트하고 싶어하는 부분을 테스트하는 function을 만든다.
npm run test:watch
파일을 저장하면 test 통과하는지 자동 실행한다.
성공시
실패시
3.2 Testing getAll and getOne
movies.service.spec.ts
describe("getAll", () => {
it('should return an array',() => {
const result = service.getAll();
expect(result).toBeInstanceOf(Array)
})
})
describe("getOne()",() => {
it("should return a movie",() => {
service.create({title:"Test Movie",genres:['test'],year:2022})
const movie = service.getOne(1)
expect(movie).toBeDefined()
expect(movie.id).toEqual(1)
})
it('should throw 404 error',() => {
try{
service.getOne(999);
}catch(e){
expect(e).toBeInstanceOf(NotFoundException)
expect(e.message).toEqual('Movie with id 999 Not Found')
}
})
})
3.3 Testing delete and create
describe("deleteOne()",() => {
it("deletes a movie",() => {
service.create({title:"Test Movie",genres:['test'],year:2022})
const beforeDelete = service.getAll().length
service.deleteOne(1)
const afterDelete = service.getAll().length
expect(afterDelete).toBeLessThan(beforeDelete);
})
it("should return a 404",() => {
try{
service.deleteOne(999);
} catch(e){
expect(e).toBeInstanceOf(NotFoundException)
}
})
})
describe("create",() => {
it('should create a movie',() => {
const beforeCreate = service.getAll().length
service.create({title:"Test Movie",genres:['test'],year:2022})
const afterCreate = service.getAll().length
expect(afterCreate).toBeGreaterThan(beforeCreate)
})
})
3.4 Testing Update
describe('update', () => {
it("should update a movie",() => {
service.create({title:"Test Movie",genres:['test'],year:2022})
service.update(1,{title:"Update Test"})
const updatedMovie = service.getOne(1)
expect(updatedMovie.title).toEqual("Update Test")
})
it("should return a 404",() => {
try{
service.update(999,{title:"Update Test"});
} catch(e){
expect(e).toBeInstanceOf(NotFoundException)
}
})
})
beforeEach, afterEach, beforeAll, afterAll 등 많을 hook 활용해서 test 해볼 수 있다.
ex) afterAll : clear testing case
출처
728x90
'Back End > NestJS' 카테고리의 다른 글
[NestJS] #4 E2E Testing (0) | 2022.09.15 |
---|---|
[NestJS] #2 Rest API (0) | 2022.09.15 |
[NestJS] #1 Architecture of NestJS (0) | 2022.09.15 |
[NestJS] #0 INTRODUCTION (0) | 2022.09.15 |
Comments