Write a custom scorer
Use createScorer() when a built-in check does not express your application's
requirements. This example compares order IDs after trimming and normalizing case.
Prerequisites
- Complete Getting started and keep its Vitest configuration.
- Use string outputs and expected values for this example. No API key is needed.
Steps
Define the check
Create src/normalized-order-id.ts:
The callback can be synchronous or asynchronous. Return a finite score and any metadata that will help explain the result. Viteval attaches the configured name.
Use the scorer in an evaluation
Replace src/viteval.eval.ts with:
Verification
Both cases pass because the scorer normalizes the extracted ID. Remove
.toUpperCase() from the scorer and run again. The lowercase case now fails.
Restore normalization after the check.
Troubleshooting
The case fails without a numeric score
Save a JSON report to inspect the score and metadata in the dashboard. Return
score: null when your scorer cannot produce a measurement. Null contributes
zero to aggregation, while throwing an error fails the case regardless of its
threshold. Inspect the score metadata or execution errors in a saved report.
References
createScorer()for callback typeswrapScorer()to transform values before an existing check- Inspect results for report setup