@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.
- package/package.json +10 -2
- package/src/__tests__/app-bootstrap-base.helper.spec.ts +86 -38
- package/src/__tests__/app-bootstrap-standalone.helper.spec.ts +35 -35
- package/src/__tests__/app-bootstrap.helper.spec.ts +162 -98
- package/src/__tests__/app-config.service.spec.ts +55 -55
- package/src/__tests__/app-context.service.spec.ts +28 -28
- package/src/__tests__/app.events.spec.ts +12 -12
- package/src/__tests__/app.helper.spec.ts +71 -204
- package/src/__tests__/base-app-module.helper.spec.ts +295 -231
- package/src/__tests__/base-app.controller.spec.ts +90 -90
- package/src/__tests__/base-app.service.spec.ts +39 -39
- package/src/__tests__/cls.module.spec.ts +33 -33
- package/src/__tests__/life-cycle-handler.service.spec.ts +57 -48
- package/src/__tests__/unwrap-result.interceptor.spec.ts +52 -52
- package/src/app-bootstrap-base.helper.ts +206 -206
- package/src/app-bootstrap-standalone.helper.ts +67 -67
- package/src/app-bootstrap.helper.js +9 -9
- package/src/app-bootstrap.helper.ts +279 -279
- package/src/app-config.service.ts +61 -61
- package/src/app-context.module.ts +6 -6
- package/src/app-context.service.ts +16 -16
- package/src/app-imports.factory.js +2 -2
- package/src/app-imports.factory.js.map +1 -1
- package/src/app-imports.factory.ts +321 -321
- package/src/app.events.ts +8 -8
- package/src/app.helper.ts +73 -73
- package/src/base-app-module.helper.d.ts +2 -2
- package/src/base-app-module.helper.js +8 -5
- package/src/base-app-module.helper.js.map +1 -1
- package/src/base-app-module.helper.ts +457 -453
- package/src/base-app.controller.ts +51 -51
- package/src/base-app.interface.ts +55 -55
- package/src/base-app.service.ts +33 -33
- package/src/cls.module.d.ts +2 -1
- package/src/cls.module.js.map +1 -1
- package/src/cls.module.ts +76 -75
- package/src/conf.type.ts +22 -22
- package/src/def.const.ts +33 -33
- package/src/global.enum.ts +6 -6
- package/src/life-cycle-handler.service.ts +66 -66
- package/src/unwrap-result.interceptor.ts +32 -32
- package/src/yalc-controller.interface.ts +6 -6
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
import { Test, TestingModule } from '@nestjs/testing';
|
|
2
|
-
import { BaseAppController } from '../base-app.controller.js';
|
|
3
|
-
import { BaseAppService } from '../base-app.service.js';
|
|
4
|
-
import { ConfigService } from '@nestjs/config';
|
|
5
|
-
import { Controller } from '@nestjs/common';
|
|
6
|
-
|
|
7
|
-
@Controller()
|
|
8
|
-
class TestAppController extends BaseAppController {}
|
|
9
|
-
|
|
10
|
-
describe('BaseAppController', () => {
|
|
11
|
-
let controller: TestAppController;
|
|
12
|
-
let mockAppService: any;
|
|
13
|
-
let mockConfigService: any;
|
|
14
|
-
|
|
15
|
-
beforeEach(async () => {
|
|
16
|
-
mockAppService = {
|
|
17
|
-
getHello: jest.fn().mockReturnValue('Hello World'),
|
|
18
|
-
};
|
|
19
|
-
mockConfigService = {
|
|
20
|
-
get: jest.fn(),
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const module: TestingModule = await Test.createTestingModule({
|
|
24
|
-
controllers: [TestAppController],
|
|
25
|
-
providers: [
|
|
26
|
-
{ provide: BaseAppService, useValue: mockAppService },
|
|
27
|
-
{ provide: ConfigService, useValue: mockConfigService },
|
|
28
|
-
],
|
|
29
|
-
}).compile();
|
|
30
|
-
|
|
31
|
-
controller = module.get<TestAppController>(TestAppController);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
describe('getHello', () => {
|
|
35
|
-
it('should return hello with app name', () => {
|
|
36
|
-
mockConfigService.get.mockReturnValue({ appName: 'test-app' });
|
|
37
|
-
expect(controller.getHello()).toBe('Hello World');
|
|
38
|
-
expect(mockAppService.getHello).toHaveBeenCalledWith('test-app');
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should return hello with default name when conf is missing', () => {
|
|
42
|
-
mockConfigService.get.mockReturnValue(undefined);
|
|
43
|
-
expect(controller.getHello()).toBe('Hello World');
|
|
44
|
-
expect(mockAppService.getHello).toHaveBeenCalledWith('no-name');
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
describe('shutdown', () => {
|
|
49
|
-
let mockExit: jest.SpyInstance;
|
|
50
|
-
let mockLog: jest.SpyInstance;
|
|
51
|
-
|
|
52
|
-
beforeEach(() => {
|
|
53
|
-
mockExit = jest.spyOn(process, 'exit').mockImplementation((() => {}) as any);
|
|
54
|
-
mockLog = jest.spyOn(console, 'log').mockImplementation((() => {}) as any);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
afterEach(() => {
|
|
58
|
-
mockExit.mockRestore();
|
|
59
|
-
mockLog.mockRestore();
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('should shutdown in dev mode', () => {
|
|
63
|
-
mockConfigService.get.mockReturnValue({ isDev: true });
|
|
64
|
-
controller.shutdown();
|
|
65
|
-
expect(mockExit).toHaveBeenCalledWith(0);
|
|
66
|
-
expect(mockLog).toHaveBeenCalledWith('Bye bye!');
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('should shutdown in test mode', () => {
|
|
70
|
-
mockConfigService.get.mockReturnValue({ isTest: true });
|
|
71
|
-
controller.shutdown();
|
|
72
|
-
expect(mockExit).toHaveBeenCalledWith(0);
|
|
73
|
-
expect(mockLog).toHaveBeenCalledWith('Bye bye!');
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('should not shutdown if neither dev nor test', () => {
|
|
77
|
-
mockConfigService.get.mockReturnValue({ isDev: false, isTest: false });
|
|
78
|
-
controller.shutdown();
|
|
79
|
-
expect(mockExit).not.toHaveBeenCalled();
|
|
80
|
-
expect(mockLog).not.toHaveBeenCalled();
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('should not shutdown if conf is missing', () => {
|
|
84
|
-
mockConfigService.get.mockReturnValue(undefined);
|
|
85
|
-
controller.shutdown();
|
|
86
|
-
expect(mockExit).not.toHaveBeenCalled();
|
|
87
|
-
expect(mockLog).not.toHaveBeenCalled();
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
});
|
|
1
|
+
import { Test, TestingModule } from '@nestjs/testing';
|
|
2
|
+
import { BaseAppController } from '../base-app.controller.js';
|
|
3
|
+
import { BaseAppService } from '../base-app.service.js';
|
|
4
|
+
import { ConfigService } from '@nestjs/config';
|
|
5
|
+
import { Controller } from '@nestjs/common';
|
|
6
|
+
|
|
7
|
+
@Controller()
|
|
8
|
+
class TestAppController extends BaseAppController {}
|
|
9
|
+
|
|
10
|
+
describe('BaseAppController', () => {
|
|
11
|
+
let controller: TestAppController;
|
|
12
|
+
let mockAppService: any;
|
|
13
|
+
let mockConfigService: any;
|
|
14
|
+
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
mockAppService = {
|
|
17
|
+
getHello: jest.fn().mockReturnValue('Hello World'),
|
|
18
|
+
};
|
|
19
|
+
mockConfigService = {
|
|
20
|
+
get: jest.fn(),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const module: TestingModule = await Test.createTestingModule({
|
|
24
|
+
controllers: [TestAppController],
|
|
25
|
+
providers: [
|
|
26
|
+
{ provide: BaseAppService, useValue: mockAppService },
|
|
27
|
+
{ provide: ConfigService, useValue: mockConfigService },
|
|
28
|
+
],
|
|
29
|
+
}).compile();
|
|
30
|
+
|
|
31
|
+
controller = module.get<TestAppController>(TestAppController);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('getHello', () => {
|
|
35
|
+
it('should return hello with app name', () => {
|
|
36
|
+
mockConfigService.get.mockReturnValue({ appName: 'test-app' });
|
|
37
|
+
expect(controller.getHello()).toBe('Hello World');
|
|
38
|
+
expect(mockAppService.getHello).toHaveBeenCalledWith('test-app');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should return hello with default name when conf is missing', () => {
|
|
42
|
+
mockConfigService.get.mockReturnValue(undefined);
|
|
43
|
+
expect(controller.getHello()).toBe('Hello World');
|
|
44
|
+
expect(mockAppService.getHello).toHaveBeenCalledWith('no-name');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('shutdown', () => {
|
|
49
|
+
let mockExit: jest.SpyInstance;
|
|
50
|
+
let mockLog: jest.SpyInstance;
|
|
51
|
+
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
mockExit = jest.spyOn(process, 'exit').mockImplementation((() => {}) as any);
|
|
54
|
+
mockLog = jest.spyOn(console, 'log').mockImplementation((() => {}) as any);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
mockExit.mockRestore();
|
|
59
|
+
mockLog.mockRestore();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should shutdown in dev mode', () => {
|
|
63
|
+
mockConfigService.get.mockReturnValue({ isDev: true });
|
|
64
|
+
controller.shutdown();
|
|
65
|
+
expect(mockExit).toHaveBeenCalledWith(0);
|
|
66
|
+
expect(mockLog).toHaveBeenCalledWith('Bye bye!');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should shutdown in test mode', () => {
|
|
70
|
+
mockConfigService.get.mockReturnValue({ isTest: true });
|
|
71
|
+
controller.shutdown();
|
|
72
|
+
expect(mockExit).toHaveBeenCalledWith(0);
|
|
73
|
+
expect(mockLog).toHaveBeenCalledWith('Bye bye!');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should not shutdown if neither dev nor test', () => {
|
|
77
|
+
mockConfigService.get.mockReturnValue({ isDev: false, isTest: false });
|
|
78
|
+
controller.shutdown();
|
|
79
|
+
expect(mockExit).not.toHaveBeenCalled();
|
|
80
|
+
expect(mockLog).not.toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should not shutdown if conf is missing', () => {
|
|
84
|
+
mockConfigService.get.mockReturnValue(undefined);
|
|
85
|
+
controller.shutdown();
|
|
86
|
+
expect(mockExit).not.toHaveBeenCalled();
|
|
87
|
+
expect(mockLog).not.toHaveBeenCalled();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { describe, expect, it, jest } from '@jest/globals';
|
|
2
|
-
import { ExecutionContext, LoggerService } from '@nestjs/common';
|
|
3
|
-
import { BaseAppService } from '../base-app.service.js';
|
|
4
|
-
|
|
5
|
-
describe('BaseAppService', () => {
|
|
6
|
-
const logger: LoggerService = {
|
|
7
|
-
debug: jest.fn(),
|
|
8
|
-
};
|
|
9
|
-
const service = new BaseAppService(logger);
|
|
10
|
-
|
|
11
|
-
it('should format hello message with app name', () => {
|
|
12
|
-
expect(service.getHello('test-app')).toBe('Hello World from test-app!');
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it('should log handler names containing underscore and not starting with "_"', () => {
|
|
16
|
-
const ctx = {
|
|
17
|
-
getHandler: () => ({ name: 'test_handler' }),
|
|
18
|
-
} as unknown as ExecutionContext;
|
|
19
|
-
|
|
20
|
-
service.handleBeforeAllRoutes(ctx);
|
|
21
|
-
|
|
22
|
-
expect(logger.debug).toHaveBeenCalledWith('Running Handler: test_handler');
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('should ignore handlers without underscore or starting with "_"', () => {
|
|
26
|
-
(logger.debug as jest.Mock).mockClear();
|
|
27
|
-
const ctxWithoutUnderscore = {
|
|
28
|
-
getHandler: () => ({ name: 'handler' }),
|
|
29
|
-
} as unknown as ExecutionContext;
|
|
30
|
-
const ctxInternal = {
|
|
31
|
-
getHandler: () => ({ name: '_internal_handler' }),
|
|
32
|
-
} as unknown as ExecutionContext;
|
|
33
|
-
|
|
34
|
-
service.handleBeforeAllRoutes(ctxWithoutUnderscore);
|
|
35
|
-
service.handleBeforeAllRoutes(ctxInternal);
|
|
36
|
-
|
|
37
|
-
expect(logger.debug).not.toHaveBeenCalled();
|
|
38
|
-
});
|
|
39
|
-
});
|
|
1
|
+
import { describe, expect, it, jest } from '@jest/globals';
|
|
2
|
+
import { ExecutionContext, LoggerService } from '@nestjs/common';
|
|
3
|
+
import { BaseAppService } from '../base-app.service.js';
|
|
4
|
+
|
|
5
|
+
describe('BaseAppService', () => {
|
|
6
|
+
const logger: LoggerService = {
|
|
7
|
+
debug: jest.fn(),
|
|
8
|
+
};
|
|
9
|
+
const service = new BaseAppService(logger);
|
|
10
|
+
|
|
11
|
+
it('should format hello message with app name', () => {
|
|
12
|
+
expect(service.getHello('test-app')).toBe('Hello World from test-app!');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should log handler names containing underscore and not starting with "_"', () => {
|
|
16
|
+
const ctx = {
|
|
17
|
+
getHandler: () => ({ name: 'test_handler' }),
|
|
18
|
+
} as unknown as ExecutionContext;
|
|
19
|
+
|
|
20
|
+
service.handleBeforeAllRoutes(ctx);
|
|
21
|
+
|
|
22
|
+
expect(logger.debug).toHaveBeenCalledWith('Running Handler: test_handler');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should ignore handlers without underscore or starting with "_"', () => {
|
|
26
|
+
(logger.debug as jest.Mock).mockClear();
|
|
27
|
+
const ctxWithoutUnderscore = {
|
|
28
|
+
getHandler: () => ({ name: 'handler' }),
|
|
29
|
+
} as unknown as ExecutionContext;
|
|
30
|
+
const ctxInternal = {
|
|
31
|
+
getHandler: () => ({ name: '_internal_handler' }),
|
|
32
|
+
} as unknown as ExecutionContext;
|
|
33
|
+
|
|
34
|
+
service.handleBeforeAllRoutes(ctxWithoutUnderscore);
|
|
35
|
+
service.handleBeforeAllRoutes(ctxInternal);
|
|
36
|
+
|
|
37
|
+
expect(logger.debug).not.toHaveBeenCalled();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { describe, expect, it, jest } from '@jest/globals';
|
|
2
|
-
import 'reflect-metadata';
|
|
3
|
-
import {
|
|
4
|
-
YalcAlsService,
|
|
5
|
-
YalcClsModule,
|
|
6
|
-
YalcGlobalClsService,
|
|
7
|
-
} from '../cls.module.js';
|
|
8
|
-
|
|
9
|
-
describe('YalcClsModule', () => {
|
|
10
|
-
it('should expose YalcGlobalClsService in module exports', () => {
|
|
11
|
-
const exportedProviders =
|
|
12
|
-
(Reflect.getMetadata('exports', YalcClsModule) as any[]) ?? [];
|
|
13
|
-
expect(exportedProviders).toContain(YalcGlobalClsService);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('should configure a CLS module import', async () => {
|
|
17
|
-
const imports =
|
|
18
|
-
(Reflect.getMetadata('imports', YalcClsModule) as any[]) ?? [];
|
|
19
|
-
expect(imports.length).toBeGreaterThan(0);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('YalcAlsService should persist store within the async context', () => {
|
|
23
|
-
const als = new YalcAlsService();
|
|
24
|
-
const store = { placeholder: 'value' };
|
|
25
|
-
let seen;
|
|
26
|
-
|
|
27
|
-
als.run(store, () => {
|
|
28
|
-
seen = als.getStore();
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
expect(seen).toEqual(store);
|
|
32
|
-
});
|
|
33
|
-
});
|
|
1
|
+
import { describe, expect, it, jest } from '@jest/globals';
|
|
2
|
+
import 'reflect-metadata';
|
|
3
|
+
import {
|
|
4
|
+
YalcAlsService,
|
|
5
|
+
YalcClsModule,
|
|
6
|
+
YalcGlobalClsService,
|
|
7
|
+
} from '../cls.module.js';
|
|
8
|
+
|
|
9
|
+
describe('YalcClsModule', () => {
|
|
10
|
+
it('should expose YalcGlobalClsService in module exports', () => {
|
|
11
|
+
const exportedProviders =
|
|
12
|
+
(Reflect.getMetadata('exports', YalcClsModule) as any[]) ?? [];
|
|
13
|
+
expect(exportedProviders).toContain(YalcGlobalClsService);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should configure a CLS module import', async () => {
|
|
17
|
+
const imports =
|
|
18
|
+
(Reflect.getMetadata('imports', YalcClsModule) as any[]) ?? [];
|
|
19
|
+
expect(imports.length).toBeGreaterThan(0);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('YalcAlsService should persist store within the async context', () => {
|
|
23
|
+
const als = new YalcAlsService();
|
|
24
|
+
const store = { placeholder: 'value' };
|
|
25
|
+
let seen;
|
|
26
|
+
|
|
27
|
+
als.run(store, () => {
|
|
28
|
+
seen = als.getStore();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
expect(seen).toEqual(store);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -1,48 +1,57 @@
|
|
|
1
|
-
import { describe, expect, it, jest } from '@jest/globals';
|
|
2
|
-
import { LifeCycleHandler } from '../life-cycle-handler.service.js';
|
|
3
|
-
import { AppContextService } from '../app-context.service.js';
|
|
4
|
-
import { createMock } from '@golevelup/ts-jest';
|
|
5
|
-
|
|
6
|
-
describe('LifeCycleHandler', () => {
|
|
7
|
-
let loggerService: any = { debug: jest.fn() };
|
|
8
|
-
let lifeCycleHandler: LifeCycleHandler;
|
|
9
|
-
let appContextService: AppContextService = createMock<AppContextService>({
|
|
10
|
-
initializedApps: new Set(),
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('should initialize app', () => {
|
|
14
|
-
lifeCycleHandler = new LifeCycleHandler(
|
|
15
|
-
loggerService,
|
|
16
|
-
'TestApp',
|
|
17
|
-
appContextService,
|
|
18
|
-
);
|
|
19
|
-
expect(loggerService.debug).toHaveBeenCalledWith(
|
|
20
|
-
'====================== Init TestApp ======================',
|
|
21
|
-
);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('should not initialize the same app twice', () => {
|
|
25
|
-
expect(
|
|
26
|
-
() => new LifeCycleHandler(loggerService, 'TestApp', appContextService),
|
|
27
|
-
).toThrow('Cannot initialize the same app (TestApp) twice');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should initialize the same app twice if skipDuplicateAppCheck is true', () => {
|
|
31
|
-
new LifeCycleHandler(loggerService, 'TestApp', appContextService, {
|
|
32
|
-
skipDuplicateAppCheck: true,
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
});
|
|
1
|
+
import { describe, expect, it, jest } from '@jest/globals';
|
|
2
|
+
import { LifeCycleHandler } from '../life-cycle-handler.service.js';
|
|
3
|
+
import { AppContextService } from '../app-context.service.js';
|
|
4
|
+
import { createMock } from '@golevelup/ts-jest';
|
|
5
|
+
|
|
6
|
+
describe('LifeCycleHandler', () => {
|
|
7
|
+
let loggerService: any = { debug: jest.fn() };
|
|
8
|
+
let lifeCycleHandler: LifeCycleHandler;
|
|
9
|
+
let appContextService: AppContextService = createMock<AppContextService>({
|
|
10
|
+
initializedApps: new Set(),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should initialize app', () => {
|
|
14
|
+
lifeCycleHandler = new LifeCycleHandler(
|
|
15
|
+
loggerService,
|
|
16
|
+
'TestApp',
|
|
17
|
+
appContextService,
|
|
18
|
+
);
|
|
19
|
+
expect(loggerService.debug).toHaveBeenCalledWith(
|
|
20
|
+
'====================== Init TestApp ======================',
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should not initialize the same app twice', () => {
|
|
25
|
+
expect(
|
|
26
|
+
() => new LifeCycleHandler(loggerService, 'TestApp', appContextService),
|
|
27
|
+
).toThrow('Cannot initialize the same app (TestApp) twice');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should initialize the same app twice if skipDuplicateAppCheck is true', () => {
|
|
31
|
+
new LifeCycleHandler(loggerService, 'TestApp', appContextService, {
|
|
32
|
+
skipDuplicateAppCheck: true,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
expect(
|
|
36
|
+
() =>
|
|
37
|
+
new LifeCycleHandler(loggerService, 'TestApp', appContextService, {
|
|
38
|
+
skipDuplicateAppCheck: true,
|
|
39
|
+
}),
|
|
40
|
+
).not.toThrow();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should handle module destroy', () => {
|
|
44
|
+
lifeCycleHandler.onModuleDestroy();
|
|
45
|
+
expect(loggerService.debug).toHaveBeenCalledWith(
|
|
46
|
+
'====================== Close TestApp ======================',
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should have onModuleInit', () => {
|
|
51
|
+
const loggerMock = { debug: jest.fn() } as any;
|
|
52
|
+
const appContextServiceMock = { initializedApps: new Set() } as any;
|
|
53
|
+
const handler = new LifeCycleHandler(loggerMock, 'test', appContextServiceMock);
|
|
54
|
+
handler.onModuleInit();
|
|
55
|
+
expect(handler).toBeDefined();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import { describe, expect, it } from '@jest/globals';
|
|
2
|
-
import { CallHandler, ExecutionContext } from '@nestjs/common';
|
|
3
|
-
import { firstValueFrom, of } from 'rxjs';
|
|
4
|
-
import { UnwrapResultInterceptor } from '../unwrap-result.interceptor.js';
|
|
5
|
-
|
|
6
|
-
const ctx = {
|
|
7
|
-
switchToHttp: () => ({
|
|
8
|
-
getRequest: () => ({}),
|
|
9
|
-
}),
|
|
10
|
-
} as unknown as ExecutionContext;
|
|
11
|
-
|
|
12
|
-
describe('UnwrapResultInterceptor', () => {
|
|
13
|
-
const interceptor = new UnwrapResultInterceptor();
|
|
14
|
-
|
|
15
|
-
it('should unwrap successful Result-like objects', async () => {
|
|
16
|
-
const next: CallHandler = {
|
|
17
|
-
handle: () =>
|
|
18
|
-
of({
|
|
19
|
-
isOk: () => true,
|
|
20
|
-
isErr: () => false,
|
|
21
|
-
value: 'payload',
|
|
22
|
-
}),
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const result = await firstValueFrom(interceptor.intercept(ctx, next));
|
|
26
|
-
expect(result).toBe('payload');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should throw when Result-like object contains error', async () => {
|
|
30
|
-
const error = new Error('boom');
|
|
31
|
-
const next: CallHandler = {
|
|
32
|
-
handle: () =>
|
|
33
|
-
of({
|
|
34
|
-
isOk: () => false,
|
|
35
|
-
isErr: () => true,
|
|
36
|
-
error,
|
|
37
|
-
}),
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
await expect(firstValueFrom(interceptor.intercept(ctx, next))).rejects.toThrow('boom');
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should pass through non Result objects', async () => {
|
|
44
|
-
const payload = { ok: true };
|
|
45
|
-
const next: CallHandler = {
|
|
46
|
-
handle: () => of(payload),
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const result = await firstValueFrom(interceptor.intercept(ctx, next));
|
|
50
|
-
expect(result).toBe(payload);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
1
|
+
import { describe, expect, it } from '@jest/globals';
|
|
2
|
+
import { CallHandler, ExecutionContext } from '@nestjs/common';
|
|
3
|
+
import { firstValueFrom, of } from 'rxjs';
|
|
4
|
+
import { UnwrapResultInterceptor } from '../unwrap-result.interceptor.js';
|
|
5
|
+
|
|
6
|
+
const ctx = {
|
|
7
|
+
switchToHttp: () => ({
|
|
8
|
+
getRequest: () => ({}),
|
|
9
|
+
}),
|
|
10
|
+
} as unknown as ExecutionContext;
|
|
11
|
+
|
|
12
|
+
describe('UnwrapResultInterceptor', () => {
|
|
13
|
+
const interceptor = new UnwrapResultInterceptor();
|
|
14
|
+
|
|
15
|
+
it('should unwrap successful Result-like objects', async () => {
|
|
16
|
+
const next: CallHandler = {
|
|
17
|
+
handle: () =>
|
|
18
|
+
of({
|
|
19
|
+
isOk: () => true,
|
|
20
|
+
isErr: () => false,
|
|
21
|
+
value: 'payload',
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const result = await firstValueFrom(interceptor.intercept(ctx, next));
|
|
26
|
+
expect(result).toBe('payload');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should throw when Result-like object contains error', async () => {
|
|
30
|
+
const error = new Error('boom');
|
|
31
|
+
const next: CallHandler = {
|
|
32
|
+
handle: () =>
|
|
33
|
+
of({
|
|
34
|
+
isOk: () => false,
|
|
35
|
+
isErr: () => true,
|
|
36
|
+
error,
|
|
37
|
+
}),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
await expect(firstValueFrom(interceptor.intercept(ctx, next))).rejects.toThrow('boom');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should pass through non Result objects', async () => {
|
|
44
|
+
const payload = { ok: true };
|
|
45
|
+
const next: CallHandler = {
|
|
46
|
+
handle: () => of(payload),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const result = await firstValueFrom(interceptor.intercept(ctx, next));
|
|
50
|
+
expect(result).toBe(payload);
|
|
51
|
+
});
|
|
52
|
+
});
|