Skip to content

Commit

Permalink
Merge branch 'micalevisk-feat/testing-module-options' into 9.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 24, 2022
2 parents 69377a6 + 21a0aee commit 55b95c7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 59 deletions.
12 changes: 6 additions & 6 deletions integration/cors/e2e/express.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NestFastifyApplication } from '@nestjs/platform-fastify';
import { NestExpressApplication } from '@nestjs/platform-express';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';

describe('Express Cors', () => {
let app: NestFastifyApplication;
let app: NestExpressApplication;
const configs = [
{
origin: 'example.com',
Expand All @@ -30,7 +30,7 @@ describe('Express Cors', () => {
imports: [AppModule],
}).compile();

app = module.createNestApplication<NestFastifyApplication>();
app = module.createNestApplication<NestExpressApplication>();

let requestId = 0;
const configDelegation = function (req, cb) {
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('Express Cors', () => {
cb(null, config);
};

app = module.createNestApplication<NestFastifyApplication>(null, {
app = module.createNestApplication<NestExpressApplication>({
cors: configDelegation,
});

Expand Down Expand Up @@ -126,7 +126,7 @@ describe('Express Cors', () => {
imports: [AppModule],
}).compile();

app = module.createNestApplication<NestFastifyApplication>();
app = module.createNestApplication<NestExpressApplication>();
app.enableCors(configs[0]);

await app.init();
Expand All @@ -153,7 +153,7 @@ describe('Express Cors', () => {
imports: [AppModule],
}).compile();

app = module.createNestApplication<NestFastifyApplication>(null, {
app = module.createNestApplication<NestExpressApplication>({
cors: configs[0],
});
await app.init();
Expand Down
33 changes: 23 additions & 10 deletions integration/cors/e2e/fastify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { NestFastifyApplication } from '@nestjs/platform-fastify';
import {
NestFastifyApplication,
FastifyAdapter,
} from '@nestjs/platform-fastify';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
Expand Down Expand Up @@ -30,7 +33,9 @@ describe('Fastify Cors', () => {
imports: [AppModule],
}).compile();

app = module.createNestApplication<NestFastifyApplication>();
app = module.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
);

let requestId = 0;
const configDelegation = function (req, cb) {
Expand Down Expand Up @@ -84,9 +89,12 @@ describe('Fastify Cors', () => {
cb(null, config);
};

app = module.createNestApplication<NestFastifyApplication>(null, {
cors: configDelegation,
});
app = module.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
{
cors: configDelegation,
},
);

await app.init();
});
Expand Down Expand Up @@ -127,7 +135,9 @@ describe('Fastify Cors', () => {
imports: [AppModule],
}).compile();

app = module.createNestApplication<NestFastifyApplication>();
app = module.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
);
app.enableCors(configs[0]);

await app.init();
Expand All @@ -147,16 +157,19 @@ describe('Fastify Cors', () => {
after(async () => {
await app.close();
});

describe('Application Options', () => {
before(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = module.createNestApplication<NestFastifyApplication>(null, {
cors: configs[0],
});
app = module.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
{
cors: configs[0],
},
);
await app.init();
});

Expand Down
7 changes: 3 additions & 4 deletions integration/nest-application/raw-body/e2e/express.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ describe('Raw body (Express Application)', () => {
imports: [ExpressModule],
}).compile();

app = moduleFixture.createNestApplication<NestExpressApplication>(
undefined,
{ rawBody: true },
);
app = moduleFixture.createNestApplication<NestExpressApplication>({
rawBody: true,
});
});

it('should return exact post body', async () => {
Expand Down
40 changes: 6 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@codechecks/client": "0.1.12",
"@commitlint/cli": "17.0.2",
"@commitlint/config-angular": "17.0.0",
"@fastify/cors": "7.0.0",
"@fastify/cors": "^8.0.0",
"@fastify/formbody": "7.0.1",
"@fastify/middie": "8.0.0",
"@fastify/multipart": "6.0.0",
Expand Down
31 changes: 27 additions & 4 deletions packages/testing/testing-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,43 @@ export class TestingModule extends NestApplicationContext {
super(container, scope, contextModule);
}

private isHttpServer(
serverOrOptions:
| HttpServer
| AbstractHttpAdapter
| NestApplicationOptions
| undefined,
): serverOrOptions is HttpServer | AbstractHttpAdapter {
return !!(serverOrOptions && (serverOrOptions as HttpServer).patch);
}

public createNestApplication<T extends INestApplication = INestApplication>(
httpAdapter: HttpServer | AbstractHttpAdapter,
options?: NestApplicationOptions,
): T;
public createNestApplication<T extends INestApplication = INestApplication>(
httpAdapter?: HttpServer | AbstractHttpAdapter,
options?: NestApplicationOptions,
): T;
public createNestApplication<T extends INestApplication = INestApplication>(
serverOrOptions:
| HttpServer
| AbstractHttpAdapter
| NestApplicationOptions
| undefined,
options?: NestApplicationOptions,
): T {
httpAdapter = httpAdapter || this.createHttpAdapter();
const [httpAdapter, appOptions] = this.isHttpServer(serverOrOptions)
? [serverOrOptions, options]
: [this.createHttpAdapter(), serverOrOptions];

this.applyLogger(options);
this.applyLogger(appOptions);
this.container.setHttpAdapter(httpAdapter);

const instance = new NestApplication(
this.container,
httpAdapter,
this.applicationConfig,
options,
appOptions,
);
return this.createAdapterProxy<T>(instance, httpAdapter);
}
Expand Down

0 comments on commit 55b95c7

Please sign in to comment.