vitest-dev/vitest · error · TypeError

expects the expected value to be a benchmark result.

Error message

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

What it means

Same .toBeFasterThan matcher, but this check fires when the actual side passed isBenchResult and the expected (second argument) did not. The expected value must also be a BenchResult with latency.mean so the matcher can compute the speed threshold.

Solutions

  1. Pass another full BenchResult (from a different bench() task) as the second argument.
  2. If you want a percentage threshold, use the options.delta parameter, not a raw number as the second arg.
  3. Verify both arguments are objects with `latency.mean` numbers before calling the matcher.

Example fix

// before — second arg is a raw number:
expect(result).toBeFasterThan(100)

// after — second arg is a bench result, delta in options:
expect(result).toBeFasterThan(baseline, { delta: 0.1 })
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(benchResult).toBeFasterThan(notABenchResult) where the second argument is a raw number, string, or object without the latency structure.

Common situations: Passing a threshold number as the second argument instead of another bench result; comparing against a stale or partial object; misunderstanding that both sides must be bench results.

Related errors


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

Appendix: source

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

  )
}

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`
          + `Received: ${RECEIVED_COLOR(formatOps(actual.throughput.mean))} ops/sec\n`
          + `Expected: ${EXPECTED_COLOR(formatOps(expected.throughput.mean))} ops/sec\n`
          : `${matcherHint('.toBeFasterThan')}\n\nExpected to be faster${delta > 0 ? ` by at least ${(delta * 100).toFixed(0)}%` : ''}, but was ${Number(relation) > 0 ? `${relation}% slower` : `only ${Math.abs(Number(relation))}% faster`}.\n\n`
            + `Received: ${RECEIVED_COLOR(formatOps(actual.throughput.mean))} ops/sec\n`
            + `Expected: ${EXPECTED_COLOR(formatOps(expected.throughput.mean))} ops/sec\n`

View on GitHub (pinned to 1fa9837ec2)