vitest-dev/vitest · error · Error

Assertion name is not set. This is a bug in Vitest. Please,

Error message

Assertion name is not set. This is a bug in Vitest. Please, open a new issue with reproduction.

What it means

Thrown by `getAssertionName` when a snapshot assertion is invoked but the internal Chai `_name` flag was never set. Vitest relies on `wrapAssertion`/explicit flag setting to record the assertion's name (e.g. `toMatchSnapshot`); an absent name indicates the assertion was registered incorrectly — an internal Vitest bug, not a user error. The message explicitly asks the user to file a bug with a reproduction.

Source

Thrown at packages/vitest/src/integrations/snapshot/chai.ts:60

  catch (e) {
    return e
  }

  throw new Error('snapshot function didn\'t throw')
}

function getTestNames(test: Test) {
  return {
    filepath: test.file.filepath,
    name: getNames(test).slice(1).join(' > '),
    testId: test.id,
  }
}

function getAssertionName(assertion: Chai.Assertion): string {
  const name = chai.util.flag(assertion, '_name') as string | undefined
  if (!name) {
    throw new Error('Assertion name is not set. This is a bug in Vitest. Please, open a new issue with reproduction.')
  }
  return name
}

function getTest(obj: Chai.Assertion) {
  const test = chai.util.flag(obj, 'vitest-test') as Test | undefined
  if (!test) {
    throw new Error(`'${getAssertionName(obj)}' cannot be used without test context`)
  }
  if (test.fails) {
    throw new TestSyntaxError(`'${getAssertionName(obj)}' cannot be used with 'test.fails'`)
  }
  return test
}

function validateAssertion(assertion: Chai.Assertion): void {
  if (chai.util.flag(assertion, 'negate')) {
    throw new Error(`${getAssertionName(assertion)} cannot be used with "not"`)

View on GitHub (pinned to 1fa9837ec2)

Solutions

  1. Report a bug to Vitest with a minimal reproduction (the message requests this).
  2. Check for custom chai plugins or third-party assertion extensions that may clear flags.
  3. Ensure only one copy of Vitest is installed (dedupe) to avoid prototype double-registration.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  expect(value).toMatchSnapshot()
} catch (e) {
  if (e instanceof Error && /Assertion name is not set/.test(e.message)) {
    // internal bug: report to Vitest with a reproduction
  }
  throw e
}

Prevention

When it happens

Trigger: Effectively unreachable through the public API. Could only occur if a snapshot assertion method was added to the Assertion prototype without going through `wrapAssertion` or without setting `_name`, or if the flag was stripped by a chai utility.

Common situations: Should not occur for end users. If seen after upgrading Vitest, suspect an internal regression, a custom chai plugin clobbering flags, or HMR/module duplication.

Related errors


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