๐ฆพ ๊ฐ์ ํ๊ธฐ
- ํํธ 2์์๋ Jest๋ฅผ ์ด์ฉํ ์ ๋ ํ
์คํธ์ e2e ํ
์คํธ๋ฅผ ์งํํ๋ค.
- Jest๋ ์๋ฐ์คํฌ๋ฆฝํธ ํ
์คํ
ํ๋ ์์ํฌ๋ก .spce.tsย ๋ก ๋์ด ์๋ ํ์ผ์ด ํ
์คํธ ํ์ผ์ด๋ค.
- Jest๋ .spec.ts ํ์ผ๋ค์ ์ฐพ์๋ณผ ์ ์๋๋ก ์ค์ ๋์ด ์๋ค.
- ์ ๋ํ
์คํธ๋ function์ ๋ฐ๋ก ํ
์คํธํ๋ ๊ฒ์ผ๋ก ์๋น์ค์์ ๋ถ๋ฆฌ๋ ์ ๋์ ํ
์คํธํ๋ ๊ฒ์ด๋ค.
- e2e ํ
์คํธ๋ ํ์ด์ง๋ก ๊ฐ๋ฉด ํน์ ํ์ด์ง๊ฐ ๋์์ผ ํ๋ ์ฌ์ฉ์ ๊ด์ ์ ํ
์คํธ๋ก ์ฌ์ฉ์๊ฐ ์ทจํ ๋งํ ์ก์
์ ํ์ธ ์ ์ฒด ์์คํ
์ ํ
์คํธ์ด๋ค.
- ์ฒ์ Jest๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ ํ
์คํธ์ e2eํ
์คํธ๋ฅผ ์งํํด๋ณด์๋๋ฐ ๊ฐ๋ฐ์๋ค์ด ์ด๋ป๊ฒ ํจ์จ์ ์ด๊ณ ์๋ฌ๊ฐ ์๋ ๊ฐ๋ฐ์ ํ ์ ์์๋์ง ์ค์ง์ ์ผ๋ก ๋ฐฐ์ธ ์ ์๋ ๊ฐ์์๋ค.
๐ฆ ์ฝ๋
import { Test, TestingModule } from '@nestjs/testing';
import { MoviesService } from './movies.service';
import { NotFoundException } from '@nestjs/common';
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', () => {
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: 2000,
});
const movie = service.getOne(1);
expect(movie).toBeDefined();
});
it('should throw 404 error', () => {
try {
service.getOne(999);
} catch (e) {
expect(e).toBeInstanceOf(NotFoundException);
}
});
});
describe('deleteOne', () => {
it('deletes a movie', () => {
service.create({
title: 'Test Movie',
genres: ['test'],
year: 2000,
});
const beforeDelete = service.getAll().length;
service.deleteOne(1);
const afterDelete = service.getAll().length;
expect(afterDelete).toBeLessThan(beforeDelete);
});
it('should throw a NotFoundException', () => {
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: 2000,
});
const afterCreate = service.getAll().length;
expect(afterCreate).toBeGreaterThan(beforeCreate);
});
});
describe('update', () => {
it('should update a movie', () => {
service.create({
title: 'Test Movie',
genres: ['test'],
year: 2000,
});
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(999, {});
} catch (e) {
expect(e).toBeInstanceOf(NotFoundException);
}
});
});
});
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();
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: 2000,
genres: ['test'],
})
.expect(201);
});
it('POST 400', () => {
return request(app.getHttpServer())
.post('/movies')
.send({
title: 'Test',
year: 2000,
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/999')
.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);
});
});
});