vitest-dev/vitest · error · TypeError

`bench.from()` requires a name (string or named function) as

Error message

`bench.from()` requires a name (string or named function) as its first argument.

What it means

`bench.from(name, source)` loads a benchmark result from a stored baseline (a file path or a factory function) instead of running it live. The first argument identifies the benchmark and must be a string name or a named function (its `.name` is used). This `TypeError` is thrown at registration time, before any baseline data is read, to fail fast on malformed calls. See `benchmark.ts:437`.

Source

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

      run: (options?: BenchRunOptions) => {
        pending.delete(registration)
        return runSingle(name, fn, fnOpts, options, meta, writeResult)
      },
    }
    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
  }

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Pass a descriptive string name as the first argument: `bench.from('my-bench', source)`.
  2. Or pass a named function whose `.name` becomes the benchmark name: `bench.from(function myBench() {}, source)`.
  3. If you only have a path and no name intent, add an explicit string label so the comparison table is readable.

Example fix

// before
bench.from('/results/sort.bench.json')

// after
bench.from('sort', '/results/sort.bench.json')
Defensive patterns

Strategy: type-guard

Validate before calling

// Before calling bench.from, validate the name argument
function validBenchName(name: unknown): name is string | Function {
  return typeof name === 'string' || typeof name === 'function'
}
if (!validBenchName(nameArg)) {
  throw new Error('bench.from needs a string or named function name')
}
const reg = bench.from(nameArg as any, source)

Type guard

function isBenchName(v: unknown): v is string | Function {
  return typeof v === 'string' || typeof v === 'function'
}

Try / catch

try {
  const reg = bench.from(name, source)
} catch (e) {
  if (e instanceof TypeError && /requires a name/.test(e.message)) {
    // fix the name argument and retry
  }
  throw e
}

Prevention

When it happens

Trigger: Calling `bench.from()` with zero arguments; passing a number, boolean, object, or `undefined` as the first argument; passing only the source path and omitting the name; destructuring incorrectly so `undefined` is passed.

Common situations: Refactoring a `bench()` call into `bench.from()` and forgetting the name slot; passing the JSON result path as the first argument by mistake; copy-paste from `bench(name, fn)` leaving the name off when switching to `.from`.

Related errors


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