1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TestingModule = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
|
6 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
7 | const core_1 = require("@nestjs/core");
|
8 | class TestingModule extends core_1.NestApplicationContext {
|
9 | constructor(container, scope, contextModule, applicationConfig) {
|
10 | super(container, scope, contextModule);
|
11 | this.applicationConfig = applicationConfig;
|
12 | }
|
13 | createNestApplication(httpAdapter, options) {
|
14 | httpAdapter = httpAdapter || this.createHttpAdapter();
|
15 | this.applyLogger(options);
|
16 | this.container.setHttpAdapter(httpAdapter);
|
17 | const instance = new core_1.NestApplication(this.container, httpAdapter, this.applicationConfig, options);
|
18 | return this.createAdapterProxy(instance, httpAdapter);
|
19 | }
|
20 | createNestMicroservice(options) {
|
21 | const { NestMicroservice } = (0, load_package_util_1.loadPackage)('@nestjs/microservices', 'TestingModule', () => require('@nestjs/microservices'));
|
22 | this.applyLogger(options);
|
23 | return new NestMicroservice(this.container, options, this.applicationConfig);
|
24 | }
|
25 | createHttpAdapter(httpServer) {
|
26 | const { ExpressAdapter } = (0, load_package_util_1.loadPackage)('@nestjs/platform-express', 'NestFactory', () => require('@nestjs/platform-express'));
|
27 | return new ExpressAdapter(httpServer);
|
28 | }
|
29 | applyLogger(options) {
|
30 | if (!options || (0, shared_utils_1.isUndefined)(options.logger)) {
|
31 | return;
|
32 | }
|
33 | common_1.Logger.overrideLogger(options.logger);
|
34 | }
|
35 | createAdapterProxy(app, adapter) {
|
36 | return new Proxy(app, {
|
37 | get: (receiver, prop) => {
|
38 | if (!(prop in receiver) && prop in adapter) {
|
39 | return adapter[prop];
|
40 | }
|
41 | return receiver[prop];
|
42 | },
|
43 | });
|
44 | }
|
45 | }
|
46 | exports.TestingModule = TestingModule;
|