test(): fix e2e tests

This commit is contained in:
Kamil Myśliwiec
2018-12-05 23:08:03 +01:00
parent a3fbb8816e
commit 42e1e79b31
16 changed files with 65 additions and 50 deletions

View File

@@ -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();
});

View File

@@ -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';

View File

@@ -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',
});
}
}

View File

@@ -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,
});

View File

@@ -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,
});

View File

@@ -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,
});

View File

@@ -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();
});

View File

@@ -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: {

View File

@@ -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,
});

View File

@@ -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: {

View File

@@ -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,
});

View File

@@ -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: {

View File

@@ -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,
});

View File

@@ -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(

View File

@@ -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)) {

View File

@@ -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);