@omnitron-dev/testing 0.4.1 → 0.4.3
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/dist/async/deferred.d.ts +15 -0
- package/dist/async/deferred.js +35 -0
- package/dist/async/deferred.js.map +1 -0
- package/dist/async/events.d.ts +32 -0
- package/dist/async/events.js +176 -0
- package/dist/async/events.js.map +1 -0
- package/dist/async/index.d.ts +5 -0
- package/dist/async/index.js +6 -0
- package/dist/async/index.js.map +1 -0
- package/dist/async/mock-timer.d.ts +19 -0
- package/dist/async/mock-timer.js +87 -0
- package/dist/async/mock-timer.js.map +1 -0
- package/dist/async/retry.d.ts +8 -0
- package/dist/async/retry.js +36 -0
- package/dist/async/retry.js.map +1 -0
- package/dist/async/wait.d.ts +10 -0
- package/dist/async/wait.js +35 -0
- package/dist/async/wait.js.map +1 -0
- package/dist/docker/database-manager.d.ts +22 -0
- package/dist/docker/database-manager.js +97 -0
- package/dist/docker/database-manager.js.map +1 -0
- package/dist/docker/docker-test-manager.d.ts +44 -0
- package/dist/docker/docker-test-manager.js +805 -0
- package/dist/docker/docker-test-manager.js.map +1 -0
- package/dist/docker/index.d.ts +4 -0
- package/dist/docker/index.js +4 -0
- package/dist/docker/index.js.map +1 -0
- package/dist/docker/redis-manager.d.ts +12 -0
- package/dist/docker/redis-manager.js +453 -0
- package/dist/docker/redis-manager.js.map +1 -0
- package/dist/docker/types.d.ts +103 -0
- package/dist/docker/types.js +2 -0
- package/dist/docker/types.js.map +1 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.js +28 -0
- package/dist/errors.js.map +1 -0
- package/dist/helpers/assertions.d.ts +6 -0
- package/dist/helpers/assertions.js +37 -0
- package/dist/helpers/assertions.js.map +1 -0
- package/dist/helpers/console.d.ts +1 -0
- package/dist/helpers/console.js +23 -0
- package/dist/helpers/console.js.map +1 -0
- package/dist/helpers/index.d.ts +3 -0
- package/dist/helpers/index.js +4 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/helpers/temp-dir.d.ts +2 -0
- package/dist/helpers/temp-dir.js +13 -0
- package/dist/helpers/temp-dir.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/performance/index.d.ts +2 -0
- package/dist/performance/index.js +3 -0
- package/dist/performance/index.js.map +1 -0
- package/dist/performance/memory-detector.d.ts +9 -0
- package/dist/performance/memory-detector.js +61 -0
- package/dist/performance/memory-detector.js.map +1 -0
- package/dist/performance/perf-timer.d.ts +9 -0
- package/dist/performance/perf-timer.js +49 -0
- package/dist/performance/perf-timer.js.map +1 -0
- package/dist/titan/container-utils.d.ts +20 -0
- package/dist/titan/container-utils.js +78 -0
- package/dist/titan/container-utils.js.map +1 -0
- package/dist/titan/database/database-testing.module.d.ts +81 -0
- package/dist/titan/database/database-testing.module.js +419 -0
- package/dist/titan/database/database-testing.module.js.map +1 -0
- package/dist/titan/database/index.d.ts +3 -0
- package/dist/titan/database/index.js +4 -0
- package/dist/titan/database/index.js.map +1 -0
- package/dist/titan/database/test-utilities.d.ts +31 -0
- package/dist/titan/database/test-utilities.js +145 -0
- package/dist/titan/database/test-utilities.js.map +1 -0
- package/dist/titan/index.d.ts +14 -0
- package/dist/titan/index.js +10 -0
- package/dist/titan/index.js.map +1 -0
- package/dist/titan/nexus/index.d.ts +3 -0
- package/dist/titan/nexus/index.js +4 -0
- package/dist/titan/nexus/index.js.map +1 -0
- package/dist/titan/nexus/mock-provider.d.ts +41 -0
- package/dist/titan/nexus/mock-provider.js +127 -0
- package/dist/titan/nexus/mock-provider.js.map +1 -0
- package/dist/titan/nexus/test-container.d.ts +120 -0
- package/dist/titan/nexus/test-container.js +559 -0
- package/dist/titan/nexus/test-container.js.map +1 -0
- package/dist/titan/nexus/test-utilities.d.ts +135 -0
- package/dist/titan/nexus/test-utilities.js +484 -0
- package/dist/titan/nexus/test-utilities.js.map +1 -0
- package/dist/titan/pm/index.d.ts +4 -0
- package/dist/titan/pm/index.js +4 -0
- package/dist/titan/pm/index.js.map +1 -0
- package/dist/titan/pm/mock-process-spawner.d.ts +60 -0
- package/dist/titan/pm/mock-process-spawner.js +321 -0
- package/dist/titan/pm/mock-process-spawner.js.map +1 -0
- package/dist/titan/pm/mock-spawner.d.ts +27 -0
- package/dist/titan/pm/mock-spawner.js +99 -0
- package/dist/titan/pm/mock-spawner.js.map +1 -0
- package/dist/titan/pm/test-process-manager.d.ts +49 -0
- package/dist/titan/pm/test-process-manager.js +188 -0
- package/dist/titan/pm/test-process-manager.js.map +1 -0
- package/dist/titan/redis-test-utils.d.ts +33 -0
- package/dist/titan/redis-test-utils.js +414 -0
- package/dist/titan/redis-test-utils.js.map +1 -0
- package/dist/titan/test-application.d.ts +21 -0
- package/dist/titan/test-application.js +90 -0
- package/dist/titan/test-application.js.map +1 -0
- package/dist/titan/test-fixtures.d.ts +97 -0
- package/dist/titan/test-fixtures.js +223 -0
- package/dist/titan/test-fixtures.js.map +1 -0
- package/dist/titan/test-mocks.d.ts +29 -0
- package/dist/titan/test-mocks.js +262 -0
- package/dist/titan/test-mocks.js.map +1 -0
- package/dist/titan/test-module.d.ts +46 -0
- package/dist/titan/test-module.js +120 -0
- package/dist/titan/test-module.js.map +1 -0
- package/package.json +41 -4
- package/src/async/README.md +336 -0
- package/src/async/deferred.ts +98 -0
- package/src/async/events.ts +386 -0
- package/src/async/index.ts +48 -0
- package/src/async/mock-timer.ts +220 -0
- package/src/async/retry.ts +118 -0
- package/src/async/wait.ts +151 -0
- package/src/docker/database-manager.ts +242 -0
- package/src/docker/docker-test-manager.ts +1186 -0
- package/src/docker/index.ts +48 -0
- package/src/docker/redis-manager.ts +738 -0
- package/src/docker/types.ts +216 -0
- package/src/errors.ts +31 -0
- package/src/helpers/assertions.ts +171 -0
- package/src/helpers/console.ts +74 -0
- package/src/helpers/index.ts +15 -0
- package/src/helpers/temp-dir.ts +50 -0
- package/src/index.ts +2 -0
- package/src/performance/index.ts +11 -0
- package/src/performance/memory-detector.ts +166 -0
- package/src/performance/perf-timer.ts +146 -0
- package/src/titan/container-utils.ts +128 -0
- package/src/titan/database/database-testing.module.ts +831 -0
- package/src/titan/database/index.ts +30 -0
- package/src/titan/database/test-utilities.ts +337 -0
- package/src/titan/index.ts +147 -0
- package/src/titan/nexus/index.ts +3 -0
- package/src/titan/nexus/mock-provider.ts +189 -0
- package/src/titan/nexus/test-container.ts +887 -0
- package/src/titan/nexus/test-utilities.ts +796 -0
- package/src/titan/pm/index.ts +22 -0
- package/src/titan/pm/mock-process-spawner.ts +481 -0
- package/src/titan/pm/mock-spawner.ts +156 -0
- package/src/titan/pm/test-process-manager.ts +345 -0
- package/src/titan/redis-test-utils.ts +566 -0
- package/src/titan/test-application.ts +154 -0
- package/src/titan/test-fixtures.ts +304 -0
- package/src/titan/test-mocks.ts +336 -0
- package/src/titan/test-module.ts +232 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { IModule, ProviderDefinition, InjectionToken } from '@omnitron-dev/titan/nexus';
|
|
2
|
+
import { Application, IApplicationConfig } from '@omnitron-dev/titan';
|
|
3
|
+
|
|
4
|
+
// Import TestContainer from local nexus testing utilities (now in this package)
|
|
5
|
+
import { TestContainer, createTestContainer } from './nexus/test-container.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for creating a test module
|
|
9
|
+
*/
|
|
10
|
+
export interface TestModuleOptions {
|
|
11
|
+
modules?: IModule[];
|
|
12
|
+
providers?: Array<[InjectionToken<any>, ProviderDefinition<any>]>;
|
|
13
|
+
mocks?: Array<{
|
|
14
|
+
token: InjectionToken<any>;
|
|
15
|
+
mock: any;
|
|
16
|
+
spy?: boolean;
|
|
17
|
+
}>;
|
|
18
|
+
config?: Partial<IApplicationConfig>;
|
|
19
|
+
autoMock?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Test module for Titan applications
|
|
24
|
+
*/
|
|
25
|
+
export class TestModule {
|
|
26
|
+
private container: TestContainer;
|
|
27
|
+
private app?: Application;
|
|
28
|
+
|
|
29
|
+
constructor(private options: TestModuleOptions = {}) {
|
|
30
|
+
this.container = createTestContainer({
|
|
31
|
+
modules: options.modules || [],
|
|
32
|
+
providers: options.providers || [],
|
|
33
|
+
mocks: options.mocks || [],
|
|
34
|
+
autoMock: options.autoMock || false,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Get the test container
|
|
40
|
+
*/
|
|
41
|
+
getContainer(): TestContainer {
|
|
42
|
+
return this.container;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Create an application instance for testing
|
|
47
|
+
*/
|
|
48
|
+
async createApplication(): Promise<Application> {
|
|
49
|
+
if (this.app) {
|
|
50
|
+
return this.app;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const config: IApplicationConfig = {
|
|
54
|
+
name: 'test-app',
|
|
55
|
+
version: '0.0.0-test',
|
|
56
|
+
...this.options.config,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
this.app = new Application(config);
|
|
60
|
+
|
|
61
|
+
// Use the test container for dependency injection
|
|
62
|
+
(this.app as any).container = this.container;
|
|
63
|
+
|
|
64
|
+
// Load modules if provided
|
|
65
|
+
if (this.options.modules) {
|
|
66
|
+
for (const module of this.options.modules) {
|
|
67
|
+
this.container.loadModule(module);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return this.app;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Mock a dependency
|
|
76
|
+
*/
|
|
77
|
+
mock<T>(token: InjectionToken<T>, mock: T, spy = false): this {
|
|
78
|
+
this.container.mock(token, mock, spy);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Stub a dependency with partial implementation
|
|
84
|
+
*/
|
|
85
|
+
stub<T>(token: InjectionToken<T>, stub: Partial<T>): this {
|
|
86
|
+
this.container.stub(token, stub);
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Override a dependency
|
|
92
|
+
*/
|
|
93
|
+
override<T>(token: InjectionToken<T>): {
|
|
94
|
+
useValue: (value: T) => TestModule;
|
|
95
|
+
useClass: (cls: new (...args: any[]) => T) => TestModule;
|
|
96
|
+
useFactory: (factory: (...args: any[]) => T) => TestModule;
|
|
97
|
+
} {
|
|
98
|
+
return {
|
|
99
|
+
useValue: (value: T) => {
|
|
100
|
+
this.container.override(token).useValue(value);
|
|
101
|
+
return this;
|
|
102
|
+
},
|
|
103
|
+
useClass: (cls: new (...args: any[]) => T) => {
|
|
104
|
+
this.container.override(token).useClass(cls);
|
|
105
|
+
return this;
|
|
106
|
+
},
|
|
107
|
+
useFactory: (factory: (...args: any[]) => T) => {
|
|
108
|
+
this.container.override(token).useFactory(factory);
|
|
109
|
+
return this;
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Spy on a method
|
|
116
|
+
*/
|
|
117
|
+
spy<T>(token: InjectionToken<T>, method: keyof T): any {
|
|
118
|
+
return this.container.spy(token, method);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Get a service from the container
|
|
123
|
+
*/
|
|
124
|
+
get<T>(token: InjectionToken<T>): T {
|
|
125
|
+
return this.container.resolve(token);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Reset all mocks
|
|
130
|
+
*/
|
|
131
|
+
resetMocks(): void {
|
|
132
|
+
this.container.resetMocks();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Clear all mocks
|
|
137
|
+
*/
|
|
138
|
+
clearMocks(): void {
|
|
139
|
+
this.container.clearMocks();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Restore to original state
|
|
144
|
+
*/
|
|
145
|
+
restore(): void {
|
|
146
|
+
this.container.restore();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Clean up resources
|
|
151
|
+
*/
|
|
152
|
+
async cleanup(): Promise<void> {
|
|
153
|
+
if (this.app) {
|
|
154
|
+
await this.app.stop();
|
|
155
|
+
this.app = undefined;
|
|
156
|
+
}
|
|
157
|
+
this.container.restore();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Create a test module
|
|
163
|
+
*/
|
|
164
|
+
export function createTestModule(options: TestModuleOptions = {}): TestModule {
|
|
165
|
+
return new TestModule(options);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Test module builder for fluent API
|
|
170
|
+
*/
|
|
171
|
+
export class TestModuleBuilder {
|
|
172
|
+
private options: TestModuleOptions = {
|
|
173
|
+
modules: [],
|
|
174
|
+
providers: [],
|
|
175
|
+
mocks: [],
|
|
176
|
+
config: {},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Add a module to test
|
|
181
|
+
*/
|
|
182
|
+
withModule(module: IModule): this {
|
|
183
|
+
this.options.modules!.push(module);
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Add a provider
|
|
189
|
+
*/
|
|
190
|
+
withProvider<T>(token: InjectionToken<T>, provider: ProviderDefinition<T>): this {
|
|
191
|
+
this.options.providers!.push([token, provider]);
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Add a mock
|
|
197
|
+
*/
|
|
198
|
+
withMock<T>(token: InjectionToken<T>, mock: T, spy = false): this {
|
|
199
|
+
this.options.mocks!.push({ token, mock, spy });
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Set application config
|
|
205
|
+
*/
|
|
206
|
+
withConfig(config: Partial<IApplicationConfig>): this {
|
|
207
|
+
this.options.config = { ...this.options.config, ...config };
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Enable auto-mocking
|
|
213
|
+
*/
|
|
214
|
+
withAutoMock(): this {
|
|
215
|
+
this.options.autoMock = true;
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Build the test module
|
|
221
|
+
*/
|
|
222
|
+
build(): TestModule {
|
|
223
|
+
return new TestModule(this.options);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Create a test module builder
|
|
229
|
+
*/
|
|
230
|
+
export function testModule(): TestModuleBuilder {
|
|
231
|
+
return new TestModuleBuilder();
|
|
232
|
+
}
|