forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting-module.ts
More file actions
75 lines (70 loc) · 2.86 KB
/
Copy pathtesting-module.ts
File metadata and controls
75 lines (70 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import * as optional from 'optional';
import { NestContainer } from '@nestjs/core/injector/container';
import { NestApplication, NestApplicationContext } from '@nestjs/core';
import { Type } from '@nestjs/common/interfaces/type.interface';
import { INestApplication, INestMicroservice } from '@nestjs/common';
import { MicroserviceOptions } from '@nestjs/common/interfaces/microservices/microservice-configuration.interface';
import { MicroservicesPackageNotFoundException } from '@nestjs/core/errors/exceptions/microservices-package-not-found.exception';
import { ApplicationConfig } from '@nestjs/core/application-config';
import { HttpServer } from '@nestjs/common';
import { ExpressFactory } from '@nestjs/core/adapters/express-factory';
import { ExpressAdapter } from '@nestjs/core/adapters/express-adapter';
import { FastifyAdapter } from '@nestjs/core/adapters/fastify-adapter';
import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface';
import { INestFastifyApplication } from '@nestjs/common/interfaces/nest-fastify-application.interface';
import { INestExpressApplication } from '@nestjs/common/interfaces/nest-express-application.interface';
const { NestMicroservice } =
optional('@nestjs/microservices/nest-microservice') || ({} as any);
export class TestingModule extends NestApplicationContext {
constructor(
container: NestContainer,
scope: Type<any>[],
contextModule,
private readonly applicationConfig: ApplicationConfig,
) {
super(container, scope, contextModule);
}
public createNestApplication(
httpServer?: FastifyAdapter,
options?: NestApplicationOptions,
): INestApplication & INestFastifyApplication;
public createNestApplication(
httpServer?: HttpServer,
options?: NestApplicationOptions,
): INestApplication & INestExpressApplication;
public createNestApplication(
httpServer?: any,
options?: NestApplicationOptions,
): INestApplication & INestExpressApplication;
public createNestApplication(
httpServer: any = ExpressFactory.create(),
options?: NestApplicationOptions,
): INestApplication & (INestExpressApplication | INestFastifyApplication) {
httpServer = this.applyExpressAdapter(httpServer);
this.container.setApplicationRef(httpServer);
return new NestApplication(
this.container,
httpServer,
this.applicationConfig,
);
}
public createNestMicroservice(
options: MicroserviceOptions,
): INestMicroservice {
if (!NestMicroservice) {
throw new MicroservicesPackageNotFoundException();
}
return new NestMicroservice(
this.container,
options,
this.applicationConfig,
);
}
private applyExpressAdapter(httpAdapter: HttpServer): HttpServer {
const isAdapter = httpAdapter.getHttpServer;
if (isAdapter) {
return httpAdapter;
}
return new ExpressAdapter(httpAdapter);
}
}