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 "!", "*", "&", "|", "(", or ")".

What it means

Thrown during tag validation when a tag name contains any of the characters `!`, `*`, `&`, `|`, `(`, `)` (regex /[!()*|&]/). These characters are reserved by the tag-filter expression grammar (logical operators AND/OR/NOT, grouping parentheses, glob/wildcard), so allowing them in a name would make filter expressions ambiguous.

Source

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

  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
    : (options.name?.label || '')

  resolved.color = typeof options.name !== 'string' ? options.name?.color : undefined

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Strip or replace the reserved characters: use 'flaky', 'a-and-b', 'core-ui', 'all'.
  2. Validate/generate tag names with an allow-list character set ([A-Za-z0-9_-]).

Example fix

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

Strategy: validation

Validate before calling

if (/[!()*|&]/.test(name)) {
  throw new Error(`tag name has reserved chars: ${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: 'flaky!' }`, `{ name: 'a&b' }`, `{ name: 'core(ui)' }`, or `{ name: 'all*' }`. The reserved-character regex matches and the error lists the forbidden characters.

Common situations: Tag names copied from labels that include punctuation; names that intentionally use C-style logical symbols; auto-generated names from paths containing parentheses.


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