@nest-yalc-2/app 2.2.1 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/package.json +10 -2
  2. package/src/__tests__/app-bootstrap-base.helper.spec.ts +86 -38
  3. package/src/__tests__/app-bootstrap-standalone.helper.spec.ts +35 -35
  4. package/src/__tests__/app-bootstrap.helper.spec.ts +162 -98
  5. package/src/__tests__/app-config.service.spec.ts +55 -55
  6. package/src/__tests__/app-context.service.spec.ts +28 -28
  7. package/src/__tests__/app.events.spec.ts +12 -12
  8. package/src/__tests__/app.helper.spec.ts +71 -204
  9. package/src/__tests__/base-app-module.helper.spec.ts +295 -231
  10. package/src/__tests__/base-app.controller.spec.ts +90 -90
  11. package/src/__tests__/base-app.service.spec.ts +39 -39
  12. package/src/__tests__/cls.module.spec.ts +33 -33
  13. package/src/__tests__/life-cycle-handler.service.spec.ts +57 -48
  14. package/src/__tests__/unwrap-result.interceptor.spec.ts +52 -52
  15. package/src/app-bootstrap-base.helper.ts +206 -206
  16. package/src/app-bootstrap-standalone.helper.ts +67 -67
  17. package/src/app-bootstrap.helper.js +9 -9
  18. package/src/app-bootstrap.helper.ts +279 -279
  19. package/src/app-config.service.ts +61 -61
  20. package/src/app-context.module.ts +6 -6
  21. package/src/app-context.service.ts +16 -16
  22. package/src/app-imports.factory.js +2 -2
  23. package/src/app-imports.factory.js.map +1 -1
  24. package/src/app-imports.factory.ts +321 -321
  25. package/src/app.events.ts +8 -8
  26. package/src/app.helper.ts +73 -73
  27. package/src/base-app-module.helper.d.ts +2 -2
  28. package/src/base-app-module.helper.js +8 -5
  29. package/src/base-app-module.helper.js.map +1 -1
  30. package/src/base-app-module.helper.ts +457 -453
  31. package/src/base-app.controller.ts +51 -51
  32. package/src/base-app.interface.ts +55 -55
  33. package/src/base-app.service.ts +33 -33
  34. package/src/cls.module.d.ts +2 -1
  35. package/src/cls.module.js.map +1 -1
  36. package/src/cls.module.ts +76 -75
  37. package/src/conf.type.ts +22 -22
  38. package/src/def.const.ts +33 -33
  39. package/src/global.enum.ts +6 -6
  40. package/src/life-cycle-handler.service.ts +66 -66
  41. package/src/unwrap-result.interceptor.ts +32 -32
  42. package/src/yalc-controller.interface.ts +6 -6
@@ -1,231 +1,295 @@
1
- import { describe, expect, it } from '@jest/globals';
2
- import { Test } from '@nestjs/testing';
3
- import {
4
- YalcBaseAppModule,
5
- registerSingletonDynamicModule,
6
- envFilePathList,
7
- yalcBaseAppModuleMetadataFactory,
8
- YalcDefaultAppModule,
9
- } from '../base-app-module.helper.js';
10
- import { LifeCycleHandler } from '../life-cycle-handler.service.js';
11
- import { DynamicModule, Module } from '@nestjs/common';
12
- import { IYalcBaseAppOptions } from '../base-app.interface.js';
13
- import { createMock } from '@golevelup/ts-jest';
14
- import { AppContextService } from '../app-context.service.js';
15
-
16
- let appContextService: AppContextService = createMock<AppContextService>({
17
- initializedApps: new Set(),
18
- });
19
-
20
- class DummyDynamicModule extends YalcBaseAppModule {
21
- static forRoot(options?: IYalcBaseAppOptions): DynamicModule {
22
- return this.assignDynamicProperties(
23
- yalcBaseAppModuleMetadataFactory(this, 'appAlias', {
24
- ...this.assignDynamicProperties({}),
25
- configFactory: () => ({}),
26
- logger: true,
27
- isSingleton: true,
28
- imports: [YalcDefaultAppModule.forRoot('appAlias', [])],
29
- ...options,
30
- }),
31
- options,
32
- );
33
- }
34
- }
35
-
36
- @Module({
37
- imports: [
38
- DummyDynamicModule.forRoot({
39
- providers: [
40
- {
41
- provide: 'test1',
42
- useValue: 'test',
43
- },
44
- ],
45
- }),
46
- ],
47
- })
48
- class DummyStaticModule1 {}
49
-
50
- @Module({
51
- imports: [
52
- DummyDynamicModule.forRoot({
53
- providers: [
54
- {
55
- provide: 'test2',
56
- useValue: 'test',
57
- },
58
- ],
59
- }),
60
- ],
61
- })
62
- class DummyStaticModule2 {}
63
-
64
- describe('base-app', () => {
65
- describe('actual Nest module instantiation', () => {
66
- it('should create dynamic module', async () => {
67
- const module = await Test.createTestingModule({
68
- imports: [DummyStaticModule2, DummyStaticModule1],
69
- })
70
- .overrideProvider(AppContextService)
71
- .useValue(appContextService);
72
-
73
- const moduleRef = await module.compile();
74
-
75
- const lifeCycleHandler = moduleRef.get(LifeCycleHandler);
76
-
77
- expect(module).toBeDefined();
78
- expect(lifeCycleHandler).toBeDefined();
79
- });
80
- });
81
-
82
- describe('YalcBaseAppModule', () => {
83
- it('should be defined', () => {
84
- expect(YalcBaseAppModule).toBeDefined();
85
- });
86
-
87
- it('should create dynamic properties', () => {
88
- const dynamicProperties = YalcBaseAppModule.assignDynamicProperties({});
89
- expect(dynamicProperties).toEqual({
90
- module: YalcBaseAppModule,
91
- global: true,
92
- isSingleton: false,
93
- });
94
- });
95
-
96
- it('should create dynamic properties with options', () => {
97
- const dynamicProperties = YalcBaseAppModule.assignDynamicProperties(
98
- {},
99
- {
100
- global: false,
101
- isSingleton: true,
102
- },
103
- );
104
- expect(dynamicProperties).toEqual({
105
- module: YalcBaseAppModule,
106
- global: false,
107
- isSingleton: true,
108
- });
109
- });
110
-
111
- it('should create dynamic module for standalone', () => {
112
- const dynamicModule = YalcBaseAppModule._forRootStandalone('appAlias');
113
- expect(dynamicModule).toBeDefined();
114
- expect(dynamicModule.global).toBe(true);
115
- expect(dynamicModule.isSingleton).toBe(false);
116
- });
117
-
118
- it('should create dynamic module', () => {
119
- const dynamicModule = YalcBaseAppModule._forRoot('appAlias');
120
- expect(dynamicModule).toBeDefined();
121
- expect(dynamicModule.global).toBe(true);
122
- expect(dynamicModule.isSingleton).toBe(false);
123
- });
124
- });
125
-
126
- describe('registerSingletonDynamicModule', () => {
127
- class TestModule extends DummyDynamicModule {
128
- static forRoot(options?: IYalcBaseAppOptions | undefined): DynamicModule {
129
- return this._forRoot('test', options);
130
- }
131
- }
132
-
133
- it('should register singleton dynamic module', () => {
134
- const config = TestModule.forRoot();
135
- const result = registerSingletonDynamicModule(true, TestModule, config);
136
- expect(result).toBe(config);
137
- });
138
-
139
- it('should not register the same singleton dynamic module twice', () => {
140
- const result = registerSingletonDynamicModule(
141
- true,
142
- TestModule,
143
- TestModule.forRoot(),
144
- );
145
- expect(result).not.toBe(true);
146
- });
147
- });
148
-
149
- describe('envFilePathList', () => {
150
- it('should create env file path list', () => {
151
- const paths = envFilePathList('.');
152
- expect(paths).toContain('./.env');
153
- if (process.env.NODE_ENV) {
154
- expect(paths).toContain(`./.env.${process.env.NODE_ENV}`);
155
- }
156
- if (process.env.NODE_ENV !== 'production') {
157
- expect(paths).toContain('./.env.dist');
158
- }
159
- });
160
-
161
- it('should create env file path list for production', () => {
162
- process.env.NODE_ENV = 'production';
163
- const paths = envFilePathList('.');
164
- expect(paths).toContain('./.env');
165
- expect(paths).toContain(`./.env.production`);
166
- expect(paths).not.toContain('./.env.dist');
167
- process.env.NODE_ENV = 'test';
168
- });
169
- });
170
-
171
- describe('baseAppModuleMetadataFactory', () => {
172
- it('should create base app module metadata', () => {
173
- const metadata = yalcBaseAppModuleMetadataFactory({}, 'appAlias', {
174
- logger: true,
175
- global: true,
176
- });
177
- expect(metadata).toBeDefined();
178
- expect(metadata.controllers).toEqual([]);
179
- });
180
-
181
- it('should create base app module metadata with options', () => {
182
- const metadata = yalcBaseAppModuleMetadataFactory({}, 'appAlias', {
183
- envPath: ['./.env.test'],
184
- extraConfigs: [],
185
- providers: [],
186
- imports: [],
187
- exports: [],
188
- controllers: [],
189
- });
190
- expect(metadata).toBeDefined();
191
- expect(metadata.controllers).toEqual([]);
192
- });
193
-
194
- it('should create base app module metadata with options and controllers, with envPath as a string', () => {
195
- class TestController {}
196
-
197
- const metadata = yalcBaseAppModuleMetadataFactory(
198
- DummyStaticModule1,
199
- 'appAlias',
200
- {
201
- envPath: './.env.test',
202
- extraConfigs: [],
203
- providers: [],
204
- imports: [],
205
- exports: [],
206
- controllers: [TestController],
207
- },
208
- );
209
- expect(metadata).toBeDefined();
210
- expect(metadata.controllers).toEqual([TestController]);
211
- });
212
-
213
- it('should create base app module metadata without envPath', () => {
214
- class TestController {}
215
-
216
- const metadata = yalcBaseAppModuleMetadataFactory(
217
- DummyStaticModule1,
218
- 'appAlias',
219
- {
220
- extraConfigs: [],
221
- providers: [],
222
- imports: [],
223
- exports: [],
224
- controllers: [TestController],
225
- },
226
- );
227
- expect(metadata).toBeDefined();
228
- expect(metadata.controllers).toEqual([TestController]);
229
- });
230
- });
231
- });
1
+ import { describe, expect, it } from '@jest/globals';
2
+ import { Test } from '@nestjs/testing';
3
+ import {
4
+ YalcBaseAppModule,
5
+ registerSingletonDynamicModule,
6
+ envFilePathList,
7
+ buildEnvFilePath,
8
+ yalcBaseAppModuleMetadataFactory,
9
+ YalcDefaultAppModule,
10
+ getCachedModule,
11
+ YalcGlobalStaticModule,
12
+ } from '../base-app-module.helper.js';
13
+ import { LifeCycleHandler } from '../life-cycle-handler.service.js';
14
+ import { DynamicModule, Module } from '@nestjs/common';
15
+ import { IYalcBaseAppOptions } from '../base-app.interface.js';
16
+ import { createMock } from '@golevelup/ts-jest';
17
+ import { AppContextService } from '../app-context.service.js';
18
+ import {
19
+ MAIN_APP_CONFIG_SERVICE,
20
+ SYSTEM_LOGGER_SERVICE,
21
+ } from '../def.const.js';
22
+ import { ConfigModule, ConfigService } from '@nestjs/config';
23
+
24
+ let appContextService: AppContextService = createMock<AppContextService>({
25
+ initializedApps: new Set(),
26
+ });
27
+
28
+ class DummyDynamicModule extends YalcBaseAppModule {
29
+ static forRoot(options?: IYalcBaseAppOptions): DynamicModule {
30
+ return this.assignDynamicProperties(
31
+ yalcBaseAppModuleMetadataFactory(this, 'appAlias', {
32
+ ...this.assignDynamicProperties({}),
33
+ configFactory: () => ({}),
34
+ logger: true,
35
+ isSingleton: true,
36
+ imports: [YalcDefaultAppModule.forRoot('appAlias', [])],
37
+ ...options,
38
+ }),
39
+ options,
40
+ );
41
+ }
42
+ }
43
+
44
+ @Module({
45
+ imports: [
46
+ DummyDynamicModule.forRoot({
47
+ providers: [
48
+ {
49
+ provide: 'test1',
50
+ useValue: 'test',
51
+ },
52
+ ],
53
+ }),
54
+ ],
55
+ })
56
+ class DummyStaticModule1 {}
57
+
58
+ @Module({
59
+ imports: [
60
+ DummyDynamicModule.forRoot({
61
+ providers: [
62
+ {
63
+ provide: 'test2',
64
+ useValue: 'test',
65
+ },
66
+ ],
67
+ }),
68
+ ],
69
+ })
70
+ class DummyStaticModule2 {}
71
+
72
+ describe('base-app', () => {
73
+ describe('actual Nest module instantiation', () => {
74
+ it('should create dynamic module', async () => {
75
+ const module = await Test.createTestingModule({
76
+ imports: [DummyStaticModule2, DummyStaticModule1],
77
+ })
78
+ .overrideProvider(AppContextService)
79
+ .useValue(appContextService);
80
+
81
+ const moduleRef = await module.compile();
82
+
83
+ const lifeCycleHandler = moduleRef.get(LifeCycleHandler);
84
+ const appConfigService = moduleRef.get(MAIN_APP_CONFIG_SERVICE);
85
+ const systemLoggerService = moduleRef.get(SYSTEM_LOGGER_SERVICE);
86
+
87
+ expect(module).toBeDefined();
88
+ expect(lifeCycleHandler).toBeDefined();
89
+ expect(appConfigService).toBeDefined();
90
+ expect(systemLoggerService).toBeDefined();
91
+ });
92
+
93
+ it('should create dynamic module with logger option', async () => {
94
+ const module = await Test.createTestingModule({
95
+ imports: [
96
+ ConfigModule.forRoot({ isGlobal: true }),
97
+ YalcDefaultAppModule.forRoot('appAlias2', [], {
98
+ logger: () => ({
99
+ provide: SYSTEM_LOGGER_SERVICE,
100
+ useFactory: () => ({
101
+ error: jest.fn(),
102
+ log: jest.fn(),
103
+ warn: jest.fn(),
104
+ debug: jest.fn(),
105
+ verbose: jest.fn(),
106
+ setContext: jest.fn(),
107
+ } as any),
108
+ } as any),
109
+ }),
110
+ ],
111
+ })
112
+ .overrideProvider(AppContextService)
113
+ .useValue(appContextService)
114
+ .overrideProvider(ConfigService)
115
+ .useValue({ get: () => ({}) });
116
+
117
+ const moduleRef = await module.compile();
118
+ const systemLoggerService = moduleRef.get(SYSTEM_LOGGER_SERVICE);
119
+ expect(systemLoggerService).toBeDefined();
120
+ });
121
+ });
122
+
123
+ describe('YalcBaseAppModule', () => {
124
+ it('should be defined', () => {
125
+ expect(YalcBaseAppModule).toBeDefined();
126
+ });
127
+
128
+ it('should create dynamic properties', () => {
129
+ const dynamicProperties = YalcBaseAppModule.assignDynamicProperties({});
130
+ expect(dynamicProperties).toEqual({
131
+ module: YalcBaseAppModule,
132
+ global: true,
133
+ isSingleton: false,
134
+ });
135
+ });
136
+
137
+ it('should create dynamic properties with options', () => {
138
+ const dynamicProperties = YalcBaseAppModule.assignDynamicProperties(
139
+ {},
140
+ {
141
+ global: false,
142
+ isSingleton: true,
143
+ },
144
+ );
145
+ expect(dynamicProperties).toEqual({
146
+ module: YalcBaseAppModule,
147
+ global: false,
148
+ isSingleton: true,
149
+ });
150
+ });
151
+
152
+ it('should create dynamic module for standalone', () => {
153
+ const dynamicModule = YalcBaseAppModule._forRootStandalone('appAlias');
154
+ expect(dynamicModule).toBeDefined();
155
+ expect(dynamicModule.global).toBe(true);
156
+ expect(dynamicModule.isSingleton).toBe(false);
157
+ });
158
+
159
+ it('should create dynamic module', () => {
160
+ const dynamicModule = YalcBaseAppModule._forRoot('appAlias');
161
+ expect(dynamicModule).toBeDefined();
162
+ expect(dynamicModule.global).toBe(true);
163
+ expect(dynamicModule.isSingleton).toBe(false);
164
+ });
165
+ });
166
+
167
+ describe('registerSingletonDynamicModule', () => {
168
+ class TestModule extends DummyDynamicModule {
169
+ static forRoot(options?: IYalcBaseAppOptions | undefined): DynamicModule {
170
+ return this._forRoot('test', options);
171
+ }
172
+ }
173
+
174
+ it('should register singleton dynamic module', () => {
175
+ const config = TestModule.forRoot();
176
+ const result = registerSingletonDynamicModule(true, TestModule, config);
177
+ expect(result).toBe(config);
178
+ });
179
+
180
+ it('should not register the same singleton dynamic module twice', () => {
181
+ const result = registerSingletonDynamicModule(
182
+ true,
183
+ TestModule,
184
+ TestModule.forRoot(),
185
+ );
186
+ expect(result).not.toBe(true);
187
+ });
188
+ });
189
+
190
+ describe('getCachedModule', () => {
191
+ it('should return null when isSingleton is false', () => {
192
+ expect(getCachedModule('someModule', false)).toBeNull();
193
+ });
194
+
195
+ it('should return cached module if it exists and isSingleton is true', () => {
196
+ registerSingletonDynamicModule(true, 'someModule', 'cachedData');
197
+ expect(getCachedModule('someModule', true)).toBe('cachedData');
198
+ });
199
+
200
+ it('should handle undefined isSingleton', () => {
201
+ expect(getCachedModule('someModule')).toBeNull();
202
+ });
203
+ });
204
+
205
+ describe('envFilePathList', () => {
206
+ it('should create env file path list with undefined argument', () => {
207
+ const paths = envFilePathList();
208
+ expect(paths).toContain('./.env');
209
+ });
210
+ it('should behave same as buildEnvFilePath', () => {
211
+ expect(buildEnvFilePath('.')).toEqual(envFilePathList('.'));
212
+ });
213
+ it('should create env file path list', () => {
214
+ expect(YalcGlobalStaticModule).toBeDefined();
215
+ const paths = envFilePathList('.');
216
+ expect(paths).toContain('./.env');
217
+ if (process.env.NODE_ENV) {
218
+ expect(paths).toContain(`./.env.${process.env.NODE_ENV}`);
219
+ }
220
+ if (process.env.NODE_ENV !== 'production') {
221
+ expect(paths).toContain('./.env.dist');
222
+ }
223
+ });
224
+
225
+ it('should create env file path list for production', () => {
226
+ process.env.NODE_ENV = 'production';
227
+ const paths = envFilePathList('.');
228
+ expect(paths).toContain('./.env');
229
+ expect(paths).toContain(`./.env.production`);
230
+ expect(paths).not.toContain('./.env.dist');
231
+ process.env.NODE_ENV = 'test';
232
+ });
233
+ });
234
+
235
+ describe('baseAppModuleMetadataFactory', () => {
236
+ it('should create base app module metadata', () => {
237
+ const metadata = yalcBaseAppModuleMetadataFactory({}, 'appAlias', {
238
+ logger: true,
239
+ global: true,
240
+ });
241
+ expect(metadata).toBeDefined();
242
+ expect(metadata.controllers).toEqual([]);
243
+ });
244
+
245
+ it('should create base app module metadata with options', () => {
246
+ const metadata = yalcBaseAppModuleMetadataFactory({}, 'appAlias', {
247
+ envPath: ['./.env.test'],
248
+ extraConfigs: [],
249
+ providers: [],
250
+ imports: [],
251
+ exports: [],
252
+ controllers: [],
253
+ });
254
+ expect(metadata).toBeDefined();
255
+ expect(metadata.controllers).toEqual([]);
256
+ });
257
+
258
+ it('should create base app module metadata with options and controllers, with envPath as a string', () => {
259
+ class TestController {}
260
+
261
+ const metadata = yalcBaseAppModuleMetadataFactory(
262
+ DummyStaticModule1,
263
+ 'appAlias',
264
+ {
265
+ envPath: './.env.test',
266
+ extraConfigs: [],
267
+ providers: [],
268
+ imports: [],
269
+ exports: [],
270
+ controllers: [TestController],
271
+ },
272
+ );
273
+ expect(metadata).toBeDefined();
274
+ expect(metadata.controllers).toEqual([TestController]);
275
+ });
276
+
277
+ it('should create base app module metadata without envPath', () => {
278
+ class TestController {}
279
+
280
+ const metadata = yalcBaseAppModuleMetadataFactory(
281
+ DummyStaticModule1,
282
+ 'appAlias',
283
+ {
284
+ extraConfigs: [],
285
+ providers: [],
286
+ imports: [],
287
+ exports: [],
288
+ controllers: [TestController],
289
+ },
290
+ );
291
+ expect(metadata).toBeDefined();
292
+ expect(metadata.controllers).toEqual([TestController]);
293
+ });
294
+ });
295
+ });