{"id":"9a2fa66e27d6c347","repo":"vitest-dev/vitest","slug":"test-attachment-with-path-should-not-have-bodye","errorCode":null,"errorMessage":"Test attachment with \"path\" should not have \"bodyEncoding\" specified.","messagePattern":"Test attachment with \"path\" should not have \"bodyEncoding\" specified\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/runner/artifact.ts","lineNumber":178,"sourceCode":"/**\n * Validates and prepares a test attachment for serialization.\n *\n * This function ensures attachments have either `body` or `path` set (but not both), and converts `Uint8Array` bodies to base64-encoded strings for easier serialization.\n *\n * @param attachment - The attachment to validate and prepare\n *\n * @throws {TypeError} If neither `body` nor `path` is provided\n * @throws {TypeError} If both `body` and `path` are provided\n */\nexport function manageArtifactAttachment(attachment: TestAttachment): void {\n  if (attachment.body == null && !attachment.path) {\n    throw new TypeError(`Test attachment requires \"body\" or \"path\" to be set. Both are missing.`)\n  }\n  if (attachment.body && attachment.path) {\n    throw new TypeError(`Test attachment requires only one of \"body\" or \"path\" to be set. Both are specified.`)\n  }\n  if (attachment.path && attachment.bodyEncoding) {\n    throw new TypeError(`Test attachment with \"path\" should not have \"bodyEncoding\" specified.`)\n  }\n  // convert to a string so it's easier to serialise\n  if (attachment.body instanceof Uint8Array) {\n    attachment.body = encodeUint8Array(attachment.body)\n  }\n  if (attachment.body != null) {\n    attachment.bodyEncoding ??= 'base64'\n  }\n}\n","sourceCodeStart":160,"sourceCodeEnd":188,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/runner/artifact.ts#L160-L188","documentation":"When recording a test artifact with an attachment, Vitest requires that a path-based attachment (pointing to a file on disk) must not specify a `bodyEncoding` field. `bodyEncoding` (e.g. 'base64') only applies to inline `body` content. This is enforced in `manageArtifactAttachment` (artifact.ts:170) which validates each attachment's shape before serialization, alongside checks that exactly one of `body`/`path` is present.","triggerScenarios":"Calling `recordArtifact(task, { attachments: [{ path: '/some/file.txt', bodyEncoding: 'base64' }] })` or `context.annotate(message, { attachment: { path, bodyEncoding } })` — any attachment object reaching `manageArtifactAttachment` with both `path` truthy and `bodyEncoding` set.","commonSituations":"Copy-pasting attachment configuration between body-based and path-based attachments; building attachments dynamically where a default `bodyEncoding: 'base64'` leaks into path-based ones; migrating from body to path attachments without dropping bodyEncoding.","solutions":["Remove the `bodyEncoding` field from any attachment that uses `path`.","If you need `bodyEncoding`, switch from `path` to `body` (a string or Uint8Array).","Ensure attachment builders are mutually exclusive: never set `bodyEncoding` when `path` is defined."],"exampleFix":"// before\nawait recordArtifact(test, {\n  type: 'report',\n  attachments: [{ path: '/tmp/result.json', bodyEncoding: 'base64' }],\n})\n// after\nawait recordArtifact(test, {\n  type: 'report',\n  attachments: [{ path: '/tmp/result.json' }],\n})","handlingStrategy":"validation","validationCode":"function isValidAttachment(a: { body?: unknown; path?: string; bodyEncoding?: string }) {\n  if (a.path && a.bodyEncoding) return false\n  return true\n}\n// before recording:\nfor (const a of artifact.attachments ?? []) {\n  if (!isValidAttachment(a)) throw new Error('Remove bodyEncoding from path-based attachment')\n}","typeGuard":"function isPathAttachmentWithoutEncoding(a: any): a is { path: string; body?: undefined; bodyEncoding?: undefined } {\n  return typeof a.path === 'string' && a.body == null && a.bodyEncoding == null\n}","tryCatchPattern":null,"preventionTips":["Use distinct builder functions for path-based vs body-based attachments so bodyEncoding never leaks.","Add a unit test that asserts your attachment factory never emits both path and bodyEncoding."],"tags":["attachment","validation","artifact","recordartifact"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}