Unit Testing
- function 단위로 테스트
- test 실행 스크립트
"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
- 자바스크립트 테스트 프레임워크
- [테스트할 파일명].spec.ts 파일에 작성
describe: test 표시
beforeEach: 각 테스트를 하기 전 실행
afterAll(): 테스트 후 실행
beforeAll(): 테스트 전 실행
it: individual test(개별 테스트)
it('shoud be 4', () => { // it(test 제목, function)
expect(2 + 3).toEqual(5); // 2+3이 5이길 기대
});
Service Test
import { NotFoundException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { MoviesService } from './movies.service';
describe('MoviesService', () => {
let service: MoviesService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [MoviesService],
}).compile();
service = module.get<MoviesService>(MoviesService);
});
it('should be defined', () => { //Service 의존성 주입 확인
expect(service).toBeDefined();
});
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: 2020,
});
const movie = service.getOne(1);
expect(movie).toBeDefined();
expect(movie.id).toEqual(1);
expect(movie.title).toEqual('Test Movie');
});
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');
}
});
});
describe('deleteOne', () => {
it('deletes a movies', () => {
service.create({
title: 'Test Movie',
genres: ['test'],
year: 2020,
});
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(1111);
} 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: 2020,
});
const afterCreate = service.getAll().length;
expect(afterCreate).toBeGreaterThan(beforeCreate);
});
});
describe('update', () => {
it('should update a movie', () => {
service.create({
title: 'Test Movie',
genres: ['test'],
year: 2020,
});
service.update(1, { title: 'Updated Test' });
const movie = service.getOne(1);
expect(movie.title).toEqual('Updated Test');
});
it('should throw a NotFoundException', () => {
try {
service.update(1111, {});
} catch (e) {
expect(e).toBeInstanceOf(NotFoundException);
}
});
});
});
- test 실행
npm run test:watch

- test 진행상황 확인
npm run test:cov

E2E
- 전체 시스템 테스트
- 요청(action, url 등) 결과 테스트
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
});
- 사용자 관점에서 특정 페이지가 나와야하는 경우 사용
- 사용자의 액션을 처음부터 끝까지 테스트
- /test/app.e2e.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
// 실제 구동 환경과 똑같이 적용
// testing 환경에 적용이 안되면 type 변환/유효성 확인이 일어나지 않아 에러
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
);
await app.init();
});
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Welcome to my Movie API');
});
describe('/movies', () => {
it('GET', () => {
return request(app.getHttpServer()).get('/movies').expect(200).expect([]);
});
it('POST 201', () => {
return request(app.getHttpServer())
.post('/movies')
.send({
title: 'Test',
year: 2020,
genres: ['test'],
})
.expect(201);
});
it('POST 400', () => {
return request(app.getHttpServer())
.post('/movies')
.send({
title: 'Test',
year: 2020,
genres: ['test'],
other: 'thing',
})
.expect(400);
});
it('DELETE', () => {
return request(app.getHttpServer()).delete('/movies').expect(404);
});
});
describe('/movies/:id', () => {
it('GET 200', () => {
return request(app.getHttpServer()).get('/movies/1').expect(200);
});
it('GET 404', () => {
return request(app.getHttpServer()).get('/movies/123').expect(404);
});
it('PATCH 200', () => {
return request(app.getHttpServer())
.patch('/movies/1')
.send({ title: 'Updated Test' })
.expect(200);
});
it('DELETE 200', () => {
return request(app.getHttpServer()).delete('/movies/1').expect(200);
});
});
});
- test 실행
npm run test:e2e

'개발 > JavaScript' 카테고리의 다른 글
| [JS] jQuery Ajax를 바닐라 JS fetch()로 바꾸기 (0) | 2022.09.14 |
|---|---|
| [NestJS] logger, Configuration (0) | 2022.09.02 |
| [NestJS] Modules, DI (0) | 2022.08.10 |
| [NestJS] DTO, 유효성 검사 Validation (0) | 2022.08.10 |
| [NestJS] Controller, Service (0) | 2022.08.10 |