Profile the Slowest Tests with Reporters
Rank tests by duration with a reporter so optimising the few slowest cases shortens the whole suite the most.
Task
Identify and optimise the slowest tests based on reporter output.
When to use this
The suite is slow but nobody knows which handful of tests dominate the runtime.
Prerequisites
- A test reporter that emits durations
- A suite with mixed test costs
- CI logs to parse
Steps
1. Emit per-test durations as machine-readable output.
npx vitest run --reporter=json --outputFile=results.json
jq -r '.testResults[].assertionResults[] | "\(.duration) \(.title)"' results.json | sort -rn | head -10Expected output: The ten slowest tests and their durations are listed.
2. Optimise the top offender and re-measure.
npx vitest run --reporter=json --outputFile=after.json
jq -r '.testResults[] | "\(.duration) \(.name)"' after.json | sort -rn | head -3Expected output: The optimised test drops down the ranking, confirming the change helped.
Troubleshooting
Problem
Durations vary run to run
Solution
Compare medians across several runs and ignore one-off outliers.
Problem
Setup dominates
Solution
Hoist shared setup or reuse fixtures instead of repeating expensive initialisation.
Limitations
- Profiling measures wall time, not CPU cost.
- CI hardware differs from local machines.
- Over-optimising tests can reduce their value.
What this recipe does
Identify and optimise the slowest tests based on reporter output.
The suite is slow but nobody knows which handful of tests dominate the runtime.
Prerequisites
- A test reporter that emits durations
- A suite with mixed test costs
- CI logs to parse
Procedure
- Emit per-test durations as machine-readable output.
npx vitest run --reporter=json --outputFile=results.json
jq -r '.testResults[].assertionResults[] | "\(.duration) \(.title)"' results.json | sort -rn | head -10
Expected result: The ten slowest tests and their durations are listed.
- Optimise the top offender and re-measure.
npx vitest run --reporter=json --outputFile=after.json
jq -r '.testResults[] | "\(.duration) \(.name)"' after.json | sort -rn | head -3
Expected result: The optimised test drops down the ranking, confirming the change helped.
How to confirm it worked
Run the commands above and confirm every expected result in order. If a step does not produce the stated output, use the troubleshooting notes before continuing. A passing run shows the slowest tests are identified and after changes the suite wall time drops measurably.
Troubleshooting
Durations vary run to run — Compare medians across several runs and ignore one-off outliers.
Setup dominates — Hoist shared setup or reuse fixtures instead of repeating expensive initialisation.
Limitations
- Profiling measures wall time, not CPU cost.
- CI hardware differs from local machines.
- Over-optimising tests can reduce their value.