vitest-dev/vitest · error · TypeError

${matcherHint('.toBeSlowerThan')} expects the actual value t

Error message

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

What it means

Thrown by the toBeSlowerThan benchmark matcher (same file, toBeFasterThan's sibling) when the actual value is not a BenchResult. Identical guard pattern: the matcher needs the actual to be a benchmark result with latency.mean so it can compare against the threshold derived from the expected baseline plus delta.

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 d568f8ce37)

Solutions

  1. Use toBeSlowerThan only inside a bench() task and pass the BenchResult to expect().
  2. For numeric comparisons, use toBeGreaterThan.
  3. Capture the result object from bench() and assert on it: expect(actualBenchResult).toBeSlowerThan(baselineBenchResult).
  4. Confirm you are running the file under the benchmark mode (vitest bench) rather than the default test run.

Example fix

// before
expect(150).toBeSlowerThan(baseline)

// after
expect(actualBenchResult).toBeSlowerThan(baselineBenchResult, { delta: 0.1 })
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: expect(myValue).toBeSlowerThan(baseline) where myValue is a number, string, or non-bench object; using toBeSlowerThan in a regular it() block; passing the wall-clock time instead of the BenchResult.

Common situations: Confusing benchmark vs numeric matchers; refactoring a bench task so expect() no longer receives a BenchResult; copy-pasting toBeFasterThan usage without adjusting for the inverse semantics.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/fbc69cb04949ad01.json. Report an issue: GitHub.