@navios/core 1.0.0 → 2.0.0-alpha.0
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/CHANGELOG.md +97 -11
- package/README.md +67 -220
- package/lib/async-context-6pdlnNLA.d.mts +237 -0
- package/lib/async-context-6pdlnNLA.d.mts.map +1 -0
- package/lib/async-context-Dqk7pE-D.d.cts +237 -0
- package/lib/async-context-Dqk7pE-D.d.cts.map +1 -0
- package/lib/attribute-factory-B3VuYnp7.d.cts +70 -0
- package/lib/attribute-factory-B3VuYnp7.d.cts.map +1 -0
- package/lib/attribute-factory-mKOXoVnV.d.mts +70 -0
- package/lib/attribute-factory-mKOXoVnV.d.mts.map +1 -0
- package/lib/attribute-value-CjX9ZkLq.cjs +172 -0
- package/lib/attribute-value-CjX9ZkLq.cjs.map +1 -0
- package/lib/attribute-value-DMEA255r.mjs +119 -0
- package/lib/attribute-value-DMEA255r.mjs.map +1 -0
- package/lib/basic-execution-context-CeGuhMOz.mjs +246 -0
- package/lib/basic-execution-context-CeGuhMOz.mjs.map +1 -0
- package/lib/basic-execution-context-DSk5MNtc.cjs +281 -0
- package/lib/basic-execution-context-DSk5MNtc.cjs.map +1 -0
- package/lib/execution-context-BPD3FeFb.d.mts +71 -0
- package/lib/execution-context-BPD3FeFb.d.mts.map +1 -0
- package/lib/execution-context-BTJdHhbf.d.cts +71 -0
- package/lib/execution-context-BTJdHhbf.d.cts.map +1 -0
- package/lib/execution-context-token-BXUKB2hf.cjs +112 -0
- package/lib/execution-context-token-BXUKB2hf.cjs.map +1 -0
- package/lib/execution-context-token-CrXmTvk2.mjs +77 -0
- package/lib/execution-context-token-CrXmTvk2.mjs.map +1 -0
- package/lib/index.cjs +1687 -4409
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +838 -3
- package/lib/index.d.cts.map +1 -0
- package/lib/index.d.mts +838 -3
- package/lib/index.d.mts.map +1 -0
- package/lib/index.mjs +1573 -4219
- package/lib/index.mjs.map +1 -1
- package/lib/legacy-compat/index.cjs +72 -371
- package/lib/legacy-compat/index.cjs.map +1 -1
- package/lib/legacy-compat/index.d.cts +42 -382
- package/lib/legacy-compat/index.d.cts.map +1 -1
- package/lib/legacy-compat/index.d.mts +41 -382
- package/lib/legacy-compat/index.d.mts.map +1 -1
- package/lib/legacy-compat/index.mjs +51 -345
- package/lib/legacy-compat/index.mjs.map +1 -1
- package/lib/module-Bha7GER_.mjs +135 -0
- package/lib/module-Bha7GER_.mjs.map +1 -0
- package/lib/module-DZewk_2r.cjs +164 -0
- package/lib/module-DZewk_2r.cjs.map +1 -0
- package/lib/module-graph-DCP9MHG0.cjs +147 -0
- package/lib/module-graph-DCP9MHG0.cjs.map +1 -0
- package/lib/module-graph-xvVEykFM.mjs +142 -0
- package/lib/module-graph-xvVEykFM.mjs.map +1 -0
- package/lib/module-metadata-C8S92seK.cjs +95 -0
- package/lib/module-metadata-C8S92seK.cjs.map +1 -0
- package/lib/module-metadata-k3RTt20x.mjs +66 -0
- package/lib/module-metadata-k3RTt20x.mjs.map +1 -0
- package/lib/module-options-Dmse0gii.d.mts +51 -0
- package/lib/module-options-Dmse0gii.d.mts.map +1 -0
- package/lib/module-options-IuDO-p8u.d.cts +51 -0
- package/lib/module-options-IuDO-p8u.d.cts.map +1 -0
- package/lib/testing/index.cjs +96 -414
- package/lib/testing/index.cjs.map +1 -1
- package/lib/testing/index.d.cts +25 -385
- package/lib/testing/index.d.cts.map +1 -1
- package/lib/testing/index.d.mts +24 -385
- package/lib/testing/index.d.mts.map +1 -1
- package/lib/testing/index.mjs +94 -408
- package/lib/testing/index.mjs.map +1 -1
- package/lib/transport.cjs +25 -0
- package/lib/transport.d.cts +190 -0
- package/lib/transport.d.cts.map +1 -0
- package/lib/transport.d.mts +190 -0
- package/lib/transport.d.mts.map +1 -0
- package/lib/transport.mjs +7 -0
- package/package.json +37 -17
- package/docs/README.md +0 -316
- package/docs/adapters.md +0 -308
- package/docs/application-setup.md +0 -524
- package/docs/attributes.md +0 -689
- package/docs/controllers.md +0 -373
- package/docs/endpoints.md +0 -444
- package/docs/exceptions.md +0 -352
- package/docs/guards.md +0 -550
- package/docs/legacy-compat.md +0 -356
- package/docs/modules.md +0 -377
- package/docs/quick-start.md +0 -290
- package/docs/services.md +0 -427
- package/docs/testing.md +0 -827
- package/lib/index-C0Sg16Eb.d.cts +0 -3365
- package/lib/index-C0Sg16Eb.d.cts.map +0 -1
- package/lib/index-DySQ6Dpd.d.mts +0 -3365
- package/lib/index-DySQ6Dpd.d.mts.map +0 -1
- package/lib/navios.factory-CO5MB_OK.cjs +0 -4125
- package/lib/navios.factory-CO5MB_OK.cjs.map +0 -1
- package/lib/navios.factory-D6Y94P9B.mjs +0 -3828
- package/lib/navios.factory-D6Y94P9B.mjs.map +0 -1
- package/lib/tokens-4J9sredA.mjs +0 -100
- package/lib/tokens-4J9sredA.mjs.map +0 -1
- package/lib/tokens-CWw9kyeD.cjs +0 -196
- package/lib/tokens-CWw9kyeD.cjs.map +0 -1
- package/lib/use-guards.decorator-BecoQSmE.mjs +0 -339
- package/lib/use-guards.decorator-BecoQSmE.mjs.map +0 -1
- package/lib/use-guards.decorator-C4ml9XaT.cjs +0 -447
- package/lib/use-guards.decorator-C4ml9XaT.cjs.map +0 -1
- package/project.json +0 -66
- package/src/__tests__/attribute.factory.spec.mts +0 -300
- package/src/__tests__/config.service.spec.mts +0 -43
- package/src/__tests__/console-logger.service.spec.mts +0 -312
- package/src/__tests__/controller-resolver.spec.mts +0 -229
- package/src/__tests__/controller.spec.mts +0 -58
- package/src/__tests__/guard-runner.service.spec.mts +0 -399
- package/src/__tests__/logger.service.spec.mts +0 -147
- package/src/__tests__/responders.spec.mts +0 -340
- package/src/__tests__/testing-module.spec.mts +0 -459
- package/src/__tests__/unit-testing-module.spec.mts +0 -424
- package/src/attribute.factory.mts +0 -325
- package/src/config/config-service.interface.mts +0 -47
- package/src/config/config.provider.mts +0 -69
- package/src/config/config.service.mts +0 -176
- package/src/config/index.mts +0 -5
- package/src/config/types.mts +0 -58
- package/src/config/utils/helpers.mts +0 -23
- package/src/config/utils/index.mts +0 -1
- package/src/decorators/controller.decorator.mts +0 -81
- package/src/decorators/endpoint.decorator.mts +0 -181
- package/src/decorators/header.decorator.mts +0 -38
- package/src/decorators/http-code.decorator.mts +0 -38
- package/src/decorators/index.mts +0 -8
- package/src/decorators/module.decorator.mts +0 -106
- package/src/decorators/multipart.decorator.mts +0 -169
- package/src/decorators/stream.decorator.mts +0 -155
- package/src/decorators/use-guards.decorator.mts +0 -73
- package/src/exceptions/bad-request.exception.mts +0 -28
- package/src/exceptions/conflict.exception.mts +0 -31
- package/src/exceptions/forbidden.exception.mts +0 -30
- package/src/exceptions/http.exception.mts +0 -33
- package/src/exceptions/index.mts +0 -7
- package/src/exceptions/internal-server-error.exception.mts +0 -33
- package/src/exceptions/not-found.exception.mts +0 -33
- package/src/exceptions/unauthorized.exception.mts +0 -30
- package/src/factories/adapter.factory.mts +0 -20
- package/src/factories/endpoint-adapter.factory.mts +0 -21
- package/src/factories/http-adapter.factory.mts +0 -20
- package/src/factories/index.mts +0 -8
- package/src/factories/multipart-adapter.factory.mts +0 -21
- package/src/factories/reply.factory.mts +0 -21
- package/src/factories/request.factory.mts +0 -21
- package/src/factories/stream-adapter.factory.mts +0 -20
- package/src/factories/xml-stream-adapter.factory.mts +0 -20
- package/src/index.mts +0 -16
- package/src/interfaces/abstract-adapter.interface.mts +0 -32
- package/src/interfaces/abstract-execution-context.inteface.mts +0 -48
- package/src/interfaces/abstract-http-adapter.interface.mts +0 -79
- package/src/interfaces/abstract-http-cors-options.interface.mts +0 -59
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +0 -127
- package/src/interfaces/abstract-http-listen-options.interface.mts +0 -4
- package/src/interfaces/adapter-environment.interface.mts +0 -74
- package/src/interfaces/can-activate.mts +0 -38
- package/src/interfaces/http-header.mts +0 -18
- package/src/interfaces/index.mts +0 -11
- package/src/interfaces/navios-module.mts +0 -28
- package/src/interfaces/plugin.interface.mts +0 -139
- package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +0 -521
- package/src/legacy-compat/__type-tests__/tsconfig.json +0 -15
- package/src/legacy-compat/attribute.factory.mts +0 -351
- package/src/legacy-compat/decorators/controller.decorator.mts +0 -31
- package/src/legacy-compat/decorators/endpoint.decorator.mts +0 -73
- package/src/legacy-compat/decorators/header.decorator.mts +0 -43
- package/src/legacy-compat/decorators/http-code.decorator.mts +0 -39
- package/src/legacy-compat/decorators/index.mts +0 -11
- package/src/legacy-compat/decorators/module.decorator.mts +0 -37
- package/src/legacy-compat/decorators/multipart.decorator.mts +0 -72
- package/src/legacy-compat/decorators/stream.decorator.mts +0 -89
- package/src/legacy-compat/decorators/use-guards.decorator.mts +0 -80
- package/src/legacy-compat/index.mts +0 -57
- package/src/logger/README.md +0 -3
- package/src/logger/console-logger.service.mts +0 -714
- package/src/logger/index.mts +0 -6
- package/src/logger/log-levels.mts +0 -21
- package/src/logger/logger-service.interface.mts +0 -42
- package/src/logger/logger.service.mts +0 -113
- package/src/logger/logger.tokens.mts +0 -44
- package/src/logger/utils/cli-colors.util.mts +0 -17
- package/src/logger/utils/filter-log-levelts.util.mts +0 -29
- package/src/logger/utils/index.mts +0 -5
- package/src/logger/utils/is-log-level-enabled.mts +0 -33
- package/src/logger/utils/is-log-level.util.mts +0 -10
- package/src/logger/utils/shared.utils.mts +0 -51
- package/src/metadata/controller.metadata.mts +0 -72
- package/src/metadata/handler.metadata.mts +0 -80
- package/src/metadata/index.mts +0 -4
- package/src/metadata/module.metadata.mts +0 -108
- package/src/metadata/navios-managed.metadata.mts +0 -42
- package/src/navios.application.mts +0 -484
- package/src/navios.environment.mts +0 -42
- package/src/navios.factory.mts +0 -158
- package/src/responders/enums/framework-error.enum.mts +0 -38
- package/src/responders/enums/index.mts +0 -1
- package/src/responders/index.mts +0 -4
- package/src/responders/interfaces/error-responder.interface.mts +0 -42
- package/src/responders/interfaces/error-response.interface.mts +0 -22
- package/src/responders/interfaces/index.mts +0 -3
- package/src/responders/interfaces/problem-details.interface.mts +0 -34
- package/src/responders/services/error-response-producer.service.mts +0 -143
- package/src/responders/services/forbidden-responder.service.mts +0 -77
- package/src/responders/services/index.mts +0 -5
- package/src/responders/services/internal-server-error-responder.service.mts +0 -57
- package/src/responders/services/not-found-responder.service.mts +0 -76
- package/src/responders/services/validation-error-responder.service.mts +0 -78
- package/src/responders/tokens/index.mts +0 -1
- package/src/responders/tokens/responder.tokens.mts +0 -84
- package/src/services/abstract-handler-adapter.service.mts +0 -366
- package/src/services/guard-runner.service.mts +0 -152
- package/src/services/index.mts +0 -4
- package/src/services/instance-resolver.service.mts +0 -177
- package/src/services/module-loader.service.mts +0 -252
- package/src/stores/index.mts +0 -1
- package/src/stores/request-id.store.mts +0 -85
- package/src/testing/index.mts +0 -3
- package/src/testing/testing-module.mts +0 -393
- package/src/testing/unit-testing-module.mts +0 -298
- package/src/tokens/adapter.token.mts +0 -6
- package/src/tokens/endpoint-adapter.token.mts +0 -8
- package/src/tokens/execution-context.token.mts +0 -9
- package/src/tokens/http-adapter.token.mts +0 -8
- package/src/tokens/index.mts +0 -10
- package/src/tokens/multipart-adapter.token.mts +0 -8
- package/src/tokens/navios-options.token.mts +0 -6
- package/src/tokens/reply.token.mts +0 -3
- package/src/tokens/request.token.mts +0 -3
- package/src/tokens/stream-adapter.token.mts +0 -8
- package/src/tokens/xml-stream-adapter.token.mts +0 -8
- package/src/utils/adapter-supports.util.mts +0 -47
- package/src/utils/index.mts +0 -1
- package/tsconfig.json +0 -21
- package/tsconfig.lib.json +0 -8
- package/tsconfig.spec.json +0 -15
- package/tsdown.config.mts +0 -35
- package/vitest.config.mts +0 -15
|
@@ -1,393 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ClassType,
|
|
3
|
-
ClassTypeWithInstance,
|
|
4
|
-
InjectionToken,
|
|
5
|
-
ScopedContainer,
|
|
6
|
-
} from '@navios/di'
|
|
7
|
-
|
|
8
|
-
import { TestContainer } from '@navios/di/testing'
|
|
9
|
-
|
|
10
|
-
import type { NaviosModule } from '../interfaces/index.mjs'
|
|
11
|
-
import type { NaviosApplicationOptions } from '../navios.application.mjs'
|
|
12
|
-
|
|
13
|
-
import { NaviosApplication } from '../navios.application.mjs'
|
|
14
|
-
import { NaviosFactory } from '../navios.factory.mjs'
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Configuration for overriding a provider in the testing module.
|
|
18
|
-
*
|
|
19
|
-
* @typeParam T - The type of the provider being overridden
|
|
20
|
-
*/
|
|
21
|
-
export interface TestingModuleOverride<T = any> {
|
|
22
|
-
/**
|
|
23
|
-
* The injection token or class to override.
|
|
24
|
-
*/
|
|
25
|
-
token: ClassType | InjectionToken<T, any>
|
|
26
|
-
/**
|
|
27
|
-
* Value to use instead of the original provider.
|
|
28
|
-
*/
|
|
29
|
-
useValue?: T
|
|
30
|
-
/**
|
|
31
|
-
* Class to use instead of the original provider.
|
|
32
|
-
*/
|
|
33
|
-
useClass?: ClassType
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Options for creating a testing module.
|
|
38
|
-
*
|
|
39
|
-
* Extends NaviosApplicationOptions but excludes the container option,
|
|
40
|
-
* as TestingModule manages its own TestContainer.
|
|
41
|
-
*/
|
|
42
|
-
export interface TestingModuleOptions extends Omit<
|
|
43
|
-
NaviosApplicationOptions,
|
|
44
|
-
'container'
|
|
45
|
-
> {
|
|
46
|
-
/**
|
|
47
|
-
* Initial provider overrides to apply when creating the testing module.
|
|
48
|
-
*
|
|
49
|
-
* You can also use `overrideProvider()` method for a fluent API.
|
|
50
|
-
*/
|
|
51
|
-
overrides?: TestingModuleOverride[]
|
|
52
|
-
/**
|
|
53
|
-
* Container to use for the testing module.
|
|
54
|
-
* If not provided, a new TestContainer will be created.
|
|
55
|
-
*/
|
|
56
|
-
container?: TestContainer
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* A testing-optimized wrapper around NaviosApplication.
|
|
61
|
-
* Provides utilities for setting up test environments with mock dependencies.
|
|
62
|
-
*
|
|
63
|
-
* When `init()` is called, a request scope is automatically started.
|
|
64
|
-
* This means `get()` calls will resolve request-scoped services correctly,
|
|
65
|
-
* simulating a real HTTP request context.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* const module = await TestingModule.create(AppModule)
|
|
70
|
-
* .overrideProvider(DatabaseService)
|
|
71
|
-
* .useValue(mockDatabase)
|
|
72
|
-
* .init()
|
|
73
|
-
*
|
|
74
|
-
* const userService = await module.get(UserService)
|
|
75
|
-
* // ... run tests ...
|
|
76
|
-
*
|
|
77
|
-
* await module.close()
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
export class TestingModule {
|
|
81
|
-
private app: NaviosApplication | null = null
|
|
82
|
-
private scopedContainer: ScopedContainer | null = null
|
|
83
|
-
private requestId = `test-request-${Date.now()}-${Math.random().toString(36).slice(2)}`
|
|
84
|
-
|
|
85
|
-
private constructor(
|
|
86
|
-
private readonly appModule: ClassTypeWithInstance<NaviosModule>,
|
|
87
|
-
private readonly container: TestContainer,
|
|
88
|
-
private readonly options: TestingModuleOptions,
|
|
89
|
-
) {}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Creates a new TestingModule for the given app module.
|
|
93
|
-
* This is the main entry point for setting up integration tests.
|
|
94
|
-
*
|
|
95
|
-
* @example
|
|
96
|
-
* ```typescript
|
|
97
|
-
* const module = await TestingModule.create(AppModule)
|
|
98
|
-
* .overrideProvider(DatabaseService)
|
|
99
|
-
* .useValue(mockDatabase)
|
|
100
|
-
* .init()
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
103
|
-
static create(
|
|
104
|
-
appModule: ClassTypeWithInstance<NaviosModule>,
|
|
105
|
-
options: TestingModuleOptions = { adapter: [] },
|
|
106
|
-
): TestingModule {
|
|
107
|
-
const container =
|
|
108
|
-
options.container ??
|
|
109
|
-
new TestContainer({
|
|
110
|
-
parentRegistry: options.registry,
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
// Apply initial overrides if provided
|
|
114
|
-
if (options.overrides) {
|
|
115
|
-
for (const override of options.overrides) {
|
|
116
|
-
if (override.useValue !== undefined) {
|
|
117
|
-
container.bind(override.token as any).toValue(override.useValue)
|
|
118
|
-
} else if (override.useClass) {
|
|
119
|
-
container.bind(override.token as any).toClass(override.useClass)
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return new TestingModule(appModule, container, options)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Compiles the testing module without initializing it.
|
|
129
|
-
* Call this if you need to access the app before initialization.
|
|
130
|
-
*
|
|
131
|
-
* @returns this for chaining
|
|
132
|
-
*/
|
|
133
|
-
async compile(): Promise<this> {
|
|
134
|
-
if (!this.app) {
|
|
135
|
-
this.app = await NaviosFactory.create(this.appModule, {
|
|
136
|
-
...this.options,
|
|
137
|
-
container: this.container,
|
|
138
|
-
})
|
|
139
|
-
}
|
|
140
|
-
return this
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Initializes the application and starts a request scope.
|
|
145
|
-
*
|
|
146
|
-
* This is equivalent to calling `compile()` followed by `app.init()`,
|
|
147
|
-
* plus starting a request context for proper request-scoped service resolution.
|
|
148
|
-
*
|
|
149
|
-
* @returns this for chaining
|
|
150
|
-
*/
|
|
151
|
-
async init(): Promise<this> {
|
|
152
|
-
if (!this.app) {
|
|
153
|
-
await this.compile()
|
|
154
|
-
}
|
|
155
|
-
await this.app!.init()
|
|
156
|
-
|
|
157
|
-
// Begin a request scope so get() can resolve request-scoped services
|
|
158
|
-
this.scopedContainer = this.container.beginRequest(this.requestId, {
|
|
159
|
-
testingModule: true,
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
return this
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Gets the compiled application.
|
|
167
|
-
*
|
|
168
|
-
* @throws Error if the module has not been compiled yet
|
|
169
|
-
*/
|
|
170
|
-
getApp(): NaviosApplication {
|
|
171
|
-
if (!this.app) {
|
|
172
|
-
throw new Error(
|
|
173
|
-
'TestingModule not compiled. Call compile() or init() first.',
|
|
174
|
-
)
|
|
175
|
-
}
|
|
176
|
-
return this.app
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Gets the underlying TestContainer for direct manipulation.
|
|
181
|
-
*/
|
|
182
|
-
getContainer(): TestContainer {
|
|
183
|
-
return this.container
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Gets the scoped container for the current test request.
|
|
188
|
-
* Only available after calling `init()`.
|
|
189
|
-
*
|
|
190
|
-
* @throws Error if init() has not been called
|
|
191
|
-
*/
|
|
192
|
-
getScopedContainer(): ScopedContainer {
|
|
193
|
-
if (!this.scopedContainer) {
|
|
194
|
-
throw new Error('No scoped container available. Call init() first.')
|
|
195
|
-
}
|
|
196
|
-
return this.scopedContainer
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Override a provider with a mock value or class.
|
|
201
|
-
* Must be called before `compile()` or `init()`.
|
|
202
|
-
*/
|
|
203
|
-
overrideProvider<T>(token: ClassType | InjectionToken<T, any>): {
|
|
204
|
-
useValue: (value: T) => TestingModule
|
|
205
|
-
useClass: (target: ClassType) => TestingModule
|
|
206
|
-
} {
|
|
207
|
-
return {
|
|
208
|
-
useValue: (value: T) => {
|
|
209
|
-
this.container.bind(token as any).toValue(value)
|
|
210
|
-
return this
|
|
211
|
-
},
|
|
212
|
-
useClass: (target: ClassType) => {
|
|
213
|
-
this.container.bind(token as any).toClass(target)
|
|
214
|
-
return this
|
|
215
|
-
},
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Gets an instance from the container.
|
|
221
|
-
*
|
|
222
|
-
* If `init()` has been called, this uses the scoped container
|
|
223
|
-
* which properly resolves request-scoped services.
|
|
224
|
-
*
|
|
225
|
-
* If only `compile()` was called, this uses the root container
|
|
226
|
-
* and request-scoped services will throw.
|
|
227
|
-
*/
|
|
228
|
-
async get<T>(
|
|
229
|
-
token: ClassTypeWithInstance<T> | InjectionToken<T, any>,
|
|
230
|
-
): Promise<T> {
|
|
231
|
-
// Use scoped container if available (after init)
|
|
232
|
-
if (this.scopedContainer) {
|
|
233
|
-
return this.scopedContainer.get(token as any)
|
|
234
|
-
}
|
|
235
|
-
// Fall back to root container (after compile only)
|
|
236
|
-
return this.container.get(token as any)
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Disposes the testing module and cleans up all resources.
|
|
241
|
-
*
|
|
242
|
-
* This will:
|
|
243
|
-
* 1. End the request scope (if started)
|
|
244
|
-
* 2. Close the application (if initialized)
|
|
245
|
-
* 3. Dispose the container
|
|
246
|
-
*/
|
|
247
|
-
async close(): Promise<void> {
|
|
248
|
-
// End the request scope first
|
|
249
|
-
if (this.scopedContainer) {
|
|
250
|
-
await this.scopedContainer.endRequest()
|
|
251
|
-
this.scopedContainer = null
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// Close the app
|
|
255
|
-
if (this.app) {
|
|
256
|
-
await this.app.close()
|
|
257
|
-
this.app = null
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// Dispose the container
|
|
261
|
-
await this.container.dispose()
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// ===========================================================================
|
|
265
|
-
// ASSERTION HELPERS (delegated to TestContainer)
|
|
266
|
-
// ===========================================================================
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Asserts that a service has been resolved at least once.
|
|
270
|
-
*/
|
|
271
|
-
expectResolved(token: ClassType | InjectionToken<any, any>): void {
|
|
272
|
-
this.container.expectResolved(token)
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Asserts that a service has NOT been resolved.
|
|
277
|
-
*/
|
|
278
|
-
expectNotResolved(token: ClassType | InjectionToken<any, any>): void {
|
|
279
|
-
this.container.expectNotResolved(token)
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Asserts that a service is registered as singleton scope.
|
|
284
|
-
*/
|
|
285
|
-
expectSingleton(token: ClassType | InjectionToken<any, any>): void {
|
|
286
|
-
this.container.expectSingleton(token)
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Asserts that a service is registered as transient scope.
|
|
291
|
-
*/
|
|
292
|
-
expectTransient(token: ClassType | InjectionToken<any, any>): void {
|
|
293
|
-
this.container.expectTransient(token)
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Asserts that a service is registered as request scope.
|
|
298
|
-
*/
|
|
299
|
-
expectRequestScoped(token: ClassType | InjectionToken<any, any>): void {
|
|
300
|
-
this.container.expectRequestScoped(token)
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Asserts that a method was called on a service.
|
|
305
|
-
* Note: You must use `recordMethodCall()` in your mocks for this to work.
|
|
306
|
-
*/
|
|
307
|
-
expectCalled(
|
|
308
|
-
token: ClassType | InjectionToken<any, any>,
|
|
309
|
-
method: string,
|
|
310
|
-
): void {
|
|
311
|
-
this.container.expectCalled(token, method)
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Asserts that a method was called with specific arguments.
|
|
316
|
-
* Note: You must use `recordMethodCall()` in your mocks for this to work.
|
|
317
|
-
*/
|
|
318
|
-
expectCalledWith(
|
|
319
|
-
token: ClassType | InjectionToken<any, any>,
|
|
320
|
-
method: string,
|
|
321
|
-
expectedArgs: unknown[],
|
|
322
|
-
): void {
|
|
323
|
-
this.container.expectCalledWith(token, method, expectedArgs)
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* Asserts that a method was called a specific number of times.
|
|
328
|
-
* Note: You must use `recordMethodCall()` in your mocks for this to work.
|
|
329
|
-
*/
|
|
330
|
-
expectCallCount(
|
|
331
|
-
token: ClassType | InjectionToken<any, any>,
|
|
332
|
-
method: string,
|
|
333
|
-
count: number,
|
|
334
|
-
): void {
|
|
335
|
-
this.container.expectCallCount(token, method, count)
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Records a method call for tracking.
|
|
340
|
-
* Call this from your mock implementations to enable call assertions.
|
|
341
|
-
*/
|
|
342
|
-
recordMethodCall(
|
|
343
|
-
token: ClassType | InjectionToken<any, any>,
|
|
344
|
-
method: string,
|
|
345
|
-
args: unknown[],
|
|
346
|
-
result?: unknown,
|
|
347
|
-
error?: Error,
|
|
348
|
-
): void {
|
|
349
|
-
this.container.recordMethodCall(token, method, args, result, error)
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Gets all recorded method calls for a service.
|
|
354
|
-
*/
|
|
355
|
-
getMethodCalls(token: ClassType | InjectionToken<any, any>) {
|
|
356
|
-
return this.container.getMethodCalls(token)
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* Gets the dependency graph for debugging or snapshot testing.
|
|
361
|
-
*/
|
|
362
|
-
getDependencyGraph() {
|
|
363
|
-
return this.container.getDependencyGraph()
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Gets a simplified dependency graph showing only token relationships.
|
|
368
|
-
*/
|
|
369
|
-
getSimplifiedDependencyGraph() {
|
|
370
|
-
return this.container.getSimplifiedDependencyGraph()
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Creates a testing module for the given app module.
|
|
376
|
-
*
|
|
377
|
-
* @deprecated Use `TestingModule.create()` instead.
|
|
378
|
-
*
|
|
379
|
-
* @example
|
|
380
|
-
* ```typescript
|
|
381
|
-
* // Old way (deprecated)
|
|
382
|
-
* const module = createTestingModule(AppModule)
|
|
383
|
-
*
|
|
384
|
-
* // New way
|
|
385
|
-
* const module = TestingModule.create(AppModule)
|
|
386
|
-
* ```
|
|
387
|
-
*/
|
|
388
|
-
export function createTestingModule(
|
|
389
|
-
appModule: ClassTypeWithInstance<NaviosModule>,
|
|
390
|
-
options: TestingModuleOptions = { adapter: [] },
|
|
391
|
-
): TestingModule {
|
|
392
|
-
return TestingModule.create(appModule, options)
|
|
393
|
-
}
|
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ClassType,
|
|
3
|
-
ClassTypeWithInstance,
|
|
4
|
-
InjectionToken,
|
|
5
|
-
} from '@navios/di'
|
|
6
|
-
import type {
|
|
7
|
-
MethodCallRecord,
|
|
8
|
-
MockServiceStats,
|
|
9
|
-
ProviderConfig,
|
|
10
|
-
} from '@navios/di/testing'
|
|
11
|
-
|
|
12
|
-
import { UnitTestContainer } from '@navios/di/testing'
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Options for creating a UnitTestingModule.
|
|
16
|
-
*/
|
|
17
|
-
export interface UnitTestingModuleOptions {
|
|
18
|
-
/**
|
|
19
|
-
* List of providers to register. Only these services can be resolved.
|
|
20
|
-
*/
|
|
21
|
-
providers: ProviderConfig[]
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* If true, unregistered dependencies will be auto-mocked instead of throwing.
|
|
25
|
-
* Default: false (throws on unregistered dependencies)
|
|
26
|
-
*/
|
|
27
|
-
allowUnregistered?: boolean
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Logger for debugging.
|
|
31
|
-
*/
|
|
32
|
-
logger?: Console | null
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Container to use for the testing module.
|
|
36
|
-
* If not provided, a new UnitTestContainer will be created.
|
|
37
|
-
*/
|
|
38
|
-
container?: UnitTestContainer
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* A lightweight testing module for isolated unit tests.
|
|
43
|
-
*
|
|
44
|
-
* Unlike `TestingModule`, this does NOT load Navios modules or create an application.
|
|
45
|
-
* It uses `UnitTestContainer` which:
|
|
46
|
-
* - Only allows explicitly provided services
|
|
47
|
-
* - Automatically tracks all method calls via proxies
|
|
48
|
-
* - Can auto-mock unregistered dependencies
|
|
49
|
-
*
|
|
50
|
-
* This is ideal for testing services in isolation without the overhead
|
|
51
|
-
* of full module loading.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```typescript
|
|
55
|
-
* const module = UnitTestingModule.create({
|
|
56
|
-
* providers: [
|
|
57
|
-
* { token: UserService, useClass: UserService },
|
|
58
|
-
* { token: DatabaseService, useValue: mockDatabase },
|
|
59
|
-
* ],
|
|
60
|
-
* })
|
|
61
|
-
*
|
|
62
|
-
* const userService = await module.get(UserService)
|
|
63
|
-
* await userService.findUser('123')
|
|
64
|
-
*
|
|
65
|
-
* // Method calls are automatically tracked
|
|
66
|
-
* module.expectCalled(UserService, 'findUser')
|
|
67
|
-
* module.expectCalledWith(UserService, 'findUser', ['123'])
|
|
68
|
-
*
|
|
69
|
-
* await module.close()
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
export class UnitTestingModule {
|
|
73
|
-
private constructor(private readonly container: UnitTestContainer) {}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Creates a new UnitTestingModule with the given providers.
|
|
77
|
-
*
|
|
78
|
-
* @example
|
|
79
|
-
* ```typescript
|
|
80
|
-
* const module = UnitTestingModule.create({
|
|
81
|
-
* providers: [
|
|
82
|
-
* { token: UserService, useClass: UserService },
|
|
83
|
-
* { token: ConfigToken, useValue: { apiUrl: 'test' } },
|
|
84
|
-
* ],
|
|
85
|
-
* })
|
|
86
|
-
* ```
|
|
87
|
-
*/
|
|
88
|
-
static create(options: UnitTestingModuleOptions): UnitTestingModule {
|
|
89
|
-
const container =
|
|
90
|
-
options.container ??
|
|
91
|
-
new UnitTestContainer({
|
|
92
|
-
providers: options.providers,
|
|
93
|
-
allowUnregistered: options.allowUnregistered,
|
|
94
|
-
logger: options.logger,
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
return new UnitTestingModule(container)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Gets the underlying UnitTestContainer for direct manipulation.
|
|
102
|
-
*/
|
|
103
|
-
getContainer(): UnitTestContainer {
|
|
104
|
-
return this.container
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Gets an instance from the container.
|
|
109
|
-
*
|
|
110
|
-
* All resolved instances are wrapped in tracking proxies,
|
|
111
|
-
* so method calls are automatically recorded.
|
|
112
|
-
*
|
|
113
|
-
* @throws Error if the token is not in the providers list
|
|
114
|
-
* and `allowUnregistered` is false
|
|
115
|
-
*/
|
|
116
|
-
async get<T>(
|
|
117
|
-
token: ClassTypeWithInstance<T> | InjectionToken<T, any>,
|
|
118
|
-
): Promise<T> {
|
|
119
|
-
return this.container.get(token as any)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Disposes the module and cleans up all resources.
|
|
124
|
-
*/
|
|
125
|
-
async close(): Promise<void> {
|
|
126
|
-
await this.container.clear()
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Enables auto-mocking for unregistered dependencies.
|
|
131
|
-
* Unregistered services will return a proxy that throws on method access.
|
|
132
|
-
*/
|
|
133
|
-
enableAutoMocking(): this {
|
|
134
|
-
this.container.enableAutoMocking()
|
|
135
|
-
return this
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Disables auto-mocking (strict mode).
|
|
140
|
-
* Unregistered dependencies will throw immediately on resolution.
|
|
141
|
-
*/
|
|
142
|
-
disableAutoMocking(): this {
|
|
143
|
-
this.container.disableAutoMocking()
|
|
144
|
-
return this
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// ===========================================================================
|
|
148
|
-
// ASSERTION HELPERS (delegated to UnitTestContainer)
|
|
149
|
-
// ===========================================================================
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Asserts that a service has been resolved at least once.
|
|
153
|
-
*/
|
|
154
|
-
expectResolved(token: ClassType | InjectionToken<any, any>): void {
|
|
155
|
-
this.container.expectResolved(token)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Asserts that a service has NOT been resolved.
|
|
160
|
-
*/
|
|
161
|
-
expectNotResolved(token: ClassType | InjectionToken<any, any>): void {
|
|
162
|
-
this.container.expectNotResolved(token)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Asserts that a service was auto-mocked (not in providers list).
|
|
167
|
-
*/
|
|
168
|
-
expectAutoMocked(token: ClassType | InjectionToken<any, any>): void {
|
|
169
|
-
this.container.expectAutoMocked(token)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Asserts that a service was NOT auto-mocked (is in providers list).
|
|
174
|
-
*/
|
|
175
|
-
expectNotAutoMocked(token: ClassType | InjectionToken<any, any>): void {
|
|
176
|
-
this.container.expectNotAutoMocked(token)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Asserts that a method was called on a service.
|
|
181
|
-
* Method calls are automatically tracked via proxy.
|
|
182
|
-
*/
|
|
183
|
-
expectCalled(
|
|
184
|
-
token: ClassType | InjectionToken<any, any>,
|
|
185
|
-
method: string,
|
|
186
|
-
): void {
|
|
187
|
-
this.container.expectCalled(token, method)
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Asserts that a method was NOT called on a service.
|
|
192
|
-
*/
|
|
193
|
-
expectNotCalled(
|
|
194
|
-
token: ClassType | InjectionToken<any, any>,
|
|
195
|
-
method: string,
|
|
196
|
-
): void {
|
|
197
|
-
this.container.expectNotCalled(token, method)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Asserts that a method was called with specific arguments.
|
|
202
|
-
*/
|
|
203
|
-
expectCalledWith(
|
|
204
|
-
token: ClassType | InjectionToken<any, any>,
|
|
205
|
-
method: string,
|
|
206
|
-
expectedArgs: unknown[],
|
|
207
|
-
): void {
|
|
208
|
-
this.container.expectCalledWith(token, method, expectedArgs)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Asserts that a method was called a specific number of times.
|
|
213
|
-
*/
|
|
214
|
-
expectCallCount(
|
|
215
|
-
token: ClassType | InjectionToken<any, any>,
|
|
216
|
-
method: string,
|
|
217
|
-
count: number,
|
|
218
|
-
): void {
|
|
219
|
-
this.container.expectCallCount(token, method, count)
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Gets all recorded method calls for a service.
|
|
224
|
-
*/
|
|
225
|
-
getMethodCalls(
|
|
226
|
-
token: ClassType | InjectionToken<any, any>,
|
|
227
|
-
): MethodCallRecord[] {
|
|
228
|
-
return this.container.getMethodCalls(token)
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Gets statistics about a service (instance count, method calls, lifecycle events).
|
|
233
|
-
*/
|
|
234
|
-
getServiceStats(
|
|
235
|
-
token: ClassType | InjectionToken<any, any>,
|
|
236
|
-
): MockServiceStats {
|
|
237
|
-
return this.container.getServiceStats(token)
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Clears all recorded method calls.
|
|
242
|
-
* Useful for resetting state between test assertions.
|
|
243
|
-
*/
|
|
244
|
-
clearMethodCalls(): void {
|
|
245
|
-
this.container.clearMethodCalls()
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Gets list of all registered provider token IDs.
|
|
250
|
-
*/
|
|
251
|
-
getRegisteredTokenIds(): ReadonlySet<string> {
|
|
252
|
-
return this.container.getRegisteredTokenIds()
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Gets list of all auto-mocked token IDs.
|
|
257
|
-
*/
|
|
258
|
-
getAutoMockedTokenIds(): ReadonlySet<string> {
|
|
259
|
-
return this.container.getAutoMockedTokenIds()
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// ===========================================================================
|
|
263
|
-
// LIFECYCLE ASSERTIONS
|
|
264
|
-
// ===========================================================================
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Asserts that a service's onServiceInit was called.
|
|
268
|
-
*/
|
|
269
|
-
expectInitialized(token: ClassType | InjectionToken<any, any>): void {
|
|
270
|
-
this.container.expectInitialized(token)
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Asserts that a service's onServiceDestroy was called.
|
|
275
|
-
*/
|
|
276
|
-
expectDestroyed(token: ClassType | InjectionToken<any, any>): void {
|
|
277
|
-
this.container.expectDestroyed(token)
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Asserts that a service has NOT been destroyed.
|
|
282
|
-
*/
|
|
283
|
-
expectNotDestroyed(token: ClassType | InjectionToken<any, any>): void {
|
|
284
|
-
this.container.expectNotDestroyed(token)
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Records a lifecycle event for tracking.
|
|
289
|
-
* Call this from your mock implementations if needed.
|
|
290
|
-
*/
|
|
291
|
-
recordLifecycleEvent(
|
|
292
|
-
token: ClassType | InjectionToken<any, any>,
|
|
293
|
-
event: 'created' | 'initialized' | 'destroyed',
|
|
294
|
-
instanceName: string,
|
|
295
|
-
): void {
|
|
296
|
-
this.container.recordLifecycleEvent(token, event, instanceName)
|
|
297
|
-
}
|
|
298
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { InjectionToken } from '@navios/di'
|
|
2
|
-
|
|
3
|
-
import type { AbstractExecutionContext } from '../interfaces/index.mjs'
|
|
4
|
-
|
|
5
|
-
export const ExecutionContextInjectionToken = 'ExecutionContextInjectionToken'
|
|
6
|
-
|
|
7
|
-
export const ExecutionContext = InjectionToken.create<AbstractExecutionContext>(
|
|
8
|
-
ExecutionContextInjectionToken,
|
|
9
|
-
)
|