vitest-dev/vitest · error · Error

AssignmentPattern is not supported. Please open a new bug…

Error message

AssignmentPattern is not supported. Please open a new bug report.

What it means

Thrown by `traversePattern` inside `automockModule` when walking the destructuring pattern of an `export const` declaration and encountering an `AssignmentPattern` node — i.e., a destructuring element with a default value such as `export const [a = 1] = arr` or `export const { x = 1 } = obj`. The automock exporter does not handle default values in destructured exports, so it refuses rather than producing a silently-wrong mock.

Solutions

  1. Provide an explicit factory to `vi.mock(path, factory)` to skip automock's static export analysis.
  2. Refactor the target module's exports to avoid default values in destructuring patterns.
  3. File the bug report requested by the message if you want automock to support this pattern.

Example fix

// before — automock chokes on the default value
// mod.ts: export const { timeout = 3000 } = config
vi.mock('./mod')
// after — explicit factory
vi.mock('./mod', () => ({ timeout: 5000 }))
Defensive patterns

Strategy: validation

Validate before calling

// Scan the target module for destructured exports with defaults before automocking.
function hasAssignmentPatternExport(ast: any): boolean {
  for (const node of ast.body) {
    if (node.type !== 'ExportNamedDeclaration' || !node.declaration) continue
    for (const decl of node.declaration.declarations || []) {
      if (containsAssignmentPattern(decl.id)) return true
    }
  }
  return false
}

Try / catch

try {
  vi.mock('./mod')
} catch (err) {
  if (err instanceof Error && err.message.includes('AssignmentPattern is not supported')) {
    vi.mock('./mod', () => ({ /* explicit stubs */ }))
  }
}

Prevention

When it happens

Trigger: Automocking (`vi.mock(path)` without a factory) a module that exports via destructuring with defaults: `export const { a = defaultA } = obj` or `export const [first = 'x'] = list`.

Common situations: Automocking utility modules that use destructuring defaults for configuration; modules that destructure from a config object with fallbacks.

Related errors


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

Appendix: source

Thrown at packages/mocker/src/node/automock.ts:116

            if (property.type === 'RestElement') {
              traversePattern(property)
            }
            // export const { test, test2: alias } = {}
            else if (property.type === 'Property') {
              traversePattern(property.value)
            }
            else {
              property satisfies never
            }
          })
        }
        else if (expression.type === 'RestElement') {
          traversePattern(expression.argument)
        }
        // const [name[1], name[2]] = []
        // cannot be used in export
        else if (expression.type === 'AssignmentPattern') {
          throw new Error(
            `AssignmentPattern is not supported. Please open a new bug report.`,
          )
        }
        // const test = thing.func()
        // cannot be used in export
        else if (expression.type === 'MemberExpression') {
          throw new Error(
            `MemberExpression is not supported. Please open a new bug report.`,
          )
        }
        else {
          expression satisfies never
        }
      }

      if (declaration) {
        if (declaration.type === 'FunctionDeclaration') {
          allSpecifiers.push({ name: declaration.id.name })

View on GitHub (pinned to 1fa9837ec2)