{"record":{"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":"validation","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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/runtime/runner/artifact.ts#L160-L188","documentation":"Thrown by manageArtifactAttachment when a TestAttachment sets `path` together with `bodyEncoding`. bodyEncoding only describes how the inline `body` string is encoded (base64 vs utf-8), so it is meaningless for a path-based attachment. Vitest enforces the invariant so downstream consumers do not try to decode a file path.","triggerScenarios":"Constructing an attachment like `{ path: '/tmp/x.png', bodyEncoding: 'base64' }` and passing it through context.annotate, recordArtifact attachments, or a custom artifact with attachments. Reusing a body-style attachment object and just adding a path without clearing bodyEncoding.","commonSituations":"Spreading a shared attachment template (`{ bodyEncoding: 'base64' }`) into path-based attachments; reporter/plugin code that always sets bodyEncoding regardless of source type.","solutions":["Remove `bodyEncoding` from any attachment that uses `path`.","Use two distinct factory helpers: one for body attachments (sets bodyEncoding) and one for path attachments (omits it).","Run a normalize step that deletes bodyEncoding whenever path is set."],"exampleFix":"// before\nattach({ path: '/tmp/log.txt', bodyEncoding: 'utf-8' })\n\n// after\nattach({ path: '/tmp/log.txt' })","handlingStrategy":"validation","validationCode":"function normalizePathAttachment(a: TestAttachment) {\n  if (a.path) delete a.bodyEncoding\n  return a\n}","typeGuard":"const pathHasNoEncoding = (a: TestAttachment) =>\n  !a.path || !a.bodyEncoding","tryCatchPattern":"try {\n  await ctx.annotate(msg, attachment)\n} catch (e) {\n  if (e instanceof TypeError && /bodyEncoding/i.test(e.message)) {\n    delete attachment.bodyEncoding\n    await ctx.annotate(msg, attachment)\n  } else throw e\n}","preventionTips":["Keep bodyEncoding coupled to body in factory code; never set it on path attachments.","Run a normalize pass that strips bodyEncoding when path is present."],"tags":["attachments","test-context","validation","artifact"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}