|
| 1 | +import { INestApplication, VersioningType } from '@nestjs/common'; |
| 2 | +import { |
| 3 | + FastifyAdapter, |
| 4 | + NestFastifyApplication, |
| 5 | +} from '@nestjs/platform-fastify'; |
| 6 | +import { Test } from '@nestjs/testing'; |
| 7 | +import * as request from 'supertest'; |
| 8 | +import { AppModule } from '../src/app.module'; |
| 9 | + |
| 10 | +/** |
| 11 | + * `.enableVersioning()` uses `VersioningType.URI` type by default |
| 12 | + * Regression test for #13496 |
| 13 | + * @see [Versioning](https://docs.nestjs.com/techniques/versioning) |
| 14 | + */ |
| 15 | +describe('Default Versioning behavior', () => { |
| 16 | + // ======================================================================== // |
| 17 | + describe('Express', () => { |
| 18 | + let app: INestApplication; |
| 19 | + before(async () => { |
| 20 | + const moduleRef = await Test.createTestingModule({ |
| 21 | + imports: [AppModule], |
| 22 | + }).compile(); |
| 23 | + |
| 24 | + app = moduleRef.createNestApplication(); |
| 25 | + app.enableVersioning(); |
| 26 | + await app.init(); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('GET /', () => { |
| 30 | + it('V1', () => { |
| 31 | + return request(app.getHttpServer()) |
| 32 | + .get('/v1') |
| 33 | + .expect(200) |
| 34 | + .expect('Hello World V1!'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('No Version', () => { |
| 38 | + return request(app.getHttpServer()).get('/').expect(404); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('GET /neutral', () => { |
| 43 | + it('No Version', () => { |
| 44 | + return request(app.getHttpServer()) |
| 45 | + .get('/neutral') |
| 46 | + .expect(200) |
| 47 | + .expect('Neutral'); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + after(async () => { |
| 52 | + await app.close(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + // ======================================================================== // |
| 57 | + describe('Fastify', () => { |
| 58 | + let app: INestApplication; |
| 59 | + before(async () => { |
| 60 | + const moduleRef = await Test.createTestingModule({ |
| 61 | + imports: [AppModule], |
| 62 | + }).compile(); |
| 63 | + |
| 64 | + app = moduleRef.createNestApplication<NestFastifyApplication>( |
| 65 | + new FastifyAdapter(), |
| 66 | + ); |
| 67 | + app.enableVersioning(); |
| 68 | + await app.init(); |
| 69 | + await app.getHttpAdapter().getInstance().ready(); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('GET /', () => { |
| 73 | + it('V1', () => { |
| 74 | + return request(app.getHttpServer()) |
| 75 | + .get('/v1') |
| 76 | + .expect(200) |
| 77 | + .expect('Hello World V1!'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('No Version', () => { |
| 81 | + return request(app.getHttpServer()).get('/').expect(404); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('GET /neutral', () => { |
| 86 | + it('No Version', () => { |
| 87 | + return request(app.getHttpServer()) |
| 88 | + .get('/neutral') |
| 89 | + .expect(200) |
| 90 | + .expect('Neutral'); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + after(async () => { |
| 95 | + await app.close(); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments