vitest-dev/vitest · error · TypeError

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

Error message

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

What it means

`bench.from(nameOrFunction, source)` requires its first argument to be a string or a function (the function's `.name` is used as the benchmark name). This `TypeError` fires when the first argument is any other type — number, object, null, etc. The check is at benchmark.ts:436-438.

Solutions

  1. Pass a literal string name: `bench.from('baseline', source)`.
  2. Or pass a named function declaration/expression: `bench.from(function baseline() {}, source)` — its `.name` is used.
  3. If the name is dynamic, ensure it is typed as `string` before the call; add a runtime check `if (typeof name !== 'string') throw ...` upstream.

Example fix

// before
bench.from({ name: 'baseline' }, './results.json')

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

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

null

Prevention

When it happens

Trigger: Calling `bench.from(123, source)`, `bench.from({}, source)`, `bench.from(null, source)`, or `bench.from(undefined, source)`. The guard `typeof nameOrFunction !== 'string' && typeof nameOrFunction !== 'function'` is narrow and explicit.

Common situations: Passing an options object as the first argument by mistake (mirroring `bench(name, options, fn)` shape that does not exist for `.from`); passing a registration object instead of a name; refactor where a name variable became `undefined`.

Related errors


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

Appendix: 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 1fa9837ec2)