diff --git a/docs/api/advanced/artifacts.md b/docs/api/advanced/artifacts.md index 67b121f9d998..9fdcd6588835 100644 --- a/docs/api/advanced/artifacts.md +++ b/docs/api/advanced/artifacts.md @@ -64,6 +64,12 @@ The `TestArtifactBase` interface is the base for all test artifacts. Extend this interface when creating custom test artifacts. Vitest automatically manages the `attachments` array and injects the `location` property to indicate where the artifact was created in your test code. +::: danger +When running with [`api.allowWrite`](/config/api#api-allowwrite) or [`browser.api.allowWrite`](/config/browser/api#api-allowwrite) disabled, Vitest empties the `attachments` array on every artifact before reporting it. + +If your custom artifact narrows the `attachments` type (e.g. to a tuple), include `| []` in the union so the type reflects what actually happens at runtime. +::: + ### `TestAttachment` ```ts @@ -109,6 +115,7 @@ Here are a few guidelines or best practices to follow: - Try using a `Symbol` as the **registry key** to guarantee uniqueness - The `type` property should follow the pattern `'package-name:artifact-name'`, **`'internal:'` is a reserved prefix** - Use `attachments` to include files or data; extend [`TestAttachment`](#testattachment) for custom metadata +- If you narrow the `attachments` type (e.g. to a tuple), include `| []` in the union since Vitest may empty the array at runtime (see [`TestArtifactBase`](#testartifactbase)) - `location` property is automatically injected ## Custom Artifacts @@ -127,7 +134,7 @@ interface AccessibilityArtifact extends TestArtifactBase { type: 'a11y:report' passed: boolean wcagLevel: 'A' | 'AA' | 'AAA' - attachments: [A11yReportAttachment] + attachments: [A11yReportAttachment] | [] } const a11yReportKey = Symbol('report') diff --git a/packages/runner/src/types/tasks.ts b/packages/runner/src/types/tasks.ts index dd03229a6114..1c231c255d9b 100644 --- a/packages/runner/src/types/tasks.ts +++ b/packages/runner/src/types/tasks.ts @@ -1293,6 +1293,8 @@ export interface TestArtifactLocation extends FileLocation {} * Base interface for all test artifacts. * * Extend this interface when creating custom test artifacts. Vitest automatically manages the `attachments` array and injects the `location` property to indicate where the artifact was created in your test code. + * + * **Important**: when running with [`api.allowWrite`](https://vitest.dev/config/api#api-allowwrite) or [`browser.api.allowWrite`](https://vitest.dev/config/browser/api#api-allowwrite) disabled, Vitest empties the `attachments` array on every artifact before reporting it. */ export interface TestArtifactBase { /** File or data attachments associated with this artifact */