Files
xget/test/helpers/index.js
T
xixu-me 81e810ec9a Add type declarations and improve test typings
Introduces src/types.d.ts and test/types.d.ts for Cloudflare Workers and test utilities. Enhances JSDoc comments and type annotations across test files for better type safety and clarity. Updates tsconfig.json to use bundler module resolution and include additional type sources. Minor test logic and assertion improvements for robustness.
2025-12-08 19:57:48 +08:00

89 lines
2.1 KiB
JavaScript

/**
* Test helpers - centralized exports
*/
// Re-export all utilities
export * from './assertions.js';
export * from './generators.js';
export * from './mocks.js';
/**
* Performance test utilities
*/
export class PerformanceTestHelper {
constructor() {
/** @type {Array<{name: string, duration: number, timestamp: number}>} */
this.measurements = [];
}
/**
* Measure execution time of an async function
* @param {Function} fn - Async function to measure
* @param {string} name - Measurement name
* @returns {Promise<any>} Function result
*/
async measure(fn, name = 'operation') {
const start = performance.now();
const result = await fn();
const end = performance.now();
this.measurements.push({
name,
duration: end - start,
timestamp: Date.now()
});
return result;
}
/**
* Get all measurements
* @returns {Array<{name: string, duration: number, timestamp: number}>} Array of measurements
*/
getMeasurements() {
return [...this.measurements];
}
/**
* Get average duration for a specific measurement name
* @param {string} name - Measurement name
* @returns {number} Average duration in milliseconds
*/
getAverageDuration(name) {
const filtered = this.measurements.filter(m => m.name === name);
if (filtered.length === 0) {
return 0;
}
const total = filtered.reduce((sum, m) => sum + m.duration, 0);
return total / filtered.length;
}
/**
* Clear all measurements
*/
clear() {
this.measurements = [];
}
}
/**
* Test timeout helper
* @param {number} ms - Timeout in milliseconds
* @returns {Promise<never>} Promise that rejects after timeout
*/
export function timeout(ms) {
return new Promise((_, reject) => {
setTimeout(() => reject(new Error(`Test timed out after ${ms}ms`)), ms);
});
}
/**
* Wait for a specified amount of time
* @param {number} ms - Time to wait in milliseconds
* @returns {Promise<void>} Promise that resolves after the specified time
*/
export function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}