|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import type { Reporter, TestModule, TestProject, TestSpecification, Vitest } from "vitest/node"; |
| 5 | + |
| 6 | +function projectIdentity(project: TestProject) { |
| 7 | + return { |
| 8 | + name: project.name, |
| 9 | + root: project.config.root, |
| 10 | + config: project.vite.config.configFile, |
| 11 | + pool: project.config.pool, |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +function moduleIdentity(spec: TestSpecification) { |
| 16 | + return { |
| 17 | + ...projectIdentity(spec.project), |
| 18 | + file: spec.moduleId, |
| 19 | + pool: spec.pool, |
| 20 | + taskId: spec.taskId, |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +// Native Istanbul reporter destinations (istanbul-reports/lib/*/index.js). |
| 25 | +// Tuple options/custom reporters can select their own paths and need a separate contract. |
| 26 | +const coverageFiles: Record<string, string[]> = { |
| 27 | + text: [], |
| 28 | + "text-summary": [], |
| 29 | + "text-lcov": [], |
| 30 | + teamcity: [], |
| 31 | + none: [], |
| 32 | + json: ["coverage-final.json"], |
| 33 | + "json-summary": ["coverage-summary.json"], |
| 34 | + lcov: ["lcov.info", "lcov-report/index.html"], |
| 35 | + lcovonly: ["lcov.info"], |
| 36 | + html: ["index.html"], |
| 37 | + "html-spa": ["index.html"], |
| 38 | + clover: ["clover.xml"], |
| 39 | + cobertura: ["cobertura-coverage.xml"], |
| 40 | +}; |
| 41 | + |
| 42 | +export type VitestReportCapture = { |
| 43 | + pid: number; |
| 44 | + command: string[]; |
| 45 | + root: string; |
| 46 | + projects: ReturnType<typeof projectIdentity>[]; |
| 47 | + modules: ReturnType<typeof moduleIdentity>[]; |
| 48 | + coverageDirectory: string | null; |
| 49 | + coverageFiles: string[]; |
| 50 | + coverageOnFailure: boolean; |
| 51 | + processTimedOut: boolean; |
| 52 | + passWithNoTests: boolean; |
| 53 | + ignoreUnhandledErrors: boolean; |
| 54 | + ended?: { reason: string; unhandledErrors: number; failedModules: number }; |
| 55 | +}; |
| 56 | + |
| 57 | +const sorted = (values: unknown[]) => values.map((value) => JSON.stringify(value)).toSorted(); |
| 58 | + |
| 59 | +/** Observe native loading and replay; never replace reporters or reconstruct native tasks. */ |
| 60 | +export default class VitestReportCaptureReporter implements Reporter { |
| 61 | + private output = ""; |
| 62 | + private capture!: VitestReportCapture; |
| 63 | + private expected: VitestReportCapture[]; |
| 64 | + |
| 65 | + constructor(options: { expected?: VitestReportCapture[] } = {}) { |
| 66 | + this.expected = options.expected ?? []; |
| 67 | + } |
| 68 | + |
| 69 | + onInit(ctx: Vitest) { |
| 70 | + const output = ctx.config.outputFile; |
| 71 | + assert(output && typeof output === "object" && typeof output.json === "string"); |
| 72 | + this.output = `${output.json}.capture.json`; |
| 73 | + for (const reporter of ctx.config.reporters) { |
| 74 | + if (Array.isArray(reporter) && ["json", "blob"].includes(reporter[0])) { |
| 75 | + assert( |
| 76 | + !("outputFile" in reporter[1] && reporter[1].outputFile != null) && |
| 77 | + !("filterMeta" in reporter[1] && reporter[1].filterMeta != null), |
| 78 | + "Multi-invocation JSON cannot preserve config-owned reporter options. Run each config separately with its own native output file.", |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + const projects = ctx.projects.map(projectIdentity); |
| 83 | + assert( |
| 84 | + projects.every( |
| 85 | + (project, index) => |
| 86 | + project.name && project.config && !ctx.projects[index]!.isBrowserEnabled(), |
| 87 | + ), |
| 88 | + "Multi-invocation JSON requires named file-based Node projects; run inline/browser configurations separately.", |
| 89 | + ); |
| 90 | + const coverage = ctx.config.coverage; |
| 91 | + const expectedCoverage = coverage.enabled |
| 92 | + ? coverage.reporter.flatMap(([name, options]) => { |
| 93 | + assert( |
| 94 | + (coverage.provider === "v8" || coverage.provider === "istanbul") && |
| 95 | + Object.hasOwn(coverageFiles, name) && |
| 96 | + Object.keys(options).length === 0, |
| 97 | + "Multi-invocation JSON cannot own custom coverage providers/reporters or tuple options. Run each config separately with unique coverage destinations.", |
| 98 | + ); |
| 99 | + return coverageFiles[name]!; |
| 100 | + }) |
| 101 | + : []; |
| 102 | + this.capture = { |
| 103 | + pid: process.pid, |
| 104 | + command: [process.execPath, ...process.execArgv, ...process.argv.slice(1)], |
| 105 | + root: ctx.config.root, |
| 106 | + projects, |
| 107 | + modules: [], |
| 108 | + coverageDirectory: ctx.config.coverage.enabled ? ctx.config.coverage.reportsDirectory : null, |
| 109 | + coverageFiles: expectedCoverage, |
| 110 | + coverageOnFailure: coverage.reportOnFailure, |
| 111 | + processTimedOut: false, |
| 112 | + passWithNoTests: ctx.config.passWithNoTests, |
| 113 | + ignoreUnhandledErrors: ctx.config.dangerouslyIgnoreUnhandledErrors, |
| 114 | + }; |
| 115 | + if (this.expected.length) { |
| 116 | + const expectedProjects = [ |
| 117 | + ...new Set(this.expected.flatMap((capture) => sorted(capture.projects))), |
| 118 | + ].toSorted(); |
| 119 | + assert.deepEqual(sorted(projects), expectedProjects, "Native merge project identity changed"); |
| 120 | + } |
| 121 | + this.writeCapture(); |
| 122 | + } |
| 123 | + |
| 124 | + onTestRunStart(specs: readonly TestSpecification[]) { |
| 125 | + if (this.expected.length) { |
| 126 | + assert.deepEqual( |
| 127 | + sorted(specs.map(moduleIdentity)), |
| 128 | + sorted(this.expected.flatMap((capture) => capture.modules)), |
| 129 | + "Native merge module/project/root/pool attribution changed", |
| 130 | + ); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + onTestRunEnd(modules: readonly TestModule[], errors: readonly unknown[], reason: string) { |
| 135 | + this.capture.modules = modules.map((module) => moduleIdentity(module.toTestSpecification())); |
| 136 | + this.capture.ended = { |
| 137 | + reason, |
| 138 | + unhandledErrors: errors.length, |
| 139 | + failedModules: modules.filter((module) => !module.ok()).length, |
| 140 | + }; |
| 141 | + this.writeCapture(); |
| 142 | + } |
| 143 | + |
| 144 | + onProcessTimeout() { |
| 145 | + this.capture.processTimedOut = true; |
| 146 | + this.writeCapture(); |
| 147 | + } |
| 148 | + |
| 149 | + private writeCapture() { |
| 150 | + fs.mkdirSync(path.dirname(this.output), { recursive: true }); |
| 151 | + fs.writeFileSync(this.output, JSON.stringify(this.capture)); |
| 152 | + } |
| 153 | +} |
0 commit comments