Files
nest/integration/hello-world/e2e/middleware-with-versioning.spec.ts
Thiago Martins 415858f635 test(integration): wrap test case for uri version
wrap test case for uri versioning context to facilitate the addition of tests for other versioning types
2022-10-31 17:18:30 -03:00

82 lines
1.7 KiB
TypeScript

import {
Controller,
Get,
INestApplication,
MiddlewareConsumer,
Module,
Version,
RequestMethod,
VersioningType,
VERSION_NEUTRAL,
VersioningOptions,
} from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
const RETURN_VALUE = 'test';
const VERSIONED_VALUE = 'test_versioned';
@Controller()
class TestController {
@Version('1')
@Get('versioned')
versionedTest() {
return RETURN_VALUE;
}
}
@Module({
imports: [AppModule],
controllers: [TestController],
})
class TestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => res.send(VERSIONED_VALUE))
.forRoutes({
path: '/versioned',
version: '1',
method: RequestMethod.ALL,
});
}
}
describe('Middleware', () => {
let app: INestApplication;
describe('when using URI versioning', () => {
beforeEach(async () => {
app = await createAppWithVersioningType(VersioningType.URI);
});
it(`forRoutes({ path: 'tests/versioned', version: '1', method: RequestMethod.ALL })`, () => {
return request(app.getHttpServer())
.get('/v1/versioned')
.expect(200, VERSIONED_VALUE);
});
});
afterEach(async () => {
await app.close();
});
});
async function createAppWithVersioningType(
versioningType: VersioningType,
): Promise<INestApplication> {
const app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication();
app.enableVersioning({
type: versioningType,
defaultVersion: VERSION_NEUTRAL,
} as VersioningOptions);
await app.init();
return app;
}