vitest-dev/vitest · error · TypeError

expects the expected value to be a benchmark result.

Error message

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

What it means

Same .toBeSlowerThan matcher, second-argument check. Fires when the actual is a valid BenchResult but the expected (second argument) is not. The matcher needs expected.latency.mean to compute the slowness threshold.

Solutions

  1. Pass a full BenchResult as the second argument.
  2. Use options.delta for a percentage-based threshold rather than a raw number.
  3. Validate the second argument has `latency.mean` before calling the matcher.

Example fix

// before:
expect(result).toBeSlowerThan(100)

// after:
expect(result).toBeSlowerThan(baselineResult, { 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).toBeSlowerThan(notABenchResult) where the second argument lacks the latency structure.

Common situations: Passing a raw number or plain object as the expected baseline instead of a bench result; misunderstanding that both arguments must be BenchResult objects.

Related errors


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

Appendix: source

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

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