ํฐ์คํ ๋ฆฌ ๋ทฐ
๐งช NestJS Testing – ์ ๋ ํ ์คํธ๋ถํฐ ํตํฉ ํ ์คํธ๊น์ง ์์ ์ ๋ณต
octo54 2025. 5. 22. 15:46๐งช NestJS Testing – ์ ๋ ํ ์คํธ๋ถํฐ ํตํฉ ํ ์คํธ๊น์ง ์์ ์ ๋ณต
NestJS๋ ํ
์คํธ๋ฅผ ํ๋ ์์ํฌ ์์ค์์ ๊ฐ๋ ฅํ๊ฒ ์ง์ํ๋ ๊ตฌ์กฐ๋ฅผ ๊ฐ๊ณ ์์ต๋๋ค.
์ด๋ฒ ๊ธ์ NestJS ๊ณต์ ๋ฌธ์ Testing์ ๋ฐํ์ผ๋ก,
์ ๋ ํ
์คํธ(Unit Test), ํตํฉ ํ
์คํธ(Integration Test), ๊ทธ๋ฆฌ๊ณ E2E ํ
์คํธ๋ฅผ ์ค๋ฌด ์ค์ฌ์ผ๋ก ๊ตฌ์ฑํ์ต๋๋ค.
๐ก “ํ ์คํธ๋ ์ ํ์ด ์๋ ํ์์ ๋๋ค. NestJS๋ DI์ ๋ชจ๋ ์์คํ ๋๋ถ์ ํ ์คํธ ์์ฑ์ด ๋งค์ฐ ์ฝ์ต๋๋ค.”
โ ํ ์คํธ ํ๊ฒฝ ์ค๋น
NestJS๋ ๊ธฐ๋ณธ์ ์ผ๋ก Jest๋ฅผ ํ ์คํธ ๋ฌ๋๋ก ์ฌ์ฉํฉ๋๋ค.
โ ์ค์ ํ์ผ
NestJS ํ๋ก์ ํธ ์์ฑ ์ ์๋ ํฌํจ:
- jest.config.ts
- test/jest-e2e.json
โ ์ค์น ํ์ธ
npm run test # ๋จ์ ํ
์คํธ ์คํ
npm run test:watch # ๋ณ๊ฒฝ ๊ฐ์ง ๋ชจ๋
npm run test:cov # ์ปค๋ฒ๋ฆฌ์ง ํฌํจ ์คํ
npm run test:e2e # E2E ํ
์คํธ ์คํ
1๏ธโฃ ์ ๋ ํ ์คํธ (Unit Test)
โ ์๋น์ค ๋จ์ ํ ์คํธ ์์
// cats.service.ts
@Injectable()
export class CatsService {
getCats() {
return ['๊ณ ์์ด1', '๊ณ ์์ด2'];
}
}
// cats.service.spec.ts
describe('CatsService', () => {
let service: CatsService;
beforeEach(() => {
service = new CatsService();
});
it('should return an array of cats', () => {
expect(service.getCats()).toEqual(['๊ณ ์์ด1', '๊ณ ์์ด2']);
});
});
๐งช ์์กด์ฑ์ด ์๋ ์๋น์ค๋ new ์ฐ์ฐ์๋ก ๋ฐ๋ก ์์ฑํ์ฌ ํ ์คํธ ๊ฐ๋ฅ
๐บ ๊ด๊ณ
์ด ๊ตฌ๊ฐ์ ํ ์คํธ ์ฝ๋๊ฐ ์ค์ ์คํ๋๋ ์์ ๊ฐ ๋ฑ์ฅํ๋ ๊ตฌ๊ฐ์ผ๋ก
์ ๋์ผ์ค ๊ด๊ณ ์ฝ์ ์ ์ ํฉํ ๊ณ ์ฃผ๋ชฉ ์์ญ์ ๋๋ค.
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXX"
data-ad-slot="YYYYYY"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
2๏ธโฃ ์์กด์ฑ ์ฃผ์ ์ด ์๋ ์ ๋ ํ ์คํธ
โ DI๊ฐ ํ์ํ ๊ฒฝ์ฐ TestingModule ์ฌ์ฉ
// cats.service.ts
@Injectable()
export class CatsService {
constructor(private readonly httpService: HttpService) {}
async fetchCats(): Promise<string[]> {
const res = await this.httpService.get('https://api.example.com/cats').toPromise();
return res.data;
}
}
</string[]>
โ ํ ์คํธ ์ค์
describe('CatsService (with DI)', () => {
let service: CatsService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
CatsService,
{
provide: HttpService,
useValue: {
get: jest.fn().mockReturnValue({
toPromise: () => Promise.resolve({ data: ['cat1', 'cat2'] }),
}),
},
},
],
}).compile();
service = module.get<CatsService>(CatsService);
});
it('should fetch cats', async () => {
expect(await service.fetchCats()).toEqual(['cat1', 'cat2']);
});
});
โ Test.createTestingModule()์ ์ฌ์ฉํ๋ฉด NestJS์ DI ์์คํ ์ ํ ์คํธ์์ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
3๏ธโฃ ์ปจํธ๋กค๋ฌ + ์๋น์ค ํตํฉ ํ ์คํธ
// cats.controller.ts
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}
@Get()
getCats() {
return this.catsService.getCats();
}
}
โ ํ ์คํธ ์ค์
describe('CatsController', () => {
let controller: CatsController;
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [CatsController],
providers: [
{
provide: CatsService,
useValue: {
getCats: jest.fn().mockReturnValue(['ํ
์คํธ ๊ณ ์์ด']),
},
},
],
}).compile();
controller = module.get<CatsController>(CatsController);
});
it('should return cats', () => {
expect(controller.getCats()).toEqual(['ํ
์คํธ ๊ณ ์์ด']);
});
});
4๏ธโฃ E2E ํ ์คํธ (End-to-End)
NestJS๋ E2E ํ ์คํธ๋ฅผ supertest์ ํจ๊ป ๊ธฐ๋ณธ ์ ๊ณตํ๋ฉฐ test/app.e2e-spec.ts์ ๊ธฐ๋ณธ ํ ํ๋ฆฟ์ด ํฌํจ๋ฉ๋๋ค.
โ ์์
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { AppModule } from '../src/app.module';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('/cats (GET)', () => {
return request(app.getHttpServer())
.get('/cats')
.expect(200)
.expect(['๊ณ ์์ด1', '๊ณ ์์ด2']);
});
});
๐ง ์ค๋ฌด ์ ๋ต
ํ ์คํธ ์ ํ ๋ชฉ์ ๋๊ตฌ
Unit Test | ๋จ์ผ ํจ์, ์๋น์ค ๋ก์ง ๊ฒ์ฆ | Jest |
Integration | ์ฌ๋ฌ ๋ชจ๋, DI ํฌํจ ์๋น์ค ํ ์คํธ | Jest + Test.createTestingModule() |
E2E Test | ์ค์ HTTP ์์ฒญ ๊ธฐ๋ฐ ์ ์ฒด ํ๋ฆ ํ ์คํธ | Jest + Supertest |
๐ง ํ ์คํธ ์คํ ๋ช ๋ น ์์ฝ
๋ช ๋ น์ด ์ค๋ช
npm run test | ์ ๋ ํ ์คํธ ์ํ |
npm run test:cov | ์ปค๋ฒ๋ฆฌ์ง ๋ฆฌํฌํธ ํฌํจ ํ ์คํธ ์คํ |
npm run test:e2e | E2E ํ ์คํธ ์ํ |
npm run test:watch | ํ์ผ ๋ณ๊ฒฝ ์ ์๋ ํ ์คํธ |
โ ๋ง๋ฌด๋ฆฌ ์์ฝ
๊ฐ๋ ์ค๋ช
Test.createTestingModule() | DI ํ๊ฒฝ์ ํ ์คํธ์์ ๊ตฌ์ฑํ๋ ํต์ฌ ๋๊ตฌ |
Mock Provider | ์๋น์ค ํ ์คํธ์์ ์์กด์ฑ ์ฃผ์ ๋์ฒด |
E2E ํ ์คํธ | ์ค์ ์์ฒญ-์๋ต ํ๋ฆ์ ๊ฒ์ฆํ์ฌ ์ ์ฒด ์์คํ ํ ์คํธ ์ํ |
์ปค๋ฒ๋ฆฌ์ง ์ฒดํฌ | --coverage ํ๋๊ทธ๋ก ์ปค๋ฒ๋ฆฌ์ง ๋ฆฌํฌํธ ์์ฑ ๊ฐ๋ฅ |
NestJS ํ ์คํธ,NestJS Unit Test,NestJS Integration Test,NestJS E2E Test,NestJS Jest ์ค์ ,NestJS Mocking,NestJS Test.createTestingModule,NestJS ํ ์คํธ ์ฝ๋,NestJS ํ ์คํธ ์ค๋ฌด,NestJS Coverage ๋ณด๊ณ ์
'framework > NestJS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- Total
- Today
- Yesterday
- fastapi
- App Router
- Docker
- CI/CD
- gatsbyjs
- PostgreSQL
- AI์ฑ๋ด
- ๋ฐฑ์๋๊ฐ๋ฐ
- Prisma
- SEO์ต์ ํ
- ๊ฐ๋ฐ๋ธ๋ก๊ทธ
- seo ์ต์ ํ 10๊ฐ
- nodejs
- SEO ์ต์ ํ
- ๋ฅ๋ฌ๋
- REACT
- flax
- ์น๊ฐ๋ฐ
- ํ๋ก ํธ์๋
- Python
- Next.js
- Ktor
- ํ์ด์ฌ์๊ณ ๋ฆฌ์ฆ
- kotlin
- ํ๋ก ํธ์๋๋ฉด์
- JAX
- NestJS
- nextJS
- llm
- rag
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |