{"id":"9a91ce111e00584d","repo":"vitest-dev/vitest","slug":"assignmentpattern-is-not-supported-please-open-a","errorCode":null,"errorMessage":"AssignmentPattern is not supported. Please open a new bug report.","messagePattern":"AssignmentPattern is not supported\\. Please open a new bug report\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/node/automock.ts","lineNumber":116,"sourceCode":"            if (property.type === 'RestElement') {\n              traversePattern(property)\n            }\n            // export const { test, test2: alias } = {}\n            else if (property.type === 'Property') {\n              traversePattern(property.value)\n            }\n            else {\n              property satisfies never\n            }\n          })\n        }\n        else if (expression.type === 'RestElement') {\n          traversePattern(expression.argument)\n        }\n        // const [name[1], name[2]] = []\n        // cannot be used in export\n        else if (expression.type === 'AssignmentPattern') {\n          throw new Error(\n            `AssignmentPattern is not supported. Please open a new bug report.`,\n          )\n        }\n        // const test = thing.func()\n        // cannot be used in export\n        else if (expression.type === 'MemberExpression') {\n          throw new Error(\n            `MemberExpression is not supported. Please open a new bug report.`,\n          )\n        }\n        else {\n          expression satisfies never\n        }\n      }\n\n      if (declaration) {\n        if (declaration.type === 'FunctionDeclaration') {\n          allSpecifiers.push({ name: declaration.id.name })","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/node/automock.ts#L98-L134","documentation":"Thrown at packages/mocker/src/node/automock.ts:115-118 by traversePattern when traversing an export destructuring pattern encounters an AssignmentPattern. Assignment patterns (`{ a = 1 } = obj`) represent default values in destructuring, which the automocker's static export collection does not support.","triggerScenarios":"Automocking (no factory) a module that contains an export with a destructuring default, e.g. `export const { count = 0 } = getConfig()` or `export const [first = 'x'] = items`.","commonSituations":"Automocking a config/util module that destructures exports with defaults; refactoring a module to use default destructuring breaks previously-working automocks.","solutions":["Provide an explicit vi.mock factory so automock parsing is bypassed.","Refactor the target module to avoid default values in exported destructuring patterns.","Mock only the specific named exports you need rather than the whole module."],"exampleFix":"// target module (causes error when automocked):\n// export const { port = 3000 } = loadConfig()\n\n// fix in the test: provide a factory\nvi.mock('./config', () => ({ port: 8080 }))","handlingStrategy":"validation","validationCode":"// Detect default destructuring in exports of the target before automocking.\nimport { readFileSync } from 'node:fs'\nfunction hasAssignmentPatternInExport(file: string): boolean {\n  // crude heuristic: exported destructuring with an '=' default\n  return /export\\s+const\\s*\\{[^}=]*=[^}=]*\\}/.test(readFileSync(file, 'utf-8'))\n}\n\nif (hasAssignmentPatternInExport('./config.ts')) {\n  vi.mock('./config.ts', () => ({ port: 8080 }))\n} else {\n  vi.mock('./config.ts')\n}","typeGuard":null,"tryCatchPattern":"try {\n  vi.mock('./config.ts')\n} catch (e) {\n  if (e instanceof Error && /AssignmentPattern is not supported/.test(e.message)) {\n    vi.mock('./config.ts', () => ({ port: 8080 }))\n  } else { throw e }\n}","preventionTips":["Avoid default values in exported destructuring patterns of modules you automock.","Provide explicit vi.mock factories for modules that use destructured exports.","Mock only the specific named exports you need."],"tags":["automock","ast","destructuring","assignment-pattern"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}