@omnitron-dev/testing 0.4.1 → 0.4.2

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