Files
xget/test/helpers/index.js
xixu-me 07f5f35972 Improve JSDoc types and add eslint-plugin-jsdoc
Standardized JSDoc type annotations across the codebase, replacing 'Object' with 'object' and refining function signatures for better type safety. Added and configured eslint-plugin-jsdoc for enhanced documentation linting, and updated ESLint settings to include adapters and new JSDoc rules. Updated dependencies to include eslint-plugin-jsdoc and related packages.
2025-12-11 13:27:26 +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 {() => Promise<unknown>} fn - Async function to measure
* @param {string} name - Measurement name
* @returns {Promise<unknown>} 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));
}