-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-setup.ts
More file actions
324 lines (290 loc) · 8 KB
/
Copy pathtest-setup.ts
File metadata and controls
324 lines (290 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* @fileoverview Shared test setup for APEX monorepo
*
* This file provides common test setup utilities that can be imported
* by individual packages or used in global test setup files.
*
* Usage in package test setup:
* ```typescript
* // In packages/[package]/src/__tests__/setup.ts
* import { setupGlobalTestEnvironment } from '../../../test-setup.js';
* setupGlobalTestEnvironment();
* ```
*
* Usage in vitest config:
* ```typescript
* export default defineConfig({
* test: {
* setupFiles: ['../../test-setup.ts'],
* // ...other config
* },
* });
* ```
*/
import { vi, beforeEach, afterEach } from 'vitest';
import { expect } from 'vitest';
/**
* Global test environment setup options
*/
export interface TestEnvironmentOptions {
/** Setup console mocking (default: true) */
setupConsole?: boolean;
/** Setup common mocks (default: true) */
setupMocks?: boolean;
/** Setup test timeout helpers (default: true) */
setupTimeouts?: boolean;
/** Setup async test utilities (default: true) */
setupAsync?: boolean;
/** Global test timeout in ms (default: 5000) */
globalTimeout?: number;
}
/**
* Sets up the global test environment for APEX tests
*/
export function setupGlobalTestEnvironment(options: TestEnvironmentOptions = {}) {
const {
setupConsole = true,
setupMocks = true,
setupTimeouts = true,
setupAsync = true,
globalTimeout = 5000,
} = options;
if (setupConsole) {
setupConsoleHelpers();
}
if (setupMocks) {
setupCommonMocks();
}
if (setupTimeouts) {
setupTimeoutHelpers(globalTimeout);
}
if (setupAsync) {
setupAsyncUtilities();
}
}
/**
* Sets up console helpers for testing
* - Provides console.mockImplementation for controlled console output
* - Automatically restores console after each test
*/
export function setupConsoleHelpers() {
let originalConsole: typeof console;
beforeEach(() => {
originalConsole = { ...console };
vi.spyOn(console, 'log').mockImplementation(() => {});
vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});
vi.spyOn(console, 'info').mockImplementation(() => {});
vi.spyOn(console, 'debug').mockImplementation(() => {});
});
afterEach(() => {
vi.restoreAllMocks();
});
}
/**
* Sets up common mocks used across tests
* - Process environment variables
* - Date mocking utilities
* - Random number generation
*/
export function setupCommonMocks() {
beforeEach(() => {
// Mock environment variables
vi.stubEnv('NODE_ENV', 'test');
vi.stubEnv('APEX_TEST_MODE', 'unit');
// Ensure consistent test environment
vi.stubEnv('CI', '');
vi.stubEnv('TZ', 'UTC');
});
afterEach(() => {
vi.unstubAllEnvs();
vi.useRealTimers();
});
}
/**
* Sets up timeout helpers for async tests
*/
export function setupTimeoutHelpers(defaultTimeout: number = 5000) {
// Make timeout helpers available globally
globalThis.testTimeout = defaultTimeout;
// Setup test timeout wrapper
globalThis.withTimeout = async function<T>(
promise: Promise<T>,
timeout: number = defaultTimeout
): Promise<T> {
return Promise.race([
promise,
new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error(`Test timeout after ${timeout}ms`)), timeout)
)
]);
};
}
/**
* Sets up async test utilities
* - Provides flushPromises() for awaiting microtasks
* - Provides nextTick() for deferring execution
* - Provides sleep() for controlled delays in tests
*/
export function setupAsyncUtilities() {
// Flush all pending promises/microtasks
globalThis.flushPromises = async function(): Promise<void> {
await new Promise(resolve => setImmediate(resolve));
};
// Next tick utility
globalThis.nextTick = async function(): Promise<void> {
await new Promise(resolve => process.nextTick(resolve));
};
// Sleep utility for tests
globalThis.sleep = function(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
};
// Wait for condition utility
globalThis.waitFor = async function(
condition: () => boolean | Promise<boolean>,
options: { timeout?: number; interval?: number } = {}
): Promise<void> {
const { timeout = 5000, interval = 50 } = options;
const start = Date.now();
while (Date.now() - start < timeout) {
if (await condition()) {
return;
}
await new Promise(resolve => setTimeout(resolve, interval));
}
throw new Error(`waitFor condition not met within ${timeout}ms`);
};
}
/**
* Creates a test factory for consistent test structure
*/
export function createTestSuite(name: string, setupFn?: () => void | Promise<void>) {
return {
describe: (description: string, tests: () => void) => {
describe(`${name}: ${description}`, () => {
if (setupFn) {
beforeEach(setupFn);
}
tests();
});
}
};
}
/**
* Test data factories
*/
export const testFactories = {
/**
* Creates a test ID with consistent format
*/
createTestId: (prefix: string = 'test'): string => {
return `${prefix}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
},
/**
* Creates test dates for consistent temporal testing
*/
createTestDate: (offset: number = 0): Date => {
return new Date('2023-01-01T00:00:00.000Z').getTime() + offset;
},
/**
* Creates test file paths for consistent path testing
*/
createTestPath: (...segments: string[]): string => {
return `/test/project/${segments.join('/')}`;
},
};
/**
* Assertion helpers
*/
export const assertionHelpers = {
/**
* Asserts that a value is defined (not null or undefined)
*/
assertDefined: <T>(value: T | null | undefined): asserts value is T => {
expect(value).toBeDefined();
expect(value).not.toBeNull();
},
/**
* Asserts that an array has a specific length
*/
assertLength: <T>(array: T[], expectedLength: number): void => {
expect(array).toHaveLength(expectedLength);
},
/**
* Asserts that a promise rejects with a specific error type
*/
assertRejectsWithError: async <T extends Error>(
promise: Promise<any>,
errorType: new (...args: any[]) => T,
message?: string | RegExp
): Promise<void> => {
await expect(promise).rejects.toThrow(errorType);
if (message) {
await expect(promise).rejects.toThrow(message);
}
},
/**
* Asserts that an object has specific properties
*/
assertHasProperties: <T extends Record<string, any>>(
obj: T,
properties: (keyof T)[]
): void => {
for (const prop of properties) {
expect(obj).toHaveProperty(prop);
}
},
};
/**
* Mock helpers for common testing patterns
*/
export const mockHelpers = {
/**
* Creates a mock function with typed return values
*/
createTypedMock: <T extends (...args: any[]) => any>(
implementation?: T
): ReturnType<typeof vi.fn<Parameters<T>, ReturnType<T>>> => {
return vi.fn(implementation);
},
/**
* Creates a partial mock of an object
*/
createPartialMock: <T extends Record<string, any>>(
overrides: Partial<T> = {}
): T => {
return {
...({} as T),
...overrides,
};
},
/**
* Mocks a class constructor
*/
mockClass: <T extends new (...args: any[]) => any>(
ClassConstructor: T,
mockImplementation?: Partial<InstanceType<T>>
) => {
return vi.mocked(ClassConstructor).mockImplementation(
() => mockImplementation as InstanceType<T>
);
},
};
// Type declarations for global utilities
declare global {
var testTimeout: number;
var withTimeout: <T>(promise: Promise<T>, timeout?: number) => Promise<T>;
var flushPromises: () => Promise<void>;
var nextTick: () => Promise<void>;
var sleep: (ms: number) => Promise<void>;
var waitFor: (
condition: () => boolean | Promise<boolean>,
options?: { timeout?: number; interval?: number }
) => Promise<void>;
}
// Export everything for convenience
export {
testFactories as factories,
assertionHelpers as assertions,
mockHelpers as mocks,
};