@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,166 @@
1
+ /**
2
+ * Memory leak detector for identifying potential memory leaks in tests.
3
+ *
4
+ * This class monitors heap memory usage over time and can detect memory growth
5
+ * trends that may indicate memory leaks. It requires Node.js to be run with the
6
+ * `--expose-gc` flag to enable manual garbage collection.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Run Node with: node --expose-gc test.js
11
+ *
12
+ * const detector = new MemoryLeakDetector();
13
+ *
14
+ * // Run test iterations
15
+ * for (let i = 0; i < 100; i++) {
16
+ * await doWork();
17
+ * detector.measure();
18
+ * }
19
+ *
20
+ * // Check for leaks
21
+ * if (detector.hasLeak()) {
22
+ * console.warn('Potential memory leak detected!');
23
+ * console.log('Trend:', detector.getTrend());
24
+ * }
25
+ * ```
26
+ */
27
+ export class MemoryLeakDetector {
28
+ private initialMemory: number;
29
+ private measurements: number[] = [];
30
+
31
+ /**
32
+ * Creates a new memory leak detector.
33
+ *
34
+ * Triggers garbage collection (if available) and records the initial heap usage.
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * const detector = new MemoryLeakDetector();
39
+ * ```
40
+ */
41
+ constructor() {
42
+ if (global.gc) {
43
+ global.gc();
44
+ }
45
+ this.initialMemory = process.memoryUsage().heapUsed;
46
+ }
47
+
48
+ /**
49
+ * Measure current memory usage relative to the initial baseline.
50
+ *
51
+ * Triggers garbage collection (if available) before measuring to get
52
+ * accurate readings.
53
+ *
54
+ * @returns The memory delta in bytes (positive indicates growth)
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * const delta = detector.measure();
59
+ * console.log(`Memory delta: ${delta / 1024 / 1024}MB`);
60
+ * ```
61
+ */
62
+ measure(): number {
63
+ if (global.gc) {
64
+ global.gc();
65
+ }
66
+ const current = process.memoryUsage().heapUsed;
67
+ const delta = current - this.initialMemory;
68
+ this.measurements.push(delta);
69
+ return delta;
70
+ }
71
+
72
+ /**
73
+ * Check if there's a potential memory leak.
74
+ *
75
+ * A leak is detected if the average of the last 5 measurements exceeds
76
+ * the specified threshold.
77
+ *
78
+ * @param threshold - The memory growth threshold in bytes (default: 10MB)
79
+ * @returns `true` if average memory growth exceeds the threshold
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * // Check with default 10MB threshold
84
+ * if (detector.hasLeak()) {
85
+ * console.warn('Potential leak detected');
86
+ * }
87
+ *
88
+ * // Check with custom 5MB threshold
89
+ * if (detector.hasLeak(5 * 1024 * 1024)) {
90
+ * console.warn('Memory grew by more than 5MB');
91
+ * }
92
+ * ```
93
+ */
94
+ hasLeak(threshold = 10 * 1024 * 1024): boolean {
95
+ // Consider it a leak if memory grows by more than threshold
96
+ if (this.measurements.length < 2) {
97
+ return false;
98
+ }
99
+
100
+ const recent = this.measurements.slice(-5);
101
+ const average = recent.reduce((sum, val) => sum + val, 0) / recent.length;
102
+ return average > threshold;
103
+ }
104
+
105
+ /**
106
+ * Get the memory growth trend.
107
+ *
108
+ * Analyzes the last 5 measurements to determine if memory is consistently
109
+ * growing, shrinking, or stable.
110
+ *
111
+ * @returns The memory trend: 'growing', 'shrinking', or 'stable'
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const trend = detector.getTrend();
116
+ * console.log(`Memory trend: ${trend}`);
117
+ * ```
118
+ */
119
+ getTrend(): 'stable' | 'growing' | 'shrinking' {
120
+ if (this.measurements.length < 3) {
121
+ return 'stable';
122
+ }
123
+
124
+ const recent = this.measurements.slice(-5);
125
+ let growing = 0;
126
+ let shrinking = 0;
127
+
128
+ for (let i = 1; i < recent.length; i++) {
129
+ const current = recent[i];
130
+ const previous = recent[i - 1];
131
+ if (current !== undefined && previous !== undefined) {
132
+ if (current > previous) {
133
+ growing++;
134
+ } else if (current < previous) {
135
+ shrinking++;
136
+ }
137
+ }
138
+ }
139
+
140
+ if (growing > shrinking * 2) {
141
+ return 'growing';
142
+ } else if (shrinking > growing * 2) {
143
+ return 'shrinking';
144
+ }
145
+ return 'stable';
146
+ }
147
+
148
+ /**
149
+ * Reset the detector to start fresh measurements.
150
+ *
151
+ * Triggers garbage collection (if available) and sets a new baseline.
152
+ *
153
+ * @example
154
+ * ```typescript
155
+ * detector.reset();
156
+ * // Start new test run
157
+ * ```
158
+ */
159
+ reset(): void {
160
+ if (global.gc) {
161
+ global.gc();
162
+ }
163
+ this.initialMemory = process.memoryUsage().heapUsed;
164
+ this.measurements = [];
165
+ }
166
+ }
@@ -0,0 +1,146 @@
1
+ import { NotFoundError } from '../errors.js';
2
+
3
+ /**
4
+ * Performance timer for measuring operation performance with percentile calculation.
5
+ *
6
+ * This class provides utilities for timing operations and calculating statistics
7
+ * like averages and percentiles across multiple measurements.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const timer = new PerfTimer();
12
+ *
13
+ * // Mark start and end points
14
+ * timer.mark('start');
15
+ * await doWork();
16
+ * timer.mark('end');
17
+ *
18
+ * // Measure duration
19
+ * const duration = timer.measure('work', 'start', 'end');
20
+ * console.log(`Duration: ${duration}ms`);
21
+ *
22
+ * // Get statistics
23
+ * const avg = timer.getAverage('work');
24
+ * const p95 = timer.getPercentile('work', 95);
25
+ * ```
26
+ */
27
+ export class PerfTimer {
28
+ private marks = new Map<string, number>();
29
+ private measures = new Map<string, number[]>();
30
+
31
+ /**
32
+ * Mark a point in time.
33
+ *
34
+ * @param name - The name of the mark
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * timer.mark('start');
39
+ * await doWork();
40
+ * timer.mark('end');
41
+ * ```
42
+ */
43
+ mark(name: string): void {
44
+ this.marks.set(name, performance.now());
45
+ }
46
+
47
+ /**
48
+ * Measure the duration between two marks.
49
+ *
50
+ * @param name - The name of the measurement (used for statistics)
51
+ * @param startMark - The name of the start mark
52
+ * @param endMark - The name of the end mark (defaults to current time if not provided)
53
+ * @returns The duration in milliseconds
54
+ * @throws {NotFoundError} If the start or end mark is not found
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * timer.mark('start');
59
+ * await doWork();
60
+ * timer.mark('end');
61
+ * const duration = timer.measure('work', 'start', 'end');
62
+ * ```
63
+ */
64
+ measure(name: string, startMark: string, endMark?: string): number {
65
+ const start = this.marks.get(startMark);
66
+ if (!start) {
67
+ throw new NotFoundError('performance mark', startMark);
68
+ }
69
+
70
+ const end = endMark ? this.marks.get(endMark) : performance.now();
71
+ if (!end) {
72
+ throw new NotFoundError('performance mark', endMark);
73
+ }
74
+
75
+ const duration = end - start;
76
+
77
+ let measures = this.measures.get(name);
78
+ if (!measures) {
79
+ measures = [];
80
+ this.measures.set(name, measures);
81
+ }
82
+ measures.push(duration);
83
+
84
+ return duration;
85
+ }
86
+
87
+ /**
88
+ * Get the average duration for a measurement.
89
+ *
90
+ * @param name - The name of the measurement
91
+ * @returns The average duration in milliseconds, or 0 if no measurements exist
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * // After multiple measurements
96
+ * const avg = timer.getAverage('work');
97
+ * console.log(`Average: ${avg}ms`);
98
+ * ```
99
+ */
100
+ getAverage(name: string): number {
101
+ const measures = this.measures.get(name);
102
+ if (!measures || measures.length === 0) {
103
+ return 0;
104
+ }
105
+ return measures.reduce((sum, val) => sum + val, 0) / measures.length;
106
+ }
107
+
108
+ /**
109
+ * Get a percentile value for a measurement.
110
+ *
111
+ * @param name - The name of the measurement
112
+ * @param percentile - The percentile to calculate (0-100)
113
+ * @returns The percentile value in milliseconds, or 0 if no measurements exist
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * // Get 95th percentile
118
+ * const p95 = timer.getPercentile('work', 95);
119
+ * console.log(`P95: ${p95}ms`);
120
+ * ```
121
+ */
122
+ getPercentile(name: string, percentile: number): number {
123
+ const measures = this.measures.get(name);
124
+ if (!measures || measures.length === 0) {
125
+ return 0;
126
+ }
127
+
128
+ const sorted = [...measures].sort((a, b) => a - b);
129
+ const index = Math.min(Math.floor(sorted.length * (percentile / 100)), sorted.length - 1);
130
+ return sorted[index] ?? 0;
131
+ }
132
+
133
+ /**
134
+ * Clear all marks and measures.
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * timer.clear();
139
+ * // Start fresh measurements
140
+ * ```
141
+ */
142
+ clear(): void {
143
+ this.marks.clear();
144
+ this.measures.clear();
145
+ }
146
+ }
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Bun adapter for running tests
3
+ * Provides compatibility layer for Vitest API
4
+ */
5
+
6
+ import {
7
+ mock,
8
+ test as bunTest,
9
+ expect as bunExpect,
10
+ describe as bunDescribe,
11
+ afterAll as bunAfterAll,
12
+ afterEach as bunAfterEach,
13
+ beforeAll as bunBeforeAll,
14
+ beforeEach as bunBeforeEach,
15
+ } from 'bun:test';
16
+
17
+ import { timerUtils } from './test-adapter.js';
18
+
19
+ // Make test-compatible APIs globally available
20
+ (global as any).describe = bunDescribe;
21
+ (global as any).test = bunTest;
22
+ (global as any).it = bunTest;
23
+ (global as any).expect = bunExpect;
24
+ (global as any).beforeEach = bunBeforeEach;
25
+ (global as any).afterEach = bunAfterEach;
26
+ (global as any).beforeAll = bunBeforeAll;
27
+ (global as any).afterAll = bunAfterAll;
28
+
29
+ // Use timer utilities from shared adapter
30
+ const fakeTimers = {
31
+ useFakeTimers: () => {
32
+ timerUtils.useFakeTimers();
33
+ return fakeTimers;
34
+ },
35
+ useRealTimers: () => {
36
+ timerUtils.useRealTimers();
37
+ return fakeTimers;
38
+ },
39
+ advanceTimersByTime: (ms: number) => {
40
+ timerUtils.advanceTimersByTime(ms);
41
+ },
42
+ runAllTimers: () => {
43
+ timerUtils.runAllTimers();
44
+ },
45
+ clearAllTimers: () => {
46
+ timerUtils.clearAllTimers();
47
+ },
48
+ };
49
+
50
+ // Mock vi APIs (Vitest-compatible)
51
+ (global as any).vi = {
52
+ fn: mock,
53
+ mock,
54
+ spyOn: (obj: any, method: string) => {
55
+ const original = obj[method];
56
+ const spy = mock(original);
57
+ obj[method] = spy;
58
+ (spy as any).mockRestore = () => {
59
+ obj[method] = original;
60
+ };
61
+ return spy;
62
+ },
63
+ isMockFunction: (fn: any) => typeof fn === 'function' && typeof fn.mock !== 'undefined',
64
+ useFakeTimers: fakeTimers.useFakeTimers,
65
+ useRealTimers: fakeTimers.useRealTimers,
66
+ advanceTimersByTime: fakeTimers.advanceTimersByTime,
67
+ runAllTimers: fakeTimers.runAllTimers,
68
+ clearAllTimers: fakeTimers.clearAllTimers,
69
+ };
70
+
71
+ // Add missing expect matchers if needed
72
+ const originalExpect = bunExpect as any;
73
+ if (!originalExpect.toBeInstanceOf) {
74
+ originalExpect.extend({
75
+ toBeInstanceOf(received: any, constructor: any) {
76
+ const pass = received instanceof constructor;
77
+ return {
78
+ pass,
79
+ message: () =>
80
+ pass
81
+ ? `expected ${received} not to be instance of ${constructor.name}`
82
+ : `expected ${received} to be instance of ${constructor.name}`,
83
+ };
84
+ },
85
+ });
86
+ }
87
+
88
+ if (!originalExpect.toStrictEqual) {
89
+ // toStrictEqual is an alias for toEqual in Bun
90
+ originalExpect.toStrictEqual = originalExpect.toEqual;
91
+ }
92
+
93
+ if (!originalExpect.toThrowError) {
94
+ originalExpect.extend({
95
+ toThrowError(received: any, expected?: any) {
96
+ let error: any;
97
+ let pass = false;
98
+
99
+ try {
100
+ if (typeof received === 'function') {
101
+ received();
102
+ }
103
+ } catch (e) {
104
+ error = e;
105
+ pass = true;
106
+
107
+ if (expected !== undefined) {
108
+ if (typeof expected === 'string') {
109
+ pass = error.message.includes(expected);
110
+ } else if (expected instanceof RegExp) {
111
+ pass = expected.test(error.message);
112
+ } else if (typeof expected === 'function') {
113
+ pass = error instanceof expected;
114
+ }
115
+ }
116
+ }
117
+
118
+ return {
119
+ pass,
120
+ message: () =>
121
+ pass
122
+ ? `expected function not to throw${expected ? ` ${expected}` : ''}`
123
+ : `expected function to throw${expected ? ` ${expected}` : ''}`,
124
+ };
125
+ },
126
+ });
127
+ }
@@ -0,0 +1,286 @@
1
+ /**
2
+ * Deno adapter for running tests
3
+ * Provides compatibility layer for Vitest API
4
+ */
5
+
6
+ // @ts-expect-error - Deno specific imports
7
+ import { FakeTime } from 'https://deno.land/std@0.208.0/testing/time.ts';
8
+ // @ts-expect-error - Deno specific imports
9
+ import { expect as denoExpect } from 'https://deno.land/x/expect@v0.3.0/mod.ts';
10
+ // @ts-expect-error - Deno specific imports
11
+ import { it as denoIt, describe as denoDescribe } from 'https://deno.land/std@0.208.0/testing/bdd.ts';
12
+
13
+ // Polyfill for node:util inherits
14
+ function inherits(ctor: any, superCtor: any) {
15
+ if (ctor === undefined || ctor === null) {
16
+ throw new TypeError('The constructor to "inherits" must not be null or undefined');
17
+ }
18
+ if (superCtor === undefined || superCtor === null) {
19
+ throw new TypeError('The super constructor to "inherits" must not be null or undefined');
20
+ }
21
+ if (superCtor.prototype === undefined) {
22
+ throw new TypeError('The super constructor to "inherits" must have a prototype property');
23
+ }
24
+ ctor.super_ = superCtor;
25
+ Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
26
+ }
27
+
28
+ // Store global state
29
+ let fakeTime: FakeTime | null = null;
30
+ const beforeEachCallbacks: Array<() => void | Promise<void>> = [];
31
+ const afterEachCallbacks: Array<() => void | Promise<void>> = [];
32
+ const beforeAllCallbacks: Array<() => void | Promise<void>> = [];
33
+ const afterAllCallbacks: Array<() => void | Promise<void>> = [];
34
+
35
+ // Custom describe that handles lifecycle hooks
36
+ function describe(name: string, fn: () => void) {
37
+ denoDescribe(name, () => {
38
+ // Clear callbacks for this describe block
39
+ // Note: These are preserved for potential future use in nested describe blocks
40
+ // const localBeforeEach = [...beforeEachCallbacks];
41
+ // const localAfterEach = [...afterEachCallbacks];
42
+ const localBeforeAll = [...beforeAllCallbacks];
43
+ const localAfterAll = [...afterAllCallbacks];
44
+
45
+ // Run beforeAll hooks
46
+ if (localBeforeAll.length > 0) {
47
+ denoIt('beforeAll', async () => {
48
+ for (const cb of localBeforeAll) {
49
+ await cb();
50
+ }
51
+ });
52
+ }
53
+
54
+ fn();
55
+
56
+ // Run afterAll hooks
57
+ if (localAfterAll.length > 0) {
58
+ denoIt('afterAll', async () => {
59
+ for (const cb of localAfterAll) {
60
+ await cb();
61
+ }
62
+ });
63
+ }
64
+ });
65
+ }
66
+
67
+ // Custom it/test that runs lifecycle hooks
68
+ function it(name: string, fn: () => void | Promise<void>) {
69
+ denoIt(name, async () => {
70
+ // Run beforeEach hooks
71
+ for (const cb of beforeEachCallbacks) {
72
+ await cb();
73
+ }
74
+
75
+ try {
76
+ await fn();
77
+ } finally {
78
+ // Run afterEach hooks
79
+ for (const cb of afterEachCallbacks) {
80
+ await cb();
81
+ }
82
+ }
83
+ });
84
+ }
85
+
86
+ // Lifecycle hooks
87
+ function beforeEach(fn: () => void | Promise<void>) {
88
+ beforeEachCallbacks.push(fn);
89
+ }
90
+
91
+ function afterEach(fn: () => void | Promise<void>) {
92
+ afterEachCallbacks.push(fn);
93
+ }
94
+
95
+ function beforeAll(fn: () => void | Promise<void>) {
96
+ beforeAllCallbacks.push(fn);
97
+ }
98
+
99
+ function afterAll(fn: () => void | Promise<void>) {
100
+ afterAllCallbacks.push(fn);
101
+ }
102
+
103
+ // Mock function implementation
104
+ function mockFn(implementation?: (...args: any[]) => any) {
105
+ const calls: any[][] = [];
106
+ const results: any[] = [];
107
+
108
+ const fn = (...args: any[]) => {
109
+ calls.push(args);
110
+ const result = implementation ? implementation(...args) : undefined;
111
+ results.push(result);
112
+ return result;
113
+ };
114
+
115
+ fn.mock = {
116
+ calls,
117
+ results,
118
+ lastCall: () => calls[calls.length - 1],
119
+ };
120
+
121
+ fn.mockImplementation = (impl: (...args: any[]) => any) => {
122
+ implementation = impl;
123
+ return fn;
124
+ };
125
+
126
+ fn.mockReturnValue = (value: any) => {
127
+ implementation = () => value;
128
+ return fn;
129
+ };
130
+
131
+ fn.mockResolvedValue = (value: any) => {
132
+ implementation = () => Promise.resolve(value);
133
+ return fn;
134
+ };
135
+
136
+ fn.mockRejectedValue = (value: any) => {
137
+ implementation = () => Promise.reject(value);
138
+ return fn;
139
+ };
140
+
141
+ fn.mockClear = () => {
142
+ calls.length = 0;
143
+ results.length = 0;
144
+ };
145
+
146
+ fn.mockReset = () => {
147
+ calls.length = 0;
148
+ results.length = 0;
149
+ implementation = undefined;
150
+ };
151
+
152
+ fn.mockRestore = () => {
153
+ fn.mockReset();
154
+ };
155
+
156
+ return fn;
157
+ }
158
+
159
+ // Mock timer functions
160
+ const fakeTimers = {
161
+ useFakeTimers: () => {
162
+ fakeTime = new FakeTime();
163
+ return fakeTimers;
164
+ },
165
+ useRealTimers: () => {
166
+ if (fakeTime) {
167
+ fakeTime.restore();
168
+ fakeTime = null;
169
+ }
170
+ return fakeTimers;
171
+ },
172
+ advanceTimersByTime: (ms: number) => {
173
+ if (fakeTime) {
174
+ fakeTime.tick(ms);
175
+ }
176
+ },
177
+ runAllTimers: () => {
178
+ if (fakeTime) {
179
+ fakeTime.runAll();
180
+ }
181
+ },
182
+ clearAllTimers: () => {
183
+ if (fakeTime) {
184
+ fakeTime.restore();
185
+ fakeTime = new FakeTime();
186
+ }
187
+ },
188
+ };
189
+
190
+ // Custom expect wrapper that adds missing methods
191
+ const expect = (value: any) => {
192
+ const matcher = denoExpect(value);
193
+
194
+ // Add missing matcher methods to the chain
195
+ matcher.toBeInstanceOf = function toBeInstanceOf(constructor: any) {
196
+ const pass = value instanceof constructor;
197
+ if (!pass) {
198
+ throw new Error(`expected ${value} to be instance of ${constructor.name}`);
199
+ }
200
+ return { pass };
201
+ };
202
+
203
+ matcher.toStrictEqual = function toStrictEqual(expected: any) {
204
+ return matcher.toEqual(expected);
205
+ };
206
+
207
+ matcher.toThrowError = function toThrowError(expected?: any) {
208
+ let error: any;
209
+ let thrown = false;
210
+
211
+ try {
212
+ if (typeof value === 'function') {
213
+ value();
214
+ }
215
+ } catch (e) {
216
+ error = e;
217
+ thrown = true;
218
+ }
219
+
220
+ if (!thrown) {
221
+ throw new Error('expected function to throw');
222
+ }
223
+
224
+ if (expected !== undefined) {
225
+ if (typeof expected === 'string' && !error.message.includes(expected)) {
226
+ throw new Error(`expected error message to include "${expected}"`);
227
+ }
228
+ if (expected instanceof RegExp && !expected.test(error.message)) {
229
+ throw new Error(`expected error message to match ${expected}`);
230
+ }
231
+ if (typeof expected === 'function' && !(error instanceof expected)) {
232
+ throw new Error(`expected error to be instance of ${expected.name}`);
233
+ }
234
+ }
235
+
236
+ return { pass: true };
237
+ };
238
+
239
+ return matcher;
240
+ };
241
+
242
+ // Add static methods
243
+ expect.extend = (matchers: Record<string, any>) => {
244
+ Object.assign(expect, matchers);
245
+ };
246
+
247
+ // Make APIs globally available
248
+ (globalThis as any).describe = describe;
249
+ (globalThis as any).test = it;
250
+ (globalThis as any).it = it;
251
+ (globalThis as any).expect = expect;
252
+ (globalThis as any).beforeEach = beforeEach;
253
+ (globalThis as any).afterEach = afterEach;
254
+ (globalThis as any).beforeAll = beforeAll;
255
+ (globalThis as any).afterAll = afterAll;
256
+
257
+ // Mock vi APIs (Vitest-compatible)
258
+ (globalThis as any).vi = {
259
+ fn: mockFn,
260
+ mock: mockFn,
261
+ spyOn: (obj: any, method: string) => {
262
+ const original = obj[method];
263
+ const spy = mockFn(original);
264
+ obj[method] = spy;
265
+ spy.mockRestore = () => {
266
+ obj[method] = original;
267
+ };
268
+ return spy;
269
+ },
270
+ isMockFunction: (fn: any) => typeof fn === 'function' && fn.mock !== undefined,
271
+ useFakeTimers: fakeTimers.useFakeTimers,
272
+ useRealTimers: fakeTimers.useRealTimers,
273
+ advanceTimersByTime: fakeTimers.advanceTimersByTime,
274
+ runAllTimers: fakeTimers.runAllTimers,
275
+ clearAllTimers: fakeTimers.clearAllTimers,
276
+ };
277
+
278
+ // Make Node.js APIs available globally for tests
279
+ (globalThis as any).node = {
280
+ util: {
281
+ inherits,
282
+ },
283
+ };
284
+
285
+ // Export everything
286
+ export { it, expect, mockFn, describe, afterAll, inherits, afterEach, beforeAll, beforeEach };