vitest-dev/vitest · error · TypeError

expects the actual value to be a benchmark result.

Error message

${matcherHint('.toBeFasterThan')} expects the actual value to be a benchmark result.

What it means

The .toBeFasterThan benchmark matcher requires the actual (received) value to be a BenchResult — an object with a `latency.mean` number, produced by vitest bench(). isBenchResult checks `typeof value === 'object'`, non-null, has 'latency', and latency.mean is a number. If the actual side fails this check, the matcher throws a TypeError immediately.

Solutions

  1. Ensure the actual value comes from a `bench()` task result (the object passed to the benchmark callback's result).
  2. Use a regular numeric matcher (toBeLessThan) if you are comparing raw numbers, not bench results.
  3. Check that both actual and expected are BenchResult objects before calling the matcher.

Example fix

// before:
expect(50).toBeFasterThan(otherResult)

// after — pass the bench result object, not a raw number:
expect(myBenchResult).toBeFasterThan(otherBenchResult)
Defensive patterns

Strategy: type-guard

Type guard

function isBenchResult(v: unknown): v is { latency: { mean: number }; throughput: { mean: number } } {
  return typeof v === 'object' && v !== null
    && 'latency' in v && typeof (v as any).latency?.mean === 'number'
}

Prevention

When it happens

Trigger: Calling expect(value).toBeFasterThan(other) where `value` is not the result of a bench() call — e.g. a plain number, a string, or a raw object lacking the latency structure.

Common situations: Using toBeFasterThan in a regular (non-bench) test; comparing raw timing numbers instead of BenchResult objects; passing the wrong variable as actual.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/e3ddaf36b73f573d. Report an issue: GitHub.

Appendix: source

Thrown at packages/vitest/src/integrations/chai/bench.ts:23

  return (
    typeof value === 'object'
    && value !== null
    && 'latency' in value
    && typeof (value as any).latency?.mean === 'number'
  )
}

function formatOps(ops: number): string {
  return ops.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
}

export const benchMatchers: MatchersObject = {
  toBeFasterThan(actual: unknown, expected: unknown, options?: { delta?: number }) {
    const { matcherHint, RECEIVED_COLOR, EXPECTED_COLOR } = this.utils
    const delta = options?.delta ?? 0

    if (!isBenchResult(actual)) {
      throw new TypeError(
        `${matcherHint('.toBeFasterThan')} expects the actual value to be a benchmark result.`,
      )
    }
    if (!isBenchResult(expected)) {
      throw new TypeError(
        `${matcherHint('.toBeFasterThan')} expects the expected value to be a benchmark result.`,
      )
    }

    const threshold = expected.latency.mean * (1 - delta)
    const pass = actual.latency.mean < threshold

    return {
      pass,
      message: () => {
        const relation = ((actual.latency.mean - expected.latency.mean) / expected.latency.mean * 100).toFixed(2)
        return pass
          ? `${matcherHint('.not.toBeFasterThan')}\n\nExpected to not be faster, but was ${Math.abs(Number(relation))}% faster.\n\n`

View on GitHub (pinned to 1fa9837ec2)