@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,12 +1,12 @@
1
- import { describe, expect, it } from '@jest/globals';
2
- import { AppEvents } from '../app.events.js';
3
-
4
- describe('AppEvents enum', () => {
5
- it('should expose the expected event names', () => {
6
- expect(AppEvents.BEFORE_ALL_ROUTES).toBe('app.before_all_routes');
7
- expect(AppEvents.AFTER_ALL_ROUTES).toBe('app.after_all_routes');
8
- expect(AppEvents.BEFORE_GQL_CONTEXT_MIDDLEWARE).toBe(
9
- 'app.before_gql_context_middleware',
10
- );
11
- });
12
- });
1
+ import { describe, expect, it } from '@jest/globals';
2
+ import { AppEvents } from '../app.events.js';
3
+
4
+ describe('AppEvents enum', () => {
5
+ it('should expose the expected event names', () => {
6
+ expect(AppEvents.BEFORE_ALL_ROUTES).toBe('app.before_all_routes');
7
+ expect(AppEvents.AFTER_ALL_ROUTES).toBe('app.after_all_routes');
8
+ expect(AppEvents.BEFORE_GQL_CONTEXT_MIDDLEWARE).toBe(
9
+ 'app.before_gql_context_middleware',
10
+ );
11
+ });
12
+ });
@@ -1,204 +1,71 @@
1
- import { jest } from '@jest/globals';
2
- import '@nest-yalc-2/jest/common-mocks.helper.js';
3
- import { expect, describe, it, beforeEach, afterEach } from '@jest/globals';
4
- import { createMock, DeepMocked } from '@golevelup/ts-jest';
5
- import { DynamicModule, INestApplicationContext, Module } from '@nestjs/common';
6
- import { NestFactory } from '@nestjs/core';
7
- import { executeStandaloneFunction } from '../app.helper.js';
8
- import {
9
- getBootstrappedApps,
10
- getMainBootstrappedApp,
11
- } from '../app-bootstrap-base.helper.js';
12
- import { AppBootstrap } from '../app-bootstrap.helper.js';
13
- import { yalcBaseAppModuleMetadataFactory } from '../base-app-module.helper.js';
14
-
15
- @Module(
16
- yalcBaseAppModuleMetadataFactory(TestModule1, 'test1', {
17
- configFactory: () => ({}),
18
- logger: true,
19
- }),
20
- )
21
- class TestModule1 {}
22
-
23
- @Module(
24
- yalcBaseAppModuleMetadataFactory(TestModule2, 'test2', {
25
- configFactory: () => ({}),
26
- logger: true,
27
- }),
28
- )
29
- class TestModule2 {}
30
-
31
- @Module(
32
- yalcBaseAppModuleMetadataFactory(BrokenModule, 'brokenModule', {
33
- configFactory: () => ({}),
34
- logger: true,
35
- providers: [
36
- {
37
- provide: 'brokenService',
38
- useFactory: () => {
39
- throw new Error('Test');
40
- },
41
- },
42
- ],
43
- }),
44
- )
45
- class BrokenModule {}
46
-
47
- describe('test standalone app functions', () => {
48
- let mockedModule: DeepMocked<DynamicModule>;
49
- let mockedServiceFunction: jest.Mock<() => string>;
50
-
51
- beforeEach(async () => {
52
- jest.resetAllMocks();
53
- mockedServiceFunction = jest.fn(() => 'Test');
54
-
55
- mockedModule = createMock<DynamicModule>({
56
- imports: [],
57
- });
58
-
59
- const mockedCreateApplicationContext = jest.spyOn(
60
- NestFactory,
61
- 'createApplicationContext',
62
- );
63
-
64
- mockedCreateApplicationContext.mockImplementation(
65
- () =>
66
- createMock<INestApplicationContext>({
67
- get: mockedServiceFunction,
68
- }) as any,
69
- );
70
- });
71
-
72
- afterEach(() => {
73
- getBootstrappedApps().forEach((app) => app.closeApp());
74
- getBootstrappedApps().clear();
75
- });
76
-
77
- it('should run executeStandaloneFunction', async () => {
78
- const mockedFunction = jest.fn(async (service: any) => {
79
- return service;
80
- });
81
-
82
- await executeStandaloneFunction(
83
- mockedModule,
84
- mockedServiceFunction,
85
- mockedFunction,
86
- {},
87
- { closeApp: true },
88
- );
89
-
90
- expect(mockedFunction).toHaveBeenCalledTimes(1);
91
- });
92
-
93
- it('should run executeStandaloneFunction without close app', async () => {
94
- const mockedFunction = jest.fn(async (service: any) => {
95
- return service;
96
- });
97
-
98
- await executeStandaloneFunction(
99
- mockedModule,
100
- mockedServiceFunction,
101
- mockedFunction,
102
- {},
103
- );
104
-
105
- expect(mockedFunction).toHaveBeenCalledTimes(1);
106
- });
107
-
108
- it('should run executeStandaloneFunction with class module', async () => {
109
- const mockedFunction = jest.fn(async (service: any) => {
110
- return service;
111
- });
112
-
113
- class TestModule {
114
- constructor() {}
115
- }
116
-
117
- await executeStandaloneFunction(
118
- TestModule,
119
- mockedServiceFunction,
120
- mockedFunction,
121
- {},
122
- );
123
-
124
- expect(mockedFunction).toHaveBeenCalledTimes(1);
125
- });
126
-
127
- it('should trigger an error if we bootstrap multiple servers', async () => {
128
- await new AppBootstrap('test1', TestModule1).initApp();
129
-
130
- let error: any = null;
131
- try {
132
- await new AppBootstrap('test2', TestModule2, {}).initApp();
133
- } catch (e: any) {
134
- // eslint-disable-next-line no-console
135
- console.log(e);
136
- error = e;
137
- }
138
-
139
- await expect(error).not.toBe(null);
140
- });
141
-
142
- it('should not trigger an error when the first app cannot be initialized because of an error', async () => {
143
- try {
144
- await new AppBootstrap('brokenModule', BrokenModule).initApp();
145
- } catch (e: any) {
146
- // nothing
147
- }
148
-
149
- let error: any = null;
150
- try {
151
- await new AppBootstrap('test2', TestModule2, {}).initApp();
152
- } catch (e: any) {
153
- // eslint-disable-next-line no-console
154
- console.log(e);
155
- error = e;
156
- }
157
-
158
- await expect(error).toBe(null);
159
- });
160
-
161
- it('should not trigger an error if we bootstrap multiple servers with the skip option', async () => {
162
- await new AppBootstrap('test1', TestModule1, {
163
- skipMultiServerCheck: true,
164
- }).initApp();
165
-
166
- let error: any = null;
167
- try {
168
- await new AppBootstrap('test2', TestModule2, {
169
- skipMultiServerCheck: true,
170
- }).initApp();
171
- } catch (e: any) {
172
- // eslint-disable-next-line no-console
173
- console.log(e);
174
- error = e;
175
- }
176
-
177
- await expect(error).toBe(null);
178
- });
179
-
180
- it('should not trigger an error if we bootstrap multiple servers with the env variable', async () => {
181
- process.env.APP_SKIP_MULTISERVER_CHECK = 'true';
182
-
183
- await new AppBootstrap('test1', TestModule1).initApp();
184
-
185
- let error: any = null;
186
- try {
187
- await new AppBootstrap('test2', TestModule2).initApp();
188
- } catch (e: any) {
189
- // eslint-disable-next-line no-console
190
- console.log(e);
191
- error = e;
192
- }
193
-
194
- await expect(error).toBe(null);
195
- });
196
-
197
- it('should get the main bootstrapped app', async () => {
198
- const app = await new AppBootstrap('test1', TestModule1).initApp();
199
-
200
- const mainApp = getMainBootstrappedApp();
201
-
202
- expect(mainApp === app).toBe(true);
203
- });
204
- });
1
+ import { describe, expect, it, jest, beforeEach } from '@jest/globals';
2
+ import { StandaloneAppBootstrap } from '../app-bootstrap-standalone.helper.js';
3
+
4
+ import {
5
+ executeFunctionForApp,
6
+ curriedExecuteStandaloneFunction,
7
+ executeStandaloneFunction,
8
+ isDynamicModule,
9
+ } from '../app.helper.js';
10
+
11
+ describe('app.helper', () => {
12
+ beforeEach(() => {
13
+ jest.restoreAllMocks();
14
+ jest.spyOn(StandaloneAppBootstrap.prototype, 'initApp').mockResolvedValue({
15
+ getApp: jest.fn().mockReturnValue({
16
+ init: jest.fn(),
17
+ resolve: jest.fn().mockResolvedValue({}),
18
+ }),
19
+ } as any);
20
+ jest.spyOn(StandaloneAppBootstrap.prototype, 'getApp').mockReturnValue({
21
+ init: jest.fn(),
22
+ resolve: jest.fn().mockResolvedValue({}),
23
+ } as any);
24
+ jest.spyOn(StandaloneAppBootstrap.prototype, 'closeApp').mockResolvedValue({} as any);
25
+ });
26
+
27
+ it('should evaluate isDynamicModule', () => {
28
+ expect(isDynamicModule({ module: {} })).toBe(true);
29
+ expect(isDynamicModule({})).toBe(false);
30
+ });
31
+
32
+ it('should execute function for app', async () => {
33
+ const mockApp = {
34
+ getApp: jest.fn().mockReturnValue({
35
+ init: jest.fn(),
36
+ resolve: jest.fn().mockResolvedValue({}),
37
+ } as any),
38
+ closeApp: jest.fn(),
39
+ };
40
+
41
+ const mockFn = jest.fn().mockResolvedValue({});
42
+
43
+ await executeFunctionForApp(mockApp as any, {}, mockFn as any, { closeApp: true });
44
+
45
+ expect(mockApp.getApp).toHaveBeenCalled();
46
+ expect(mockFn).toHaveBeenCalled();
47
+ expect(mockApp.closeApp).toHaveBeenCalled();
48
+ });
49
+
50
+ it('should create curried function', async () => {
51
+ class DummyModule {}
52
+ const curried = await curriedExecuteStandaloneFunction(DummyModule, { skipMultiServerCheck: true } as any);
53
+ expect(typeof curried).toBe('function');
54
+ });
55
+
56
+ it('should create curried function with dynamic module', async () => {
57
+ const dynamicModule = { module: { name: 'DynamicDummy' } };
58
+ const curried = await curriedExecuteStandaloneFunction(dynamicModule, { skipMultiServerCheck: true } as any);
59
+ expect(typeof curried).toBe('function');
60
+ });
61
+
62
+ it('should execute standalone function', async () => {
63
+ class DummyModule {}
64
+ class DummyService {}
65
+ const mockFn = jest.fn().mockResolvedValue({});
66
+
67
+ await executeStandaloneFunction(DummyModule, DummyService, mockFn, { skipMultiServerCheck: true } as any);
68
+
69
+ expect(mockFn).toHaveBeenCalled();
70
+ });
71
+ });