with-performance.ts
This file
Path
test/helpers/with-performance.ts
30 lines · 905 bytes · TypeScript
Last changed by
Fix flaky testsSindre Sorhusbefore adoptionfec6c43
History of this fileTake it
The walk stopped after 200 commits: lines older than 37fc250 point at it.
Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 1–11why, one step back1import {performance} from 'node:perf_hooks';2import process from 'node:process';3import type {ExecutionContext} from 'ava';45type Argument = {6 expectedDuration: number;7 t: ExecutionContext;8 test: () => Promise<void>;9};1011// We allow the tests to take more time on CI than locally, to reduce flakinessAdd `retryOnTimeout` and `shouldRetry` options (#767)Sindre Sorhusbefore adoptionf0fdbd4
line 12why, one step back12// Node.js 22+ has higher overhead on CI (~400-650ms), so we increase the toleranceAdd retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 13why, one step back13const nodeVersion = Number(process.versions.node.split('.')[0]);Fix flaky testsSindre Sorhusbefore adoptionfec6c43
line 14why, one step back14const allowedOffset = process.env.CI ? (nodeVersion >= 22 ? 700 : 500) : 400;Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 15–30why, one step back1516export async function withPerformance({17 expectedDuration,18 t,19 test,20}: Argument) {21 const startTime = performance.now();22 await test();23 const endTime = performance.now();2425 const duration = endTime - startTime;26 t.true(27 Math.abs(duration - expectedDuration) < allowedOffset,28 `Duration of ${duration}ms is not close to expected duration ${expectedDuration}ms`,29 );30}