{"id":"22e78e909b445919","repo":"vitest-dev/vitest","slug":"matcherhint-tobeslowerthan-expects-the-expe","errorCode":null,"errorMessage":"${matcherHint('.toBeSlowerThan')} expects the expected value to be a benchmark result.","messagePattern":"(.+?) expects the expected value to be a benchmark result\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/chai/bench.ts","lineNumber":61,"sourceCode":"          + `Expected: ${EXPECTED_COLOR(formatOps(expected.throughput.mean))} ops/sec\\n`\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`\n            + `Received: ${RECEIVED_COLOR(formatOps(actual.throughput.mean))} ops/sec\\n`\n            + `Expected: ${EXPECTED_COLOR(formatOps(expected.throughput.mean))} ops/sec\\n`\n      },\n    }\n  },\n\n  toBeSlowerThan(actual: unknown, expected: unknown, options?: { delta?: number }) {\n    const { matcherHint, RECEIVED_COLOR, EXPECTED_COLOR } = this.utils\n    const delta = options?.delta ?? 0\n\n    if (!isBenchResult(actual)) {\n      throw new TypeError(\n        `${matcherHint('.toBeSlowerThan')} expects the actual value to be a benchmark result.`,\n      )\n    }\n    if (!isBenchResult(expected)) {\n      throw new TypeError(\n        `${matcherHint('.toBeSlowerThan')} expects the expected value to be a benchmark result.`,\n      )\n    }\n\n    const threshold = expected.latency.mean * (1 + delta)\n    const pass = actual.latency.mean > threshold\n\n    return {\n      pass,\n      message: () => {\n        const relation = ((actual.latency.mean - expected.latency.mean) / expected.latency.mean * 100).toFixed(2)\n        return pass\n          ? `${matcherHint('.not.toBeSlowerThan')}\\n\\nExpected to not be slower, but was ${relation}% slower.\\n\\n`\n          + `Received: ${RECEIVED_COLOR(formatOps(actual.throughput.mean))} ops/sec\\n`\n          + `Expected: ${EXPECTED_COLOR(formatOps(expected.throughput.mean))} ops/sec\\n`\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`\n            + `Received: ${RECEIVED_COLOR(formatOps(actual.throughput.mean))} ops/sec\\n`\n            + `Expected: ${EXPECTED_COLOR(formatOps(expected.throughput.mean))} ops/sec\\n`","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/integrations/chai/bench.ts#L43-L79","documentation":"Thrown by the `.toBeSlowerThan` benchmark matcher when the EXPECTED (second) argument is not a valid `BenchResult`. A valid result is a non-null object with a numeric `latency.mean` (produced by `bench()`/`vitest bench`). The matcher cannot compute a threshold without comparable latency data, so it refuses to proceed rather than returning a misleading pass/fail.","triggerScenarios":"Calling `expect(actualResult).toBeSlowerThan(expected)` where `expected` is a plain number, string, raw function, undefined, or any object lacking a numeric `latency.mean`. Both arguments must originate from `bench()` tasks.","commonSituations":"Passing a millisecond number instead of a bench result object (e.g. `.toBeSlowerThan(100)`), comparing against a pre-saved number, or destructuring a result incorrectly so `latency` is undefined. Also happens when comparing a `bench()` result against a result from a different benchmarking library.","solutions":["Pass a full `BenchResult` object as the expected value, produced by another `bench()`/`vitest bench` task.","If you want a numeric latency comparison, write a custom assertion or use `expect(actual.latency.mean).toBeGreaterThan(expectedMs)` instead.","Verify the expected value has `typeof expected.latency.mean === 'number'` before calling the matcher."],"exampleFix":"// before\nexpect(result).toBeSlowerThan(100)\n// after\nconst baseline = await bench('baseline', baselineFn)\nexpect(result).toBeSlowerThan(baseline, { delta: 0.1 })","handlingStrategy":"type-guard","validationCode":"function isBenchResult(v: unknown): v is { latency: { mean: number }; throughput: { mean: number } } {\n  return typeof v === 'object' && v !== null\n    && typeof (v as any).latency?.mean === 'number'\n}","typeGuard":"import { bench } from 'vitest'\n// vitest does not export isBenchResult publicly; use the local shape check above\nconst ok = isBenchResult(expected)\nif (!ok) throw new Error('expected is not a bench result')\nexpect(actual).toBeSlowerThan(expected)","tryCatchPattern":null,"preventionTips":["Always pass objects returned by `bench()` tasks as both arguments to `.toBeFasterThan`/`.toBeSlowerThan`.","Avoid passing raw millisecond numbers; convert numeric comparisons to `.toBeGreaterThan` on `latency.mean`."],"tags":["bench","matcher","type-error","assertion"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}