@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,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Docker Test Manager Types
|
|
3
|
+
*
|
|
4
|
+
* Type definitions for Docker container management in tests.
|
|
5
|
+
* These are runtime-agnostic and have no framework dependencies.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Status information for a Docker container
|
|
10
|
+
*/
|
|
11
|
+
export interface DockerContainerStatus {
|
|
12
|
+
/** Whether the container is currently running */
|
|
13
|
+
isRunning: boolean;
|
|
14
|
+
/** Container state (running, exited, paused, etc.) */
|
|
15
|
+
state?: string;
|
|
16
|
+
/** Health check status (healthy, unhealthy, starting) */
|
|
17
|
+
health?: string;
|
|
18
|
+
/** Exit code if container has stopped */
|
|
19
|
+
exitCode?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Represents a running Docker container instance
|
|
24
|
+
*/
|
|
25
|
+
export interface DockerContainer {
|
|
26
|
+
/** Unique test identifier */
|
|
27
|
+
id: string;
|
|
28
|
+
/** Container name */
|
|
29
|
+
name: string;
|
|
30
|
+
/** Docker image used */
|
|
31
|
+
image: string;
|
|
32
|
+
/** Primary host port (first mapped port for convenience) */
|
|
33
|
+
port?: number;
|
|
34
|
+
/** Host address (typically 127.0.0.1) */
|
|
35
|
+
host: string;
|
|
36
|
+
/** Map of container port to host port */
|
|
37
|
+
ports: Map<number, number>;
|
|
38
|
+
/** Environment variables set in container */
|
|
39
|
+
environment: Record<string, string>;
|
|
40
|
+
/** Docker labels applied to container */
|
|
41
|
+
labels: Record<string, string>;
|
|
42
|
+
/** Networks the container is connected to */
|
|
43
|
+
networks: string[];
|
|
44
|
+
/** When the container was created */
|
|
45
|
+
createdAt: Date;
|
|
46
|
+
/** Internal IP address within Docker network */
|
|
47
|
+
internalIp?: string;
|
|
48
|
+
/** Clean up and remove this container */
|
|
49
|
+
cleanup: () => Promise<void>;
|
|
50
|
+
/** Get current container status */
|
|
51
|
+
getStatus: () => Promise<DockerContainerStatus>;
|
|
52
|
+
/** Check if container is healthy */
|
|
53
|
+
isHealthy: () => Promise<boolean>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Configuration options for DockerTestManager
|
|
58
|
+
*/
|
|
59
|
+
export interface DockerTestManagerOptions {
|
|
60
|
+
/** Path to docker executable (auto-detected if not provided) */
|
|
61
|
+
dockerPath?: string;
|
|
62
|
+
/** Base port for dynamic port allocation (default: 10000 + worker offset) */
|
|
63
|
+
basePort?: number;
|
|
64
|
+
/** Maximum retries for port allocation (default: 20) */
|
|
65
|
+
maxRetries?: number;
|
|
66
|
+
/** Timeout in ms for container startup (default: 30000) */
|
|
67
|
+
startupTimeout?: number;
|
|
68
|
+
/** Whether to cleanup containers on exit (default: true) */
|
|
69
|
+
cleanup?: boolean;
|
|
70
|
+
/** Enable verbose logging (default: false) */
|
|
71
|
+
verbose?: boolean;
|
|
72
|
+
/** Default network for containers */
|
|
73
|
+
network?: string;
|
|
74
|
+
/** Timeout in ms for graceful shutdown (default: 10000) */
|
|
75
|
+
gracefulShutdownTimeout?: number;
|
|
76
|
+
/** Maximum retries for container cleanup (default: 3) */
|
|
77
|
+
maxCleanupRetries?: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Options for creating a Docker container
|
|
82
|
+
*/
|
|
83
|
+
export interface ContainerOptions {
|
|
84
|
+
/** Container name (auto-generated if not provided) */
|
|
85
|
+
name?: string;
|
|
86
|
+
/** Docker image to use */
|
|
87
|
+
image: string;
|
|
88
|
+
/** Command to run in container */
|
|
89
|
+
command?: string;
|
|
90
|
+
/** Port mappings: container port -> host port or 'auto' */
|
|
91
|
+
ports?: Record<number, number | 'auto'>;
|
|
92
|
+
/** Environment variables */
|
|
93
|
+
environment?: Record<string, string>;
|
|
94
|
+
/** Docker labels */
|
|
95
|
+
labels?: Record<string, string>;
|
|
96
|
+
/** Volume mounts (format: "host:container" or "volume:container") */
|
|
97
|
+
volumes?: string[];
|
|
98
|
+
/** Networks to connect to */
|
|
99
|
+
networks?: string[];
|
|
100
|
+
/** Health check configuration */
|
|
101
|
+
healthcheck?: {
|
|
102
|
+
/** Health check command (array format: ['CMD', 'arg1', ...] or ['CMD-SHELL', 'command']) */
|
|
103
|
+
test: string[];
|
|
104
|
+
/** Interval between checks (e.g., "1s") */
|
|
105
|
+
interval?: string;
|
|
106
|
+
/** Timeout for each check (e.g., "3s") */
|
|
107
|
+
timeout?: string;
|
|
108
|
+
/** Number of retries before unhealthy */
|
|
109
|
+
retries?: number;
|
|
110
|
+
/** Start period before checks begin (e.g., "2s") */
|
|
111
|
+
startPeriod?: string;
|
|
112
|
+
};
|
|
113
|
+
/** Wait conditions before considering container ready */
|
|
114
|
+
waitFor?: {
|
|
115
|
+
/** Wait for specific port to be listening */
|
|
116
|
+
port?: number;
|
|
117
|
+
/** Timeout in ms for wait conditions (default: uses startupTimeout) */
|
|
118
|
+
timeout?: number;
|
|
119
|
+
/** Wait for health check to pass */
|
|
120
|
+
healthcheck?: boolean;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Options for creating a standalone Redis container
|
|
126
|
+
*/
|
|
127
|
+
export interface RedisContainerOptions {
|
|
128
|
+
/** Container name (auto-generated if not provided) */
|
|
129
|
+
name?: string;
|
|
130
|
+
/** Host port or 'auto' for dynamic allocation */
|
|
131
|
+
port?: number | 'auto';
|
|
132
|
+
/** Redis password (if requirePass is true) */
|
|
133
|
+
password?: string;
|
|
134
|
+
/** Redis database number (default: 0) */
|
|
135
|
+
database?: number;
|
|
136
|
+
/** Maximum memory limit (default: "256mb") */
|
|
137
|
+
maxMemory?: string;
|
|
138
|
+
/** Require password authentication (default: true if password is set) */
|
|
139
|
+
requirePass?: boolean;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Options for creating a Redis Cluster
|
|
144
|
+
*/
|
|
145
|
+
export interface RedisClusterOptions {
|
|
146
|
+
/** Number of master nodes (default: 3) */
|
|
147
|
+
masterCount?: number;
|
|
148
|
+
/** Number of replicas per master (default: 1) */
|
|
149
|
+
replicasPerMaster?: number;
|
|
150
|
+
/** Base port for first node (deprecated - uses dynamic allocation) */
|
|
151
|
+
basePort?: number;
|
|
152
|
+
/** Redis password for all nodes */
|
|
153
|
+
password?: string;
|
|
154
|
+
/** Network name (auto-generated if not provided) */
|
|
155
|
+
network?: string;
|
|
156
|
+
/** Timeout in ms for cluster to become ready (default: 120000) */
|
|
157
|
+
readyTimeout?: number;
|
|
158
|
+
/** Pre-initialization delay in ms after containers start (default: 5000) */
|
|
159
|
+
preInitDelay?: number;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Represents a Redis Cluster with all its components
|
|
164
|
+
*/
|
|
165
|
+
export interface RedisClusterContainers {
|
|
166
|
+
/** Master node containers */
|
|
167
|
+
masters: DockerContainer[];
|
|
168
|
+
/** Replica node containers */
|
|
169
|
+
replicas: DockerContainer[];
|
|
170
|
+
/** Docker network name */
|
|
171
|
+
network: string;
|
|
172
|
+
/** List of all nodes with their host:port */
|
|
173
|
+
nodes: Array<{ host: string; port: number }>;
|
|
174
|
+
/** NAT mapping for cluster connectivity (internal IP:port -> external host:port) */
|
|
175
|
+
natMap?: Record<string, { host: string; port: number }>;
|
|
176
|
+
/** Clean up all cluster containers and network */
|
|
177
|
+
cleanup: () => Promise<void>;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Options for creating a Redis Sentinel setup
|
|
182
|
+
*/
|
|
183
|
+
export interface RedisSentinelOptions {
|
|
184
|
+
/** Sentinel master name (default: "mymaster") */
|
|
185
|
+
masterName?: string;
|
|
186
|
+
/** Number of replica nodes (default: 2) */
|
|
187
|
+
replicaCount?: number;
|
|
188
|
+
/** Number of sentinel nodes (default: 3) */
|
|
189
|
+
sentinelCount?: number;
|
|
190
|
+
/** Base port for first node (deprecated - uses dynamic allocation) */
|
|
191
|
+
basePort?: number;
|
|
192
|
+
/** Redis password for all nodes */
|
|
193
|
+
password?: string;
|
|
194
|
+
/** Network name (auto-generated if not provided) */
|
|
195
|
+
network?: string;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Represents a Redis Sentinel setup with all its components
|
|
200
|
+
*/
|
|
201
|
+
export interface RedisSentinelContainers {
|
|
202
|
+
/** Master node container */
|
|
203
|
+
master: DockerContainer;
|
|
204
|
+
/** Replica node containers */
|
|
205
|
+
replicas: DockerContainer[];
|
|
206
|
+
/** Sentinel node containers */
|
|
207
|
+
sentinels: DockerContainer[];
|
|
208
|
+
/** Docker network name */
|
|
209
|
+
network: string;
|
|
210
|
+
/** Sentinel master name */
|
|
211
|
+
masterName: string;
|
|
212
|
+
/** Sentinel ports for connecting */
|
|
213
|
+
sentinelPorts: number[];
|
|
214
|
+
/** Clean up all containers and network */
|
|
215
|
+
cleanup: () => Promise<void>;
|
|
216
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic testing error utilities
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export class TestingError extends Error {
|
|
6
|
+
constructor(message: string, public code: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'TestingError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class TimeoutError extends TestingError {
|
|
13
|
+
constructor(operation: string, timeout: number) {
|
|
14
|
+
super(`Timeout waiting for: ${operation} (${timeout}ms)`, 'TIMEOUT');
|
|
15
|
+
this.name = 'TimeoutError';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class NotFoundError extends TestingError {
|
|
20
|
+
constructor(entity: string, identifier?: string) {
|
|
21
|
+
super(`Not found: ${entity}${identifier ? ` (${identifier})` : ''}`, 'NOT_FOUND');
|
|
22
|
+
this.name = 'NotFoundError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class RetryError extends TestingError {
|
|
27
|
+
constructor(message: string, public attempts: number, public lastError?: Error) {
|
|
28
|
+
super(message, 'RETRY_EXHAUSTED');
|
|
29
|
+
this.name = 'RetryError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test Assertions and Fixtures
|
|
3
|
+
*
|
|
4
|
+
* Custom assertion utilities and fixture management for tests
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Assert that a promise rejects with a specific error
|
|
9
|
+
*
|
|
10
|
+
* Verifies that a promise rejects and optionally checks the error message
|
|
11
|
+
* or error type.
|
|
12
|
+
*
|
|
13
|
+
* @param promise - The promise that should reject
|
|
14
|
+
* @param expectedError - Optional error matcher (string, regex, or Error class)
|
|
15
|
+
* @throws Error if the promise resolves or error doesn't match
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // Assert rejection with any error
|
|
20
|
+
* await assertRejects(Promise.reject(new Error('fail')));
|
|
21
|
+
*
|
|
22
|
+
* // Assert rejection with specific message substring
|
|
23
|
+
* await assertRejects(
|
|
24
|
+
* Promise.reject(new Error('File not found')),
|
|
25
|
+
* 'not found'
|
|
26
|
+
* );
|
|
27
|
+
*
|
|
28
|
+
* // Assert rejection with regex pattern
|
|
29
|
+
* await assertRejects(
|
|
30
|
+
* Promise.reject(new Error('Invalid input: 123')),
|
|
31
|
+
* /Invalid input:/
|
|
32
|
+
* );
|
|
33
|
+
*
|
|
34
|
+
* // Assert rejection with specific error type
|
|
35
|
+
* await assertRejects(
|
|
36
|
+
* Promise.reject(new TypeError('wrong type')),
|
|
37
|
+
* TypeError
|
|
38
|
+
* );
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export async function assertRejects(
|
|
42
|
+
promise: Promise<any>,
|
|
43
|
+
expectedError?: string | RegExp | typeof Error
|
|
44
|
+
): Promise<void> {
|
|
45
|
+
try {
|
|
46
|
+
await promise;
|
|
47
|
+
throw new Error('Expected promise to reject');
|
|
48
|
+
} catch (error: any) {
|
|
49
|
+
if (expectedError) {
|
|
50
|
+
if (typeof expectedError === 'string') {
|
|
51
|
+
if (!error.message.includes(expectedError)) {
|
|
52
|
+
throw new Error(`Expected error to include "${expectedError}", got: ${error.message}`);
|
|
53
|
+
}
|
|
54
|
+
} else if (expectedError instanceof RegExp) {
|
|
55
|
+
if (!expectedError.test(error.message)) {
|
|
56
|
+
throw new Error(`Expected error to match ${expectedError}, got: ${error.message}`);
|
|
57
|
+
}
|
|
58
|
+
} else if (typeof expectedError === 'function') {
|
|
59
|
+
if (!(error instanceof expectedError)) {
|
|
60
|
+
throw new Error(`Expected error to be instance of ${expectedError.name}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Test fixture interface
|
|
69
|
+
*
|
|
70
|
+
* Defines the structure for a test fixture with setup and optional teardown.
|
|
71
|
+
*
|
|
72
|
+
* @template T - The type of the fixture instance
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* interface DatabaseFixture {
|
|
77
|
+
* connection: Connection;
|
|
78
|
+
* cleanup: () => Promise<void>;
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* const dbFixture: TestFixture<DatabaseFixture> = {
|
|
82
|
+
* setup: async () => {
|
|
83
|
+
* const connection = await createConnection();
|
|
84
|
+
* return {
|
|
85
|
+
* connection,
|
|
86
|
+
* cleanup: async () => await connection.close()
|
|
87
|
+
* };
|
|
88
|
+
* },
|
|
89
|
+
* teardown: async (fixture) => {
|
|
90
|
+
* await fixture.cleanup();
|
|
91
|
+
* }
|
|
92
|
+
* };
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export interface TestFixture<T> {
|
|
96
|
+
/**
|
|
97
|
+
* Setup function that creates the fixture instance
|
|
98
|
+
*/
|
|
99
|
+
setup: () => T | Promise<T>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Optional teardown function that cleans up the fixture
|
|
103
|
+
*/
|
|
104
|
+
teardown?: (fixture: T) => void | Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Execute a test with a fixture
|
|
109
|
+
*
|
|
110
|
+
* Manages the lifecycle of a test fixture by calling setup before the test
|
|
111
|
+
* and teardown after the test, ensuring cleanup even if the test fails.
|
|
112
|
+
*
|
|
113
|
+
* @template T - The type of the fixture instance
|
|
114
|
+
* @template R - The return type of the test function
|
|
115
|
+
* @param fixture - The test fixture configuration
|
|
116
|
+
* @param fn - The test function to execute with the fixture
|
|
117
|
+
* @returns The result of the test function
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const tempDirFixture: TestFixture<string> = {
|
|
122
|
+
* setup: () => createTempDir('test-'),
|
|
123
|
+
* teardown: (dir) => cleanupTempDir(dir)
|
|
124
|
+
* };
|
|
125
|
+
*
|
|
126
|
+
* it('should use temp directory', async () => {
|
|
127
|
+
* await withFixture(tempDirFixture, async (tmpDir) => {
|
|
128
|
+
* // Use tmpDir...
|
|
129
|
+
* const file = path.join(tmpDir, 'test.txt');
|
|
130
|
+
* await fs.promises.writeFile(file, 'content');
|
|
131
|
+
* // Cleanup happens automatically
|
|
132
|
+
* });
|
|
133
|
+
* });
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const dbFixture: TestFixture<Database> = {
|
|
139
|
+
* setup: async () => {
|
|
140
|
+
* const db = await Database.connect();
|
|
141
|
+
* await db.migrate();
|
|
142
|
+
* return db;
|
|
143
|
+
* },
|
|
144
|
+
* teardown: async (db) => {
|
|
145
|
+
* await db.rollback();
|
|
146
|
+
* await db.disconnect();
|
|
147
|
+
* }
|
|
148
|
+
* };
|
|
149
|
+
*
|
|
150
|
+
* it('should interact with database', async () => {
|
|
151
|
+
* await withFixture(dbFixture, async (db) => {
|
|
152
|
+
* const user = await db.users.create({ name: 'Test' });
|
|
153
|
+
* expect(user.id).toBeDefined();
|
|
154
|
+
* // Teardown runs even if expect fails
|
|
155
|
+
* });
|
|
156
|
+
* });
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export async function withFixture<T, R>(
|
|
160
|
+
fixture: TestFixture<T>,
|
|
161
|
+
fn: (fixture: T) => R | Promise<R>
|
|
162
|
+
): Promise<R> {
|
|
163
|
+
const instance = await fixture.setup();
|
|
164
|
+
try {
|
|
165
|
+
return await fn(instance);
|
|
166
|
+
} finally {
|
|
167
|
+
if (fixture.teardown) {
|
|
168
|
+
await fixture.teardown(instance);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Console Output Suppression
|
|
3
|
+
*
|
|
4
|
+
* Utilities for suppressing console output during tests
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Suppress console output during tests
|
|
9
|
+
*
|
|
10
|
+
* Temporarily replaces console methods with no-op functions to prevent
|
|
11
|
+
* noise during test execution. Returns a restore function to restore
|
|
12
|
+
* the original console methods.
|
|
13
|
+
*
|
|
14
|
+
* @returns Restore function that restores the original console methods
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* describe('My Test Suite', () => {
|
|
19
|
+
* let restore: () => void;
|
|
20
|
+
*
|
|
21
|
+
* beforeEach(() => {
|
|
22
|
+
* restore = suppressConsole();
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* afterEach(() => {
|
|
26
|
+
* restore();
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* it('should not log anything', () => {
|
|
30
|
+
* console.log('This will not appear');
|
|
31
|
+
* // Test logic...
|
|
32
|
+
* });
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* // Using in a single test
|
|
39
|
+
* it('should suppress console', () => {
|
|
40
|
+
* const restore = suppressConsole();
|
|
41
|
+
* try {
|
|
42
|
+
* console.log('Hidden');
|
|
43
|
+
* // Test logic...
|
|
44
|
+
* } finally {
|
|
45
|
+
* restore();
|
|
46
|
+
* }
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export function suppressConsole(): () => void {
|
|
51
|
+
const originalConsole = {
|
|
52
|
+
log: console.log,
|
|
53
|
+
warn: console.warn,
|
|
54
|
+
error: console.error,
|
|
55
|
+
debug: console.debug,
|
|
56
|
+
info: console.info,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// Use noop functions to avoid test framework dependencies
|
|
60
|
+
const noop = () => {};
|
|
61
|
+
console.log = noop;
|
|
62
|
+
console.warn = noop;
|
|
63
|
+
console.error = noop;
|
|
64
|
+
console.debug = noop;
|
|
65
|
+
console.info = noop;
|
|
66
|
+
|
|
67
|
+
return () => {
|
|
68
|
+
console.log = originalConsole.log;
|
|
69
|
+
console.warn = originalConsole.warn;
|
|
70
|
+
console.error = originalConsole.error;
|
|
71
|
+
console.debug = originalConsole.debug;
|
|
72
|
+
console.info = originalConsole.info;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test Helper Utilities
|
|
3
|
+
*
|
|
4
|
+
* Common utilities for testing including temporary directory management,
|
|
5
|
+
* console suppression, assertions, and fixture management.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Temporary directory management
|
|
9
|
+
export { createTempDir, cleanupTempDir } from './temp-dir.js';
|
|
10
|
+
|
|
11
|
+
// Console suppression
|
|
12
|
+
export { suppressConsole } from './console.js';
|
|
13
|
+
|
|
14
|
+
// Assertions and fixtures
|
|
15
|
+
export { assertRejects, type TestFixture, withFixture } from './assertions.js';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Temporary Directory Management
|
|
3
|
+
*
|
|
4
|
+
* Utilities for creating and cleaning up temporary directories during tests
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as fs from 'node:fs';
|
|
8
|
+
import * as path from 'node:path';
|
|
9
|
+
import * as os from 'node:os';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Create a temporary directory for tests
|
|
13
|
+
*
|
|
14
|
+
* Creates a unique temporary directory with the specified prefix.
|
|
15
|
+
* The directory is created in the system's temp directory.
|
|
16
|
+
*
|
|
17
|
+
* @param prefix - Prefix for the temporary directory name (default: 'test-')
|
|
18
|
+
* @returns Absolute path to the created temporary directory
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const tmpDir = createTempDir('my-test-');
|
|
23
|
+
* // Returns: /tmp/my-test-abc123
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function createTempDir(prefix = 'test-'): string {
|
|
27
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
|
28
|
+
return tempDir;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Clean up temporary directory
|
|
33
|
+
*
|
|
34
|
+
* Recursively removes a directory and all its contents.
|
|
35
|
+
* Safe to call even if the directory doesn't exist.
|
|
36
|
+
*
|
|
37
|
+
* @param dir - Absolute path to the directory to remove
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const tmpDir = createTempDir();
|
|
42
|
+
* // ... use tmpDir ...
|
|
43
|
+
* cleanupTempDir(tmpDir); // Clean up after test
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function cleanupTempDir(dir: string): void {
|
|
47
|
+
if (fs.existsSync(dir)) {
|
|
48
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performance testing utilities.
|
|
3
|
+
*
|
|
4
|
+
* This module provides tools for measuring performance and detecting memory leaks
|
|
5
|
+
* in tests.
|
|
6
|
+
*
|
|
7
|
+
* @module @omnitron-dev/testing/performance
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export { MemoryLeakDetector } from './memory-detector.js';
|
|
11
|
+
export { PerfTimer } from './perf-timer.js';
|