import { INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import request from 'supertest'; import { App } from 'supertest/types'; import { AppModule } from '../src/app.module'; import { configureApp } from '../src/common/configure-app'; describe('Swagger (e2e)', () => { describe('when enabled', () => { let app: INestApplication; beforeAll(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule], }).compile(); app = moduleFixture.createNestApplication(); configureApp(app, { NODE_ENV: 'test', SWAGGER_ENABLED: 'true', }); await app.init(); }); afterAll(async () => { await app.close(); }); it('GET /docs returns HTML UI', async () => { const res = await request(app.getHttpServer()).get('/docs').expect(200); expect(res.text).toContain('Swagger UI'); }); it('GET /docs-json includes auth paths', async () => { const res = await request(app.getHttpServer()) .get('/docs-json') .expect(200); const body = res.body as { paths: Record; components: { securitySchemes: Record }; }; expect(body.paths['/auth/login']).toBeDefined(); expect(body.paths['/auth/me']).toBeDefined(); expect(body.paths['/auth/register']).toBeDefined(); expect(body.components.securitySchemes['access-token']).toBeDefined(); }); }); describe('when production and not explicitly enabled', () => { let app: INestApplication; beforeAll(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule], }).compile(); app = moduleFixture.createNestApplication(); configureApp(app, { NODE_ENV: 'production', }); await app.init(); }); afterAll(async () => { await app.close(); }); it('GET /docs returns 404', () => { return request(app.getHttpServer()).get('/docs').expect(404); }); it('GET /docs-json returns 404', () => { return request(app.getHttpServer()).get('/docs-json').expect(404); }); }); });