Skip to content

Commit

Permalink
test(): fix e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Dec 5, 2018
1 parent a3fbb88 commit 42e1e79
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 50 deletions.
10 changes: 5 additions & 5 deletions integration/hello-world/e2e/express-instance.spec.ts
@@ -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)', () => {
Expand All @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion integration/hello-world/e2e/fastify-adapter.spec.ts
@@ -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';
Expand Down
8 changes: 6 additions & 2 deletions integration/hello-world/src/errors/errors.controller.ts
@@ -1,4 +1,4 @@
import { Get, BadRequestException, Controller } from '@nestjs/common';
import { BadRequestException, Controller, Get } from '@nestjs/common';

@Controller()
export class ErrorsController {
Expand All @@ -13,6 +13,10 @@ export class ErrorsController {
}

throwError() {
throw new BadRequestException('Integration test');
throw new BadRequestException({
statusCode: 400,
error: 'Bad Request',
message: 'Integration test',
});
}
}
10 changes: 5 additions & 5 deletions integration/microservices/e2e/broadcast-mqtt.spec.ts
@@ -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', () => {
Expand All @@ -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,
});
Expand Down
10 changes: 5 additions & 5 deletions integration/microservices/e2e/broadcast-nats.spec.ts
@@ -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', () => {
Expand All @@ -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,
});
Expand Down
11 changes: 5 additions & 6 deletions integration/microservices/e2e/broadcast-redis.spec.ts
@@ -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', () => {
Expand All @@ -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,
});
Expand Down
6 changes: 3 additions & 3 deletions integration/microservices/e2e/disconnected-client.spec.ts
@@ -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';

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

Expand Down
6 changes: 3 additions & 3 deletions integration/microservices/e2e/sum-grpc.spec.ts
@@ -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';
Expand All @@ -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: {
Expand Down
6 changes: 3 additions & 3 deletions integration/microservices/e2e/sum-mqtt.spec.ts
@@ -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';

Expand All @@ -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,
});
Expand Down
6 changes: 3 additions & 3 deletions integration/microservices/e2e/sum-nats.spec.ts
@@ -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';

Expand All @@ -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: {
Expand Down
6 changes: 3 additions & 3 deletions integration/microservices/e2e/sum-redis.spec.ts
@@ -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';

Expand All @@ -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,
});
Expand Down
6 changes: 3 additions & 3 deletions integration/microservices/e2e/sum-rmq.spec.ts
@@ -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';

Expand All @@ -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: {
Expand Down
6 changes: 3 additions & 3 deletions integration/microservices/e2e/sum-rpc.spec.ts
@@ -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';

Expand All @@ -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,
});
Expand Down
4 changes: 1 addition & 3 deletions packages/core/nest-application.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions packages/platform-express/adapters/express-adapter.ts
Expand Up @@ -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)) {
Expand Down
14 changes: 12 additions & 2 deletions packages/platform-fastify/adapters/fastify-adapter.ts
Expand Up @@ -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);
Expand Down

0 comments on commit 42e1e79

Please sign in to comment.