@omnitron-dev/testing 0.1.1

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