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
88 lines (80 loc) · 2.85 KB
/
Copy pathtesting-module.ts
File metadata and controls
88 lines (80 loc) · 2.85 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
76
77
78
79
80
81
82
83
84
85
86
87
88
import {
HttpServer,
INestApplication,
INestMicroservice,
Logger,
} from '@nestjs/common';
import { MicroserviceOptions } from '@nestjs/common/interfaces/microservices/microservice-configuration.interface';
import { NestMicroserviceOptions } from '@nestjs/common/interfaces/microservices/nest-microservice-options.interface';
import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface';
import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface';
import { Type } from '@nestjs/common/interfaces/type.interface';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { NestApplication, NestApplicationContext } from '@nestjs/core';
import { ApplicationConfig } from '@nestjs/core/application-config';
import { NestContainer } from '@nestjs/core/injector/container';
import { Module } from '@nestjs/core/injector/module';
export class TestingModule extends NestApplicationContext {
constructor(
container: NestContainer,
scope: Type<any>[],
contextModule: Module,
private readonly applicationConfig: ApplicationConfig,
) {
super(container, scope, contextModule);
}
public createNestApplication<T extends INestApplication = INestApplication>(
httpAdapter?: HttpServer,
options?: NestApplicationOptions,
): T {
httpAdapter = httpAdapter || this.createHttpAdapter();
this.applyLogger(options);
this.container.setHttpAdapter(httpAdapter);
const instance = new NestApplication(
this.container,
httpAdapter,
this.applicationConfig,
options,
);
return this.createAdapterProxy<T>(instance, httpAdapter);
}
public createNestMicroservice(
options: NestMicroserviceOptions & MicroserviceOptions,
): INestMicroservice {
const { NestMicroservice } = loadPackage(
'@nestjs/microservices',
'TestingModule',
() => require('@nestjs/microservices'),
);
this.applyLogger(options);
return new NestMicroservice(
this.container,
options,
this.applicationConfig,
);
}
private createHttpAdapter<T = any>(httpServer?: T): HttpServer {
const { ExpressAdapter } = loadPackage(
'@nestjs/platform-express',
'NestFactory',
() => require('@nestjs/platform-express'),
);
return new ExpressAdapter(httpServer);
}
private applyLogger(options: NestApplicationContextOptions | undefined) {
if (!options || !options.logger) {
return;
}
Logger.overrideLogger(options.logger);
}
private createAdapterProxy<T>(app: NestApplication, adapter: HttpServer): T {
return (new Proxy(app, {
get: (receiver: Record<string, any>, prop: string) => {
if (!(prop in receiver) && prop in adapter) {
return adapter[prop];
}
return receiver[prop];
},
}) as any) as T;
}
}