{"id":"997b3d8774597772","repo":"vitest-dev/vitest","slug":"failed-to-load-custom-defines-error-message-997b3d","errorCode":null,"errorMessage":"Failed to load custom \"defines\": ${error.message}","messagePattern":"Failed to load custom \"defines\": (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/workers/vm.ts","lineNumber":151,"sourceCode":"      context,\n      externalModulesExecutor,\n    },\n    configurable: true,\n    enumerable: false,\n    writable: false,\n  })\n  context.__vitest_mocker__ = moduleRunner.mocker\n\n  setupEnv(ctx.config.env, state.metaEnv)\n\n  if (ctx.config.serializedDefines) {\n    try {\n      runInContext(ctx.config.serializedDefines, context, {\n        filename: 'virtual:load-defines.js',\n      })\n    }\n    catch (error: any) {\n      throw new Error(`Failed to load custom \"defines\": ${error.message}`)\n    }\n  }\n  await moduleRunner.mocker.initializeSpyModule()\n\n  const { run } = (await moduleRunner.import(\n    entryFile,\n  )) as typeof import('../runVmTests')\n\n  try {\n    await run(\n      method,\n      ctx.files,\n      ctx.config,\n      moduleRunner,\n      traces,\n    )\n  }\n  finally {","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/workers/vm.ts#L133-L169","documentation":"Same define-evaluation guard as error 383 but in the VM pool: runVmTests evaluates config.serializedDefines via vm.runInContext inside the VM context (vm.ts:144-153). If the injected define code is invalid, the error is wrapped and rethrown.","triggerScenarios":"Configuring test.define / vite define with values that serialize to invalid JS, surfaced specifically when running under the vmThreads/vmForks pool. The serialized defines are run inside the freshly created VM context.","commonSituations":"A define value that is not JSON-stringified (object/function inserted raw); a define key containing characters that break the generated code; switching to the VM pool exposing a define bug that threads tolerated by chance.","solutions":["JSON.stringify every non-primitive define value.","Read the appended inner error.message for the precise syntax/runtime fault.","Temporarily run with pool 'threads' to confirm the defines work, then fix and re-enable the VM pool."],"exampleFix":"// before\nexport default defineConfig({\n  test: { define: { __CFG: { api: '/v1' } } },\n})\n\n// after\nexport default defineConfig({\n  test: { define: { __CFG: JSON.stringify({ api: '/v1' }) } },\n})","handlingStrategy":"validation","validationCode":"function validateSerializedDefines(source: string) {\n  try {\n    // mimic runInContext by parsing as a script\n    new Function(source)\n  } catch (e) {\n    throw new Error(`define source is not valid JS: ${(e as Error).message}`)\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use JSON.stringify for non-literal define values.","Keep defines to primitive literals in config.","Smoke-test config boot under both threads and vm pools."],"tags":["vm-pool","define","config","vm-eval"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}