vitest-dev/vitest · error · Error

Tag name "${tag.name}" is invalid. Tag names cannot contain

Error message

Tag name "${tag.name}" is invalid. Tag names cannot contain spaces.

What it means

Thrown during tag validation when a tag name contains whitespace (matches /\s/). Tag selection expressions are tokenized on whitespace, so a name containing spaces would be ambiguous to parse; such names are rejected to keep the filter grammar well-defined.

Source

Thrown at packages/vitest/src/node/config/resolveConfig.ts:268

  }

  resolved.pool ??= 'forks'

  resolved.project = toArray(resolved.project)
  resolved.provide ??= {}

  // shallow copy tags array to avoid mutating user config
  resolved.tags = [...resolved.tags || []]
  const definedTags = new Set<string>()
  resolved.tags.forEach((tag) => {
    if (!tag.name || typeof tag.name !== 'string') {
      throw new Error(`Each tag defined in "test.tags" must have a "name" property, received: ${JSON.stringify(tag)}`)
    }
    if (definedTags.has(tag.name)) {
      throw new Error(`Tag name "${tag.name}" is already defined in "test.tags". Tag names must be unique.`)
    }
    if (/\s/.test(tag.name)) {
      throw new Error(`Tag name "${tag.name}" is invalid. Tag names cannot contain spaces.`)
    }
    if (/[!()*|&]/.test(tag.name)) {
      throw new Error(`Tag name "${tag.name}" is invalid. Tag names cannot contain "!", "*", "&", "|", "(", or ")".`)
    }
    if (/^\s*(?:and|or|not)\s*$/i.test(tag.name)) {
      throw new Error(`Tag name "${tag.name}" is invalid. Tag names cannot be a logical operator like "and", "or", "not".`)
    }
    if (typeof tag.retry === 'object' && typeof tag.retry.condition === 'function') {
      throw new TypeError(`Tag "${tag.name}": retry.condition function cannot be used inside a config file. Use a RegExp pattern instead, or define the function in your test file.`)
    }
    if (tag.priority != null && (typeof tag.priority !== 'number' || tag.priority < 0)) {
      throw new TypeError(`Tag "${tag.name}": priority must be a non-negative number.`)
    }
    definedTags.add(tag.name)
  })

  resolved.name = typeof options.name === 'string'
    ? options.name

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Remove spaces from tag names: use 'slow-tests', 'slowTests', or 'slow'.
  2. Normalize external strings (replace \s+ with '-' or '_') before assigning as tag names.

Example fix

// before
export default defineConfig({ test: { tags: [{ name: 'slow tests' }] } })
// after
export default defineConfig({ test: { tags: [{ name: 'slow-tests' }] } })
Defensive patterns

Strategy: validation

Validate before calling

if (/\s/.test(name)) {
  throw new Error(`tag name contains spaces: ${name}`)
}

Type guard

function isValidTagName(name: string): boolean {
  return name.length > 0 && !/\s/.test(name) && !/[!()*|&]/.test(name)
    && !/^\s*(?:and|or|not)\s*$/i.test(name)
}

Prevention

When it happens

Trigger: Defining a tag like `{ name: 'slow tests' }` or `{ name: 'unit core' }`, or reading tag names from user data/file names that contain spaces.

Common situations: Tag names derived from human-readable labels, test suite titles, or spreadsheet columns without normalization.


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