vitest-dev/vitest · error · TypeError

`bench.from()` expects a string path or a function…

Error message

`bench.from()` expects a string path or a function returning the result data as its second argument.

What it means

The second argument to `bench.from(name, source)` must be either a string path (resolved against project root with `${projectName}` substitution) or a function returning baseline data. This `TypeError` fires when `source` is any other type. The guard is at benchmark.ts:439-441.

Solutions

  1. Pass a path string: `bench.from('baseline', './results/baseline.json')`.
  2. Or pass a function returning the data: `bench.from('baseline', () => baselineData)`.
  3. If you have a raw object, wrap it: `bench.from('baseline', () => ({ latency, throughput, period, totalTime }))`.

Example fix

// before
bench.from('baseline', rawDataObject)

// after
bench.from('baseline', () => rawDataObject)
Defensive patterns

Strategy: validation

Validate before calling

if (typeof source !== 'string' && typeof source !== 'function') throw new TypeError('source must be string path or function')

Type guard

function isFromSource(v: unknown): v is string | (() => any) { return typeof v === 'string' || typeof v === 'function' }

Try / catch

null

Prevention

When it happens

Trigger: Calling `bench.from('name', 123)`, `bench.from('name', null)`, `bench.from('name', { latency: ... })` (object instead of a function), or `bench.from('name', undefined)`. Only `string` or `function` are accepted.

Common situations: Passing the baseline data object directly instead of a function returning it; passing a registration object; refactor removing the source argument.

Related errors


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

Appendix: source

Thrown at packages/vitest/src/runtime/benchmark.ts:440

      },
    }
    if (perProject) {
      registration[kPerProject] = true
    }
    if (writeResult) {
      registration[kWriteResult] = writeResult
    }
    pending.add(registration)
    return registration
  }

  bench.from = <Name extends string>(nameOrFunction: Name | Function, source: string | BenchFromSource): BenchRegistration<Name> => {
    validateBenchmarkProject(config)
    if (typeof nameOrFunction !== 'string' && typeof nameOrFunction !== 'function') {
      throw new TypeError('`bench.from()` requires a name (string or named function) as its first argument.')
    }
    if (typeof source !== 'string' && typeof source !== 'function') {
      throw new TypeError('`bench.from()` expects a string path or a function returning the result data as its second argument.')
    }
    const name = (typeof nameOrFunction === 'function' ? nameOrFunction.name || '<anonymous>' : nameOrFunction) as Name
    const registration: FromRegistration<Name> = {
      [kRegistration]: true,
      [kFromSource]: source,
      name,
      run: () => {
        pending.delete(registration)
        return runFrom(name, source)
      },
    }
    pending.add(registration)
    return registration
  }

  bench.compare = async (...args) => {
    validateBenchmarkProject(config)

View on GitHub (pinned to 1fa9837ec2)