mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 15:08:37 +00:00
61 lines
1.1 KiB
TypeScript
61 lines
1.1 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
INestApplication,
|
|
Injectable,
|
|
MiddlewareConsumer,
|
|
Module,
|
|
} from '@nestjs/common';
|
|
import { Test } from '@nestjs/testing';
|
|
import * as request from 'supertest';
|
|
import { ApplicationModule } from '../src/app.module';
|
|
|
|
const RETURN_VALUE = 'test';
|
|
|
|
@Injectable()
|
|
class Middleware {
|
|
use(req, res, next) {
|
|
res.send(RETURN_VALUE);
|
|
}
|
|
}
|
|
|
|
@Controller()
|
|
class TestController {
|
|
@Get('test')
|
|
test() {
|
|
return 'test';
|
|
}
|
|
}
|
|
|
|
@Module({
|
|
imports: [ApplicationModule],
|
|
controllers: [TestController],
|
|
})
|
|
class TestModule {
|
|
configure(consumer: MiddlewareConsumer) {
|
|
consumer.apply(Middleware).forRoutes('*');
|
|
}
|
|
}
|
|
|
|
describe('Middleware (class)', () => {
|
|
let app: INestApplication;
|
|
|
|
beforeEach(async () => {
|
|
app = (
|
|
await Test.createTestingModule({
|
|
imports: [TestModule],
|
|
}).compile()
|
|
).createNestApplication();
|
|
|
|
await app.init();
|
|
});
|
|
|
|
it(`forRoutes(*)`, () => {
|
|
return request(app.getHttpServer()).get('/hello').expect(200, RETURN_VALUE);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await app.close();
|
|
});
|
|
});
|