mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
test(): fix e2e tests
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { ExpressAdapter } from '@nestjs/platform-express';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { ApplicationModule } from '../src/app.module';
|
||||
|
||||
describe('Hello world (express instance)', () => {
|
||||
@@ -11,10 +12,9 @@ describe('Hello world (express instance)', () => {
|
||||
beforeEach(async () => {
|
||||
const module = await Test.createTestingModule({
|
||||
imports: [ApplicationModule],
|
||||
})
|
||||
.compile();
|
||||
}).compile();
|
||||
|
||||
app = module.createNestApplication(express());
|
||||
app = module.createNestApplication(new ExpressAdapter(express()));
|
||||
server = app.getHttpServer();
|
||||
await app.init();
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { INestFastifyApplication } from '@nestjs/common/interfaces/nest-fastify-application.interface';
|
||||
import { FastifyAdapter } from '@nestjs/core/adapters/fastify-adapter';
|
||||
import { FastifyAdapter } from '@nestjs/platform-fastify';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { expect } from 'chai';
|
||||
import { ApplicationModule } from '../src/app.module';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Get, BadRequestException, Controller } from '@nestjs/common';
|
||||
import { BadRequestException, Controller, Get } from '@nestjs/common';
|
||||
|
||||
@Controller()
|
||||
export class ErrorsController {
|
||||
@@ -13,6 +13,10 @@ export class ErrorsController {
|
||||
}
|
||||
|
||||
throwError() {
|
||||
throw new BadRequestException('Integration test');
|
||||
throw new BadRequestException({
|
||||
statusCode: 400,
|
||||
error: 'Bad Request',
|
||||
message: 'Integration test',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as request from 'supertest';
|
||||
import { MqttBroadcastController } from '../src/mqtt/mqtt-broadcast.controller';
|
||||
|
||||
describe('MQTT transport', () => {
|
||||
@@ -14,8 +13,9 @@ describe('MQTT transport', () => {
|
||||
controllers: [MqttBroadcastController],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
app.connectMicroservice({
|
||||
transport: Transport.MQTT,
|
||||
});
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as request from 'supertest';
|
||||
import { NatsBroadcastController } from '../src/nats/nats-broadcast.controller';
|
||||
|
||||
describe('NATS transport', () => {
|
||||
@@ -14,8 +13,9 @@ describe('NATS transport', () => {
|
||||
controllers: [NatsBroadcastController],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
app.connectMicroservice({
|
||||
transport: Transport.NATS,
|
||||
});
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { RedisController } from '../src/redis/redis.controller';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as request from 'supertest';
|
||||
import { RedisBroadcastController } from '../src/redis/redis-broadcast.controller';
|
||||
|
||||
describe('REDIS transport', () => {
|
||||
@@ -15,8 +13,9 @@ describe('REDIS transport', () => {
|
||||
controllers: [RedisBroadcastController],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
app.connectMicroservice({
|
||||
transport: Transport.REDIS,
|
||||
});
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { DisconnectedClientController } from '../src/disconnected.controller';
|
||||
|
||||
@@ -14,8 +13,9 @@ describe('Disconnected client', () => {
|
||||
controllers: [DisconnectedClientController],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
await app.init();
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as express from 'express';
|
||||
import { join } from 'path';
|
||||
import * as request from 'supertest';
|
||||
import { GrpcController } from '../src/grpc/grpc.controller';
|
||||
@@ -15,8 +14,9 @@ describe('GRPC transport', () => {
|
||||
controllers: [GrpcController],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
app.connectMicroservice({
|
||||
transport: Transport.GRPC,
|
||||
options: {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { MqttController } from '../src/mqtt/mqtt.controller';
|
||||
|
||||
@@ -14,8 +13,9 @@ describe('MQTT transport', () => {
|
||||
controllers: [MqttController],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
app.connectMicroservice({
|
||||
transport: Transport.MQTT,
|
||||
});
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { NatsController } from '../src/nats/nats.controller';
|
||||
|
||||
@@ -14,8 +13,9 @@ describe('NATS transport', () => {
|
||||
controllers: [NatsController],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
app.connectMicroservice({
|
||||
transport: Transport.NATS,
|
||||
options: {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { RedisController } from '../src/redis/redis.controller';
|
||||
|
||||
@@ -14,8 +13,9 @@ describe('REDIS transport', () => {
|
||||
controllers: [RedisController],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
app.connectMicroservice({
|
||||
transport: Transport.REDIS,
|
||||
});
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { RMQController } from '../src/rmq/rmq.controller';
|
||||
|
||||
@@ -14,8 +13,9 @@ describe('RabbitMQ transport', () => {
|
||||
controllers: [RMQController],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
app.connectMicroservice({
|
||||
transport: Transport.RMQ,
|
||||
options: {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Transport } from '@nestjs/microservices';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as express from 'express';
|
||||
import * as request from 'supertest';
|
||||
import { ApplicationModule } from '../src/app.module';
|
||||
|
||||
@@ -14,8 +13,9 @@ describe('RPC transport', () => {
|
||||
imports: [ApplicationModule],
|
||||
}).compile();
|
||||
|
||||
server = express();
|
||||
app = module.createNestApplication(server);
|
||||
app = module.createNestApplication();
|
||||
server = app.getHttpAdapter().getInstance();
|
||||
|
||||
app.connectMicroservice({
|
||||
transport: Transport.TCP,
|
||||
});
|
||||
|
||||
@@ -14,8 +14,6 @@ import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-applicati
|
||||
import { Logger } from '@nestjs/common/services/logger.service';
|
||||
import { loadPackage } from '@nestjs/common/utils/load-package.util';
|
||||
import { isObject, validatePath } from '@nestjs/common/utils/shared.utils';
|
||||
import { Server } from 'http';
|
||||
import { Server as HttpsServer } from 'https';
|
||||
import iterate from 'iterare';
|
||||
import * as optional from 'optional';
|
||||
import { ApplicationConfig } from './application-config';
|
||||
@@ -45,7 +43,7 @@ export class NestApplication extends NestApplicationContext
|
||||
private readonly socketModule = SocketModule ? new SocketModule() : null;
|
||||
private readonly routesResolver: Resolver;
|
||||
private readonly microservices: any[] = [];
|
||||
private httpServer: Server | HttpsServer;
|
||||
private httpServer: any;
|
||||
private isInitialized = false;
|
||||
|
||||
constructor(
|
||||
|
||||
@@ -14,6 +14,10 @@ import { ServeStaticOptions } from './../interfaces/serve-static-options.interfa
|
||||
export class ExpressAdapter extends AbstractHttpAdapter {
|
||||
private readonly routerMethodFactory = new RouterMethodFactory();
|
||||
|
||||
constructor(instance?: any) {
|
||||
super(instance || express());
|
||||
}
|
||||
|
||||
public reply(response, body: any, statusCode: number) {
|
||||
const res = response.status(statusCode);
|
||||
if (isNil(body)) {
|
||||
|
||||
@@ -10,8 +10,18 @@ import * as formBody from 'fastify-formbody';
|
||||
import * as pathToRegexp from 'path-to-regexp';
|
||||
|
||||
export class FastifyAdapter extends AbstractHttpAdapter {
|
||||
constructor(options?: fastify.ServerOptions) {
|
||||
super(fastify(options));
|
||||
constructor(
|
||||
instanceOrOptions:
|
||||
| fastify.FastifyInstance<any, any, any>
|
||||
| fastify.ServerOptions = fastify(),
|
||||
) {
|
||||
const instance =
|
||||
instanceOrOptions &&
|
||||
(instanceOrOptions as fastify.FastifyInstance<any, any, any>).server
|
||||
? instanceOrOptions
|
||||
: fastify(instanceOrOptions as fastify.ServerOptions);
|
||||
|
||||
super(instance);
|
||||
}
|
||||
|
||||
public use(handler: RequestHandler | ErrorHandler);
|
||||
|
||||
Reference in New Issue
Block a user