mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
88 lines
2.1 KiB
TypeScript
88 lines
2.1 KiB
TypeScript
import * as request from 'supertest';
|
|
import { Test } from '@nestjs/testing';
|
|
import { INestApplication, Injectable } from '@nestjs/common';
|
|
import { ApplicationModule } from '../src/app.module';
|
|
import { APP_INTERCEPTOR } from '@nestjs/core';
|
|
import { of } from 'rxjs';
|
|
import { map } from 'rxjs/operators';
|
|
|
|
const RETURN_VALUE = 'test';
|
|
|
|
@Injectable()
|
|
export class OverrideInterceptor {
|
|
intercept(context, stream) {
|
|
return of(RETURN_VALUE);
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class TransformInterceptor {
|
|
intercept(context, stream) {
|
|
return stream.pipe(map(data => ({ data })));
|
|
}
|
|
}
|
|
|
|
function createTestModule(interceptor) {
|
|
return Test.createTestingModule({
|
|
imports: [ApplicationModule],
|
|
providers: [
|
|
{
|
|
provide: APP_INTERCEPTOR,
|
|
useValue: interceptor,
|
|
},
|
|
],
|
|
}).compile();
|
|
}
|
|
|
|
describe('Interceptors', () => {
|
|
let app: INestApplication;
|
|
|
|
it(`should transform response (sync)`, async () => {
|
|
app = (await createTestModule(
|
|
new OverrideInterceptor(),
|
|
)).createNestApplication();
|
|
|
|
await app.init();
|
|
return request(app.getHttpServer())
|
|
.get('/hello')
|
|
.expect(200, RETURN_VALUE);
|
|
});
|
|
|
|
it(`should map response`, async () => {
|
|
app = (await createTestModule(
|
|
new TransformInterceptor(),
|
|
)).createNestApplication();
|
|
|
|
await app.init();
|
|
return request(app.getHttpServer())
|
|
.get('/hello')
|
|
.expect(200, { data: 'Hello world!' });
|
|
});
|
|
|
|
it(`should map response (async)`, async () => {
|
|
app = (await createTestModule(
|
|
new TransformInterceptor(),
|
|
)).createNestApplication();
|
|
|
|
await app.init();
|
|
return request(app.getHttpServer())
|
|
.get('/hello/stream')
|
|
.expect(200, { data: 'Hello world!' });
|
|
});
|
|
|
|
it(`should map response (stream)`, async () => {
|
|
app = (await createTestModule(
|
|
new TransformInterceptor(),
|
|
)).createNestApplication();
|
|
|
|
await app.init();
|
|
return request(app.getHttpServer())
|
|
.get('/hello/async')
|
|
.expect(200, { data: 'Hello world!' });
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await app.close();
|
|
});
|
|
});
|