@nest-yalc-2/app 2.2.2 → 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 +5 -3
  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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nest-yalc-2/app",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "./src/index.js",
@@ -16,13 +16,15 @@
16
16
  "src"
17
17
  ],
18
18
  "dependencies": {
19
- "tslib": "^2.6.3"
19
+ "tslib": "^2.8.1"
20
20
  },
21
21
  "keywords": [
22
22
  "nestjs",
23
+ "nestjs-yalc",
23
24
  "yalc",
25
+ "app",
24
26
  "framework",
25
- "app"
27
+ "utility"
26
28
  ],
27
29
  "repository": {
28
30
  "type": "git",
@@ -1,38 +1,86 @@
1
- import { describe, expect, it, jest } from '@jest/globals';
2
- import { BaseAppBootstrap } from '../app-bootstrap-base.helper.js';
3
-
4
- class DummyModule {}
5
-
6
- class TestBootstrap extends BaseAppBootstrap<any> {
7
- constructor() {
8
- super('test-app', DummyModule as any, {
9
- globalsOptions: { skipMultiServerCheck: true },
10
- });
11
- }
12
- }
13
-
14
- describe('BaseAppBootstrap', () => {
15
- it('should monkey patch app init/close and update flags', async () => {
16
- const bootstrap = new TestBootstrap();
17
-
18
- const closeFn = jest.fn().mockResolvedValue(undefined);
19
- const initFn = jest.fn().mockResolvedValue(undefined);
20
-
21
- const fakeApp: any = {
22
- close: closeFn,
23
- init: initFn,
24
- get: jest.fn(),
25
- useLogger: jest.fn(),
26
- };
27
-
28
- bootstrap.setApp(fakeApp);
29
-
30
- expect(bootstrap.isAppClosed()).toBe(false);
31
- await fakeApp.init();
32
- expect(bootstrap.isAppClosed()).toBe(false);
33
-
34
- await bootstrap.closeApp();
35
- expect(closeFn).toHaveBeenCalled();
36
- expect(bootstrap.isAppClosed()).toBe(true);
37
- });
38
- });
1
+ import { describe, expect, it, jest } from '@jest/globals';
2
+ import { BaseAppBootstrap } from '../app-bootstrap-base.helper.js';
3
+
4
+ class DummyModule {}
5
+
6
+ class TestBootstrap extends BaseAppBootstrap<any> {
7
+ constructor() {
8
+ super('test-app', DummyModule as any, {
9
+ globalsOptions: { skipMultiServerCheck: true },
10
+ });
11
+ }
12
+ }
13
+
14
+ describe('BaseAppBootstrap', () => {
15
+ it('should monkey patch app init/close and update flags', async () => {
16
+ const bootstrap = new TestBootstrap();
17
+
18
+ const closeFn = jest.fn().mockResolvedValue(undefined);
19
+ const initFn = jest.fn().mockResolvedValue(undefined);
20
+
21
+ const fakeApp: any = {
22
+ close: closeFn,
23
+ init: initFn,
24
+ get: jest.fn(),
25
+ useLogger: jest.fn(),
26
+ };
27
+
28
+ bootstrap.setApp(fakeApp);
29
+
30
+ expect(bootstrap.isAppClosed()).toBe(false);
31
+ await fakeApp.init();
32
+ expect(bootstrap.isAppClosed()).toBe(false);
33
+
34
+ await bootstrap.closeApp();
35
+ expect(closeFn).toHaveBeenCalled();
36
+ expect(bootstrap.isAppClosed()).toBe(true);
37
+ });
38
+
39
+ it('should getConf and getModule correctly', () => {
40
+ const bootstrap = new TestBootstrap();
41
+ const mockConfig = { get: jest.fn().mockReturnValue('mock-conf') };
42
+ const fakeApp: any = {
43
+ close: jest.fn(),
44
+ init: jest.fn(),
45
+ get: jest.fn().mockReturnValue(mockConfig),
46
+ useLogger: jest.fn(),
47
+ };
48
+ bootstrap.setApp(fakeApp);
49
+
50
+ expect(bootstrap.getConf()).toEqual('mock-conf');
51
+ expect(bootstrap.getModule()).toBeDefined();
52
+ expect(mockConfig.get).toHaveBeenCalledWith('test-app');
53
+ });
54
+
55
+ it('should throw error when bootstrapping multiple servers without skip flag', () => {
56
+ // We already bootstrapped TestBootstrap in the previous tests.
57
+ // It is in getBootstrappedApps() unless we clear it.
58
+ // But since we want to trigger line 81, we can just instantiate another one without skip flag.
59
+ class TestBootstrapNoSkip extends BaseAppBootstrap<any> {
60
+ constructor() {
61
+ super('test-app-2', DummyModule as any, {
62
+ globalsOptions: { skipMultiServerCheck: false },
63
+ });
64
+ }
65
+ }
66
+
67
+ // Set env to not skip
68
+ const oldEnv = process.env.APP_SKIP_MULTISERVER_CHECK;
69
+ process.env.APP_SKIP_MULTISERVER_CHECK = 'false';
70
+
71
+ expect(() => new TestBootstrapNoSkip()).toThrow('You are trying to bootstrap multiple servers');
72
+
73
+ process.env.APP_SKIP_MULTISERVER_CHECK = oldEnv;
74
+ });
75
+
76
+ it('getMainBootstrappedApp should return the first app', () => {
77
+ const { getMainBootstrappedApp } = require('../app-bootstrap-base.helper.js');
78
+ const mainApp = getMainBootstrappedApp();
79
+ expect(mainApp).toBeDefined();
80
+ });
81
+
82
+ it('should return bootstrapped apps', () => {
83
+ const apps = require('../app-bootstrap-base.helper.js').getBootstrappedApps();
84
+ expect(apps).toBeDefined();
85
+ });
86
+ });
@@ -1,35 +1,35 @@
1
- import { describe, expect, it, jest, beforeEach } from '@jest/globals';
2
- import { StandaloneAppBootstrap } from '../app-bootstrap-standalone.helper.js';
3
- import { NestFactory } from '@nestjs/core';
4
-
5
- describe('StandaloneAppBootstrap', () => {
6
- const logger = { log: jest.fn(), debug: jest.fn() };
7
- const initMock = jest.fn().mockResolvedValue(undefined);
8
- const fakeApp = {
9
- init: initMock,
10
- close: jest.fn().mockResolvedValue(undefined),
11
- get: jest.fn().mockReturnValue(logger),
12
- useLogger: jest.fn(),
13
- };
14
-
15
- beforeEach(() => {
16
- jest.clearAllMocks();
17
- jest
18
- .spyOn(NestFactory, 'createApplicationContext')
19
- .mockResolvedValue(fakeApp as any);
20
- });
21
-
22
- it('should create application context and initialize', async () => {
23
- const bootstrap = new StandaloneAppBootstrap(
24
- 'standalone',
25
- class Dummy {},
26
- { skipMultiServerCheck: true } as any,
27
- );
28
-
29
- await bootstrap.initApp();
30
-
31
- expect(NestFactory.createApplicationContext).toHaveBeenCalled();
32
- expect(initMock).toHaveBeenCalled();
33
- expect(bootstrap.getApp()).toBe(fakeApp);
34
- });
35
- });
1
+ import { describe, expect, it, jest, beforeEach } from '@jest/globals';
2
+ import { StandaloneAppBootstrap } from '../app-bootstrap-standalone.helper.js';
3
+ import { NestFactory } from '@nestjs/core';
4
+
5
+ describe('StandaloneAppBootstrap', () => {
6
+ const logger = { log: jest.fn(), debug: jest.fn() };
7
+ const initMock = jest.fn().mockResolvedValue(undefined);
8
+ const fakeApp = {
9
+ init: initMock,
10
+ close: jest.fn().mockResolvedValue(undefined),
11
+ get: jest.fn().mockReturnValue(logger),
12
+ useLogger: jest.fn(),
13
+ };
14
+
15
+ beforeEach(() => {
16
+ jest.clearAllMocks();
17
+ jest
18
+ .spyOn(NestFactory, 'createApplicationContext')
19
+ .mockResolvedValue(fakeApp as any);
20
+ });
21
+
22
+ it('should create application context and initialize', async () => {
23
+ const bootstrap = new StandaloneAppBootstrap(
24
+ 'standalone',
25
+ class Dummy {},
26
+ { skipMultiServerCheck: true } as any,
27
+ );
28
+
29
+ await bootstrap.initApp();
30
+
31
+ expect(NestFactory.createApplicationContext).toHaveBeenCalled();
32
+ expect(initMock).toHaveBeenCalled();
33
+ expect(bootstrap.getApp()).toBe(fakeApp);
34
+ });
35
+ });
@@ -1,98 +1,162 @@
1
- import { describe, expect, it, jest, beforeEach } from '@jest/globals';
2
- import '@nest-yalc-2/jest/common-mocks.helper.js';
3
-
4
- import { AppBootstrap } from '../app-bootstrap.helper.js';
5
- import { SYSTEM_LOGGER_SERVICE } from '../def.const.js';
6
- import { UnwrapResultInterceptor } from '../unwrap-result.interceptor.js';
7
- import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
8
-
9
- jest.mock('class-validator', () => ({
10
- useContainer: jest.fn(),
11
- }));
12
-
13
- jest.mock('@fastify/cookie', () => jest.fn());
14
-
15
- describe('AppBootstrap.applyBootstrapGlobals', () => {
16
- const logger = { log: jest.fn(), debug: jest.fn() };
17
- const customFilter = { handle: jest.fn() };
18
- const createDocumentMock = jest.fn().mockReturnValue({ doc: true });
19
- const setupSwaggerMock = jest.fn();
20
- const setTitleMock = jest.fn().mockReturnThis();
21
- const setDescriptionMock = jest.fn().mockReturnThis();
22
- const buildMock = jest.fn().mockReturnValue({ swagger: true });
23
-
24
- const fakeApp = {
25
- useGlobalPipes: jest.fn(),
26
- setGlobalPrefix: jest.fn(),
27
- register: jest.fn(),
28
- useGlobalInterceptors: jest.fn(),
29
- useGlobalFilters: jest.fn(),
30
- useLogger: jest.fn(),
31
- get: jest.fn().mockImplementation((token) =>
32
- token === SYSTEM_LOGGER_SERVICE ? logger : undefined,
33
- ),
34
- select: jest.fn().mockReturnValue({}),
35
- };
36
-
37
- beforeEach(() => {
38
- jest.restoreAllMocks();
39
- jest.clearAllMocks();
40
- jest.spyOn(SwaggerModule, 'createDocument').mockImplementation((...args: any[]) =>
41
- createDocumentMock(...args),
42
- );
43
- jest.spyOn(SwaggerModule, 'setup').mockImplementation((...args: any[]) =>
44
- setupSwaggerMock(...args),
45
- );
46
- jest
47
- .spyOn(DocumentBuilder.prototype, 'setTitle')
48
- .mockImplementation(function (...args: any[]) {
49
- setTitleMock(...args);
50
- return this;
51
- });
52
- jest
53
- .spyOn(DocumentBuilder.prototype, 'setDescription')
54
- .mockImplementation(function (...args: any[]) {
55
- setDescriptionMock(...args);
56
- return this;
57
- });
58
- jest.spyOn(DocumentBuilder.prototype, 'build').mockImplementation((...args: any[]) =>
59
- buildMock(...args),
60
- );
61
- });
62
-
63
- it('should set up pipes, filters, interceptors and swagger', async () => {
64
- const bootstrap = new AppBootstrap(
65
- 'app',
66
- class Dummy {},
67
- { skipMultiServerCheck: true } as any,
68
- );
69
-
70
- bootstrap['app'] = fakeApp as any;
71
- bootstrap['loggerService'] = logger as any;
72
- bootstrap.getConf = jest.fn().mockReturnValue({
73
- apiPrefix: 'api',
74
- host: 'localhost',
75
- port: 3000,
76
- }) as any;
77
- bootstrap.getModule = jest.fn().mockReturnValue({}) as any;
78
-
79
- await bootstrap.applyBootstrapGlobals({
80
- enableSwagger: true,
81
- apiPrefix: 'api',
82
- filters: [customFilter as any],
83
- validationPipeOptions: { transformOptions: { enableImplicitConversion: true } },
84
- });
85
-
86
- expect(fakeApp.useGlobalPipes).toHaveBeenCalled();
87
- expect(fakeApp.setGlobalPrefix).toHaveBeenCalledWith('api');
88
- expect(fakeApp.register).toHaveBeenCalled();
89
- expect(fakeApp.useGlobalInterceptors).toHaveBeenCalledWith(
90
- expect.any(UnwrapResultInterceptor),
91
- );
92
- expect(fakeApp.useGlobalFilters).toHaveBeenCalled();
93
- expect(createDocumentMock).toHaveBeenCalled();
94
- expect(setTitleMock).toHaveBeenCalled();
95
- expect(setDescriptionMock).toHaveBeenCalled();
96
- expect(setupSwaggerMock).toHaveBeenCalled();
97
- });
98
- });
1
+ import { describe, expect, it, jest, beforeEach } from '@jest/globals';
2
+ import '@nest-yalc-2/jest/common-mocks.helper.js';
3
+
4
+ import { AppBootstrap } from '../app-bootstrap.helper.js';
5
+ import { SYSTEM_LOGGER_SERVICE } from '../def.const.js';
6
+ import { UnwrapResultInterceptor } from '../unwrap-result.interceptor.js';
7
+ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
8
+
9
+ jest.mock('class-validator', () => ({
10
+ useContainer: jest.fn(),
11
+ }));
12
+
13
+ jest.mock('@fastify/cookie', () => jest.fn());
14
+
15
+ describe('AppBootstrap.applyBootstrapGlobals', () => {
16
+ const logger = { log: jest.fn(), debug: jest.fn() };
17
+ const customFilter = { handle: jest.fn() };
18
+ const createDocumentMock = jest.fn().mockReturnValue({ doc: true });
19
+ const setupSwaggerMock = jest.fn();
20
+ const setTitleMock = jest.fn().mockReturnThis();
21
+ const setDescriptionMock = jest.fn().mockReturnThis();
22
+ const buildMock = jest.fn().mockReturnValue({ swagger: true });
23
+
24
+ const fakeApp = {
25
+ useGlobalPipes: jest.fn(),
26
+ setGlobalPrefix: jest.fn(),
27
+ register: jest.fn(),
28
+ useGlobalInterceptors: jest.fn(),
29
+ useGlobalFilters: jest.fn(),
30
+ useLogger: jest.fn(),
31
+ get: jest.fn().mockImplementation((token) =>
32
+ token === SYSTEM_LOGGER_SERVICE ? logger : undefined,
33
+ ),
34
+ select: jest.fn().mockReturnValue({}),
35
+ };
36
+
37
+ beforeEach(() => {
38
+ jest.restoreAllMocks();
39
+ jest.clearAllMocks();
40
+ jest.spyOn(SwaggerModule, 'createDocument').mockImplementation((...args: any[]) =>
41
+ createDocumentMock(...args),
42
+ );
43
+ jest.spyOn(SwaggerModule, 'setup').mockImplementation((...args: any[]) =>
44
+ setupSwaggerMock(...args),
45
+ );
46
+ jest
47
+ .spyOn(DocumentBuilder.prototype, 'setTitle')
48
+ .mockImplementation(function (...args: any[]) {
49
+ setTitleMock(...args);
50
+ return this;
51
+ });
52
+ jest
53
+ .spyOn(DocumentBuilder.prototype, 'setDescription')
54
+ .mockImplementation(function (...args: any[]) {
55
+ setDescriptionMock(...args);
56
+ return this;
57
+ });
58
+ jest.spyOn(DocumentBuilder.prototype, 'build').mockImplementation((...args: any[]) =>
59
+ buildMock(...args),
60
+ );
61
+ });
62
+
63
+ it('should set up pipes, filters, interceptors and swagger', async () => {
64
+ const bootstrap = new AppBootstrap(
65
+ 'app',
66
+ class Dummy {},
67
+ { skipMultiServerCheck: true } as any,
68
+ );
69
+
70
+ bootstrap['app'] = fakeApp as any;
71
+ bootstrap['loggerService'] = logger as any;
72
+ bootstrap.getConf = jest.fn().mockReturnValue({
73
+ apiPrefix: 'api',
74
+ host: 'localhost',
75
+ port: 3000,
76
+ }) as any;
77
+ bootstrap.getModule = jest.fn().mockReturnValue({}) as any;
78
+
79
+ await bootstrap.applyBootstrapGlobals({
80
+ enableSwagger: true,
81
+ apiPrefix: 'api',
82
+ filters: [customFilter as any],
83
+ validationPipeOptions: { transformOptions: { enableImplicitConversion: true } },
84
+ });
85
+
86
+ expect(fakeApp.useGlobalPipes).toHaveBeenCalled();
87
+ expect(fakeApp.setGlobalPrefix).toHaveBeenCalledWith('api');
88
+ expect(fakeApp.register).toHaveBeenCalled();
89
+ expect(fakeApp.useGlobalInterceptors).toHaveBeenCalledWith(
90
+ expect.any(UnwrapResultInterceptor),
91
+ );
92
+ expect(fakeApp.useGlobalFilters).toHaveBeenCalled();
93
+ expect(createDocumentMock).toHaveBeenCalled();
94
+ expect(setTitleMock).toHaveBeenCalled();
95
+ expect(setDescriptionMock).toHaveBeenCalled();
96
+ expect(setupSwaggerMock).toHaveBeenCalled();
97
+ });
98
+ });
99
+
100
+ describe('AppBootstrap - additional coverage', () => {
101
+ it('initApp should call createApp and initSetup', async () => {
102
+ const bootstrap = new AppBootstrap('app', class Dummy {}, { skipMultiServerCheck: true } as any);
103
+ bootstrap.createApp = jest.fn().mockResolvedValue(bootstrap as any);
104
+ bootstrap.initSetup = jest.fn().mockResolvedValue(bootstrap as any);
105
+ await bootstrap.initApp();
106
+ expect(bootstrap.createApp).toHaveBeenCalled();
107
+ expect(bootstrap.initSetup).toHaveBeenCalled();
108
+ });
109
+
110
+ it('initSetup should call applyBootstrapGlobals and init', async () => {
111
+ const bootstrap = new AppBootstrap('app', class Dummy {}, { skipMultiServerCheck: true } as any);
112
+ bootstrap.applyBootstrapGlobals = jest.fn();
113
+ const mockApp = { init: jest.fn() };
114
+ bootstrap.getApp = jest.fn().mockReturnValue(mockApp as any);
115
+ await bootstrap.initSetup();
116
+ expect(bootstrap.applyBootstrapGlobals).toHaveBeenCalled();
117
+ expect(mockApp.init).toHaveBeenCalled();
118
+ });
119
+
120
+ it('createApp should call NestFactory.create and setApp', async () => {
121
+ const bootstrap = new AppBootstrap('app', class Dummy {}, { skipMultiServerCheck: true } as any);
122
+ bootstrap.setApp = jest.fn().mockReturnValue(bootstrap as any);
123
+ const mockNestApp = {};
124
+ const NestFactory = require('@nestjs/core').NestFactory;
125
+ jest.spyOn(NestFactory, 'create').mockResolvedValue(mockNestApp as any);
126
+ await bootstrap.createApp();
127
+ expect(NestFactory.create).toHaveBeenCalled();
128
+ expect(bootstrap.setApp).toHaveBeenCalledWith(mockNestApp);
129
+ });
130
+
131
+ it('createApp should handle errors during createApp', async () => {
132
+ const bootstrap = new AppBootstrap('app', class Dummy {}, { skipMultiServerCheck: true } as any);
133
+ const NestFactory = require('@nestjs/core').NestFactory;
134
+ jest.spyOn(NestFactory, 'create').mockRejectedValue(new Error('test error') as never);
135
+ bootstrap.closeCleanup = jest.fn() as any;
136
+ // mock console.error
137
+ const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
138
+ await expect(bootstrap.createApp()).rejects.toThrow('Process aborted');
139
+ expect(bootstrap.closeCleanup).toHaveBeenCalled();
140
+ errSpy.mockRestore();
141
+ });
142
+ });
143
+ describe('AppBootstrap.applyBootstrapGlobals - no options', () => {
144
+ it('should handle undefined options', async () => {
145
+ const bootstrap = new AppBootstrap('app', class Dummy {}, { skipMultiServerCheck: true } as any);
146
+ const fakeApp = {
147
+ useGlobalPipes: jest.fn(),
148
+ setGlobalPrefix: jest.fn(),
149
+ register: jest.fn(),
150
+ useGlobalInterceptors: jest.fn(),
151
+ useGlobalFilters: jest.fn(),
152
+ get: jest.fn().mockReturnValue({ log: jest.fn(), debug: jest.fn() }),
153
+ useLogger: jest.fn(),
154
+ select: jest.fn().mockReturnValue({}),
155
+ };
156
+ bootstrap['app'] = fakeApp as any;
157
+ bootstrap.getConf = jest.fn().mockReturnValue({}) as any;
158
+ bootstrap.getModule = jest.fn().mockReturnValue({}) as any;
159
+ await bootstrap.applyBootstrapGlobals();
160
+ expect(fakeApp.useGlobalPipes).toHaveBeenCalled();
161
+ });
162
+ });
@@ -1,55 +1,55 @@
1
- import { describe, expect, it, jest } from '@jest/globals';
2
- import { ConfigService } from '@nestjs/config';
3
- import {
4
- AppConfigService,
5
- createAppConfigProvider,
6
- getAppConfigToken,
7
- getAppEventToken,
8
- getAppLoggerToken,
9
- } from '../app-config.service.js';
10
-
11
- describe('AppConfigService', () => {
12
- it('should expose config values', () => {
13
- const config = { appName: 'demo' };
14
- const configService = {
15
- get: jest.fn().mockReturnValue(config),
16
- } as unknown as ConfigService;
17
-
18
- const service = new AppConfigService(configService, 'demoAlias');
19
-
20
- expect(service.values).toBe(config);
21
- expect(service.get()).toBe(config);
22
- expect(configService.get).toHaveBeenCalledWith('demoAlias');
23
- });
24
-
25
- it('should throw when config is missing', () => {
26
- const configService = {
27
- get: jest.fn().mockReturnValue(undefined),
28
- } as unknown as ConfigService;
29
-
30
- expect(() => new AppConfigService(configService, 'missing')).toThrow(
31
- "AppConfigService: No config found for app alias 'missing'",
32
- );
33
- });
34
- });
35
-
36
- describe('AppConfigService tokens', () => {
37
- it('should generate token names', () => {
38
- expect(getAppConfigToken('alias')).toBe('aliasConfig');
39
- expect(getAppEventToken('alias')).toBe('aliasEvent');
40
- expect(getAppLoggerToken('alias')).toBe('aliasLogger');
41
- });
42
-
43
- it('should build a provider that resolves AppConfigService', () => {
44
- const provider = createAppConfigProvider('alias');
45
- const config = { foo: 'bar' };
46
- const configService = { get: jest.fn().mockReturnValue(config) };
47
-
48
- const instance = (provider as any).useFactory(
49
- configService as ConfigService,
50
- ) as AppConfigService;
51
-
52
- expect(provider.inject).toEqual([ConfigService]);
53
- expect(instance.values).toBe(config);
54
- });
55
- });
1
+ import { describe, expect, it, jest } from '@jest/globals';
2
+ import { ConfigService } from '@nestjs/config';
3
+ import {
4
+ AppConfigService,
5
+ createAppConfigProvider,
6
+ getAppConfigToken,
7
+ getAppEventToken,
8
+ getAppLoggerToken,
9
+ } from '../app-config.service.js';
10
+
11
+ describe('AppConfigService', () => {
12
+ it('should expose config values', () => {
13
+ const config = { appName: 'demo' };
14
+ const configService = {
15
+ get: jest.fn().mockReturnValue(config),
16
+ } as unknown as ConfigService;
17
+
18
+ const service = new AppConfigService(configService, 'demoAlias');
19
+
20
+ expect(service.values).toBe(config);
21
+ expect(service.get()).toBe(config);
22
+ expect(configService.get).toHaveBeenCalledWith('demoAlias');
23
+ });
24
+
25
+ it('should throw when config is missing', () => {
26
+ const configService = {
27
+ get: jest.fn().mockReturnValue(undefined),
28
+ } as unknown as ConfigService;
29
+
30
+ expect(() => new AppConfigService(configService, 'missing')).toThrow(
31
+ "AppConfigService: No config found for app alias 'missing'",
32
+ );
33
+ });
34
+ });
35
+
36
+ describe('AppConfigService tokens', () => {
37
+ it('should generate token names', () => {
38
+ expect(getAppConfigToken('alias')).toBe('aliasConfig');
39
+ expect(getAppEventToken('alias')).toBe('aliasEvent');
40
+ expect(getAppLoggerToken('alias')).toBe('aliasLogger');
41
+ });
42
+
43
+ it('should build a provider that resolves AppConfigService', () => {
44
+ const provider = createAppConfigProvider('alias');
45
+ const config = { foo: 'bar' };
46
+ const configService = { get: jest.fn().mockReturnValue(config) };
47
+
48
+ const instance = (provider as any).useFactory(
49
+ configService as ConfigService,
50
+ ) as AppConfigService;
51
+
52
+ expect(provider.inject).toEqual([ConfigService]);
53
+ expect(instance.values).toBe(config);
54
+ });
55
+ });
@@ -1,28 +1,28 @@
1
- import { describe, expect, it } from '@jest/globals';
2
- import {
3
- GraphQLObjectType,
4
- GraphQLSchema,
5
- GraphQLString,
6
- } from 'graphql';
7
- import { AppContextService } from '../app-context.service.js';
8
-
9
- describe('AppContextService', () => {
10
- it('should store initialized apps and schema', () => {
11
- const service = new AppContextService();
12
- service.initializedApps.add('app1');
13
-
14
- const schema = new GraphQLSchema({
15
- query: new GraphQLObjectType({
16
- name: 'Query',
17
- fields: {
18
- hello: { type: GraphQLString },
19
- },
20
- }),
21
- });
22
-
23
- service.setSchema(schema);
24
-
25
- expect(service.initializedApps.has('app1')).toBe(true);
26
- expect(service.schema).toBe(schema);
27
- });
28
- });
1
+ import { describe, expect, it } from '@jest/globals';
2
+ import {
3
+ GraphQLObjectType,
4
+ GraphQLSchema,
5
+ GraphQLString,
6
+ } from 'graphql';
7
+ import { AppContextService } from '../app-context.service.js';
8
+
9
+ describe('AppContextService', () => {
10
+ it('should store initialized apps and schema', () => {
11
+ const service = new AppContextService();
12
+ service.initializedApps.add('app1');
13
+
14
+ const schema = new GraphQLSchema({
15
+ query: new GraphQLObjectType({
16
+ name: 'Query',
17
+ fields: {
18
+ hello: { type: GraphQLString },
19
+ },
20
+ }),
21
+ });
22
+
23
+ service.setSchema(schema);
24
+
25
+ expect(service.initializedApps.has('app1')).toBe(true);
26
+ expect(service.schema).toBe(schema);
27
+ });
28
+ });