{"record":{"id":"61d1af47d12faa4e","repo":"vitest-dev/vitest","slug":"cannot-provide-key-because-it-s-not-serializa","errorCode":null,"errorMessage":"Cannot provide \"${key}\" because it's not serializable.","messagePattern":"Cannot provide \"(.+?)\" because it's not serializable\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/project.ts","lineNumber":132,"sourceCode":"          this._fetcher,\n          this.config,\n        )\n  }\n\n  // \"provide\" is a property, not a method to keep the context when destructed in the global setup,\n  // making it a method would be a breaking change, and can be done in Vitest 3 at minimum\n  /**\n   * Provide a value to the test context. This value will be available to all tests with `inject`.\n   */\n  provide = <T extends keyof ProvidedContext & string>(\n    key: T,\n    value: ProvidedContext[T],\n  ): void => {\n    try {\n      structuredClone(value)\n    }\n    catch (err) {\n      throw new Error(\n        `Cannot provide \"${key}\" because it's not serializable.`,\n        {\n          cause: err,\n        },\n      )\n    }\n    // casting `any` because the default type is `never` since `ProvidedContext` is empty\n    (this._provided as any)[key] = value\n  }\n\n  /**\n   * Get the provided context. The project context is merged with the global context.\n   */\n  getProvidedContext(): ProvidedContext {\n    if (this.isRootProject()) {\n      return this._provided\n    }\n    // globalSetup can run even if core workspace is not part of the test run","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/node/project.ts#L114-L150","documentation":"project.provide() stores a value in the provided context that is shipped to workers via structured clone (the RPC boundary). Before storing it, Vitest calls structuredClone(value) to fail fast: if the value cannot be cloned (functions, DOM nodes, class instances with non-cloneable internals), it rejects with the offending key named and the clone error as cause.","triggerScenarios":"Calling project.provide('key', value) (or the global provide) with a value that fails structuredClone - e.g. a function, a class instance, a Proxy, or an object containing non-transferable members.","commonSituations":"Providing a function or class instance expecting it to survive the worker boundary, providing a handle to a server/db connection, or providing an object that transitively holds a function.","solutions":["Provide only plain JSON-serializable data (primitives, plain objects, arrays, Date, Map, Set, ArrayBuffer).","If you must pass behavior, provide a serializable token/factory string and reconstruct it inside the test.","Pre-test the value with structuredClone(value) locally to find the non-cloneable member.","Strip functions/proxies/class instances before calling provide."],"exampleFix":"// before\nproject.provide('db', databaseConnection) // not serializable\n// after\nproject.provide('dbConfig', { url: process.env.DB_URL })\n// then connect inside the test using inject('dbConfig')","handlingStrategy":"validation","validationCode":"// Mirror Vitest's own check before calling provide.\nfunction safeProvide(project: { provide: (k: string, v: unknown) => void }, key: string, value: unknown) {\n  try {\n  structuredClone(value)\n  } catch (err) {\n  throw new Error(`Refusing to provide \"${key}\": value is not structured-cloneable`, { cause: err })\n  }\n  project.provide(key as any, value as any)\n}","typeGuard":"const isSerializable = (value: unknown): boolean => {\n  try { structuredClone(value); return true } catch { return false }\n}","tryCatchPattern":null,"preventionTips":["Only provide plain data (primitives, plain objects/arrays, Date, Map, Set, ArrayBuffer).","Never provide functions, class instances, proxies, or resource handles.","Unit-test your provide payloads with structuredClone in CI."],"tags":["provide","inject","serialization","structured-clone","config"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}