vitest-dev/vitest · error · TypeError

expects the actual value to be a benchmark result.

Error message

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

What it means

The .toBeSlowerThan benchmark matcher is the inverse of toBeFasterThan. It validates that the actual (received) value is a BenchResult (object with latency.mean number). If the actual side fails isBenchResult, this TypeError fires before any comparison is attempted.

Solutions

  1. Ensure the actual value is the BenchResult object from a bench() task.
  2. Use toBeGreaterThan for raw numeric comparisons instead of the bench matcher.
  3. Confirm the object shape has `latency.mean` as a number before asserting.

Example fix

// before:
expect(200).toBeSlowerThan(baseline)

// after:
expect(myBenchResult).toBeSlowerThan(baselineBenchResult)
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).toBeSlowerThan(other) where `value` is not a bench result — a raw number, string, or object without the latency structure.

Common situations: Using toBeSlowerThan outside of a bench() context; passing the wrong variable as actual; comparing raw timing values.

Related errors


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

Appendix: source

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

      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`
      },
    }
  },

  toBeSlowerThan(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('.toBeSlowerThan')} expects the actual value to be a benchmark result.`,
      )
    }
    if (!isBenchResult(expected)) {
      throw new TypeError(
        `${matcherHint('.toBeSlowerThan')} 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.toBeSlowerThan')}\n\nExpected to not be slower, but was ${relation}% slower.\n\n`

View on GitHub (pinned to 1fa9837ec2)