Nestia를 이용해 스웨거와 e2e테스팅 세팅을 진행합니다.
Nestia는 타입을 읽어 스웨거 파일을 자동으로 생성해주는 강력한 기능을 제공합니다.
먼저 스웨거 모듈을 설치합니다.
npm install @nestjs/swagger --save-dev
아래 파일을 루트 디렉토리에 추가합니다. (sdk 설정 포함)
import { INestiaConfig } from '@nestia/sdk';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './src/app.module';
export const NESTIA_CONFIG: INestiaConfig = {
input: () => NestFactory.create(AppModule),
output: 'src/api', // typescript 파일로서 SDK를 생성
swagger: {
output: './swagger.json', // 스웨거 json 파일 생성 위치
servers: [
{
url: '<http://localhost:3000>', // 스웨거에서 요청 보낼 URL 경로
description: 'Local Server',
},
],
beautify: true,
},
primitive: false,
simulate: true,
};
export default NESTIA_CONFIG;
main.ts 스웨거 문서 로딩 세팅async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors();
// Swgger 세팅
const swaggerFilePath = path.join(__dirname, '../swagger.json'); // 스웨거 json 파일의 경로를 지정한다.
if (!existsSync(swaggerFilePath)) {
writeFileSync(swaggerFilePath, '{}');
}
const swaagerConfig = readFileSync(swaggerFilePath, 'utf8');
const swaggerDocument = JSON.parse(swaagerConfig);
SwaggerModule.setup('api/swagger', app, swaggerDocument); // '/api/swagger'로 문서 경로 설정
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();