vitest-dev/vitest · error · Error

MemberExpression is not supported. Please open a new bug rep

Error message

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

What it means

Thrown at packages/mocker/src/node/automock.ts:122-125 by traversePattern when traversing an export destructuring pattern encounters a MemberExpression. Member expressions in destructuring (e.g. `{ [obj.key]: value }` or computed member access in the pattern) cannot be statically analyzed into named exports, so the automocker refuses them.

Source

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

            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 })
        }
        else if (declaration.type === 'VariableDeclaration') {
          declaration.declarations.forEach((declaration) => {
            traversePattern(declaration.id)
          })
        }
        else if (declaration.type === 'ClassDeclaration') {

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Provide an explicit vi.mock factory to bypass automock parsing.
  2. Refactor the target module to use static export names instead of computed destructuring.
  3. Mock individual named exports rather than the whole module.

Example fix

// target module (causes error when automocked):
// export const { [config.key]: handler } = handlers

// fix in the test: provide a factory
vi.mock('./handlers', () => ({ handler: vi.fn() }))
Defensive patterns

Strategy: validation

Validate before calling

// Detect member expressions in export destructuring of the target before automocking.
import { readFileSync } from 'node:fs'
function hasMemberExpressionInExport(file: string): boolean {
  // crude heuristic: computed/member key in exported destructure
  return /export\s+const\s*\{\s*\[[^\]]+\]/.test(readFileSync(file, 'utf-8'))
}

if (hasMemberExpressionInExport('./handlers.ts')) {
  vi.mock('./handlers.ts', () => ({ handler: vi.fn() }))
} else {
  vi.mock('./handlers.ts')
}

Try / catch

try {
  vi.mock('./handlers.ts')
} catch (e) {
  if (e instanceof Error && /MemberExpression is not supported/.test(e.message)) {
    vi.mock('./handlers.ts', () => ({ handler: vi.fn() }))
  } else { throw e }
}

Prevention

When it happens

Trigger: Automocking (no factory) a module containing an export destructuring pattern with a member expression, e.g. `export const { [keys.main]: primary } = source`.

Common situations: Automocking a module with computed/dynamic destructuring keys; refactor introducing a member expression in an exported destructure.

Related errors


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