Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions packages/ui/node/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ModuleGraphData, RunnerTestFile, SerializedConfig } from 'vitest'
import type { ModuleGraphData, RunnerTask, RunnerTestFile, SerializedConfig } from 'vitest'
import type { HTMLOptions, Reporter, Vitest } from 'vitest/node'
import { existsSync, promises as fs } from 'node:fs'
import { fileURLToPath } from 'node:url'
Expand Down Expand Up @@ -101,6 +101,11 @@ export default class HTMLReporter implements Reporter {
}))

await Promise.all(promises)

if (this.options.singleFile) {
await inlineAttachments(result.files)
}

await this.writeReport(stringify(result))
}

Expand Down Expand Up @@ -136,7 +141,7 @@ export default class HTMLReporter implements Reporter {

// copy attachments
// TODO: unify attachmentsDir and html outputFile, so both live together without extra copy
if (existsSync(this.ctx.config.attachmentsDir)) {
if (!this.options.singleFile && existsSync(this.ctx.config.attachmentsDir)) {
const destAttachmentsDir = resolve(this.reporterDir, 'data')
await fs.rm(destAttachmentsDir, { recursive: true, force: true })
await fs.mkdir(destAttachmentsDir, { recursive: true })
Expand Down Expand Up @@ -171,3 +176,28 @@ export default class HTMLReporter implements Reporter {
}
}
}

async function inlineAttachments(files: RunnerTestFile[]): Promise<void> {
for (const file of files) {
await inlineTaskAttachments(file)
}
}

async function inlineTaskAttachments(task: RunnerTask): Promise<void> {
if ('tasks' in task) {
for (const child of task.tasks) {
await inlineTaskAttachments(child)
}
}
if ('artifacts' in task) {
for (const artifact of task.artifacts) {
for (const attachment of artifact.attachments ?? []) {
if (attachment.path) {
const buffer = await fs.readFile(attachment.path)
attachment.body = buffer.toString('base64')
attachment.path = undefined
}
}
}
}
}
1 change: 1 addition & 0 deletions packages/vitest/src/node/reporters/html.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface HTMLOptions {
outputFile?: string
singleFile?: boolean
}
Loading