|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { spawnNodeEvalSync } from "../src/test-utils/node-process.js"; |
| 3 | +import { DEFAULT_VITEST_TEST_TIMEOUT_MS } from "./vitest/vitest.timeouts.ts"; |
| 4 | + |
| 5 | +const reporterConfigs = [ |
| 6 | + "vitest.config.ts", |
| 7 | + "test/vitest/vitest.cli-process.config.ts", |
| 8 | + "test/vitest/vitest.ui.config.ts", |
| 9 | + "test/vitest/vitest.ui-e2e.config.ts", |
| 10 | + "test/vitest/vitest.e2e.config.ts", |
| 11 | + "ui/vitest.config.ts", |
| 12 | + "ui/vitest.node.config.ts", |
| 13 | +]; |
| 14 | + |
| 15 | +type ReporterEntry = [string, Record<string, unknown>]; |
| 16 | +type ReporterResolution = { |
| 17 | + defaults: Array<{ config: string; reporters: ReporterEntry[]; cli: ReporterEntry[] }>; |
| 18 | + custom: ReporterEntry[]; |
| 19 | + customCli: ReporterEntry[]; |
| 20 | + injectedPty: ReporterEntry[]; |
| 21 | +}; |
| 22 | + |
| 23 | +describe("Vitest reporter contracts", () => { |
| 24 | + it.each(["false", "true"])( |
| 25 | + "reports completed agent tests and preserves overrides with GITHUB_ACTIONS=%s", |
| 26 | + (githubActions) => { |
| 27 | + // Resolve in a fresh process: shared config and std-env capture their environment on import. |
| 28 | + // This starts no test workers and leaves the enclosing Vitest module/cache ownership alone. |
| 29 | + const result = spawnNodeEvalSync( |
| 30 | + ` |
| 31 | + import path from "node:path"; |
| 32 | + import { parseCLI, resolveConfig } from "vitest/node"; |
| 33 | + import { sharedVitestConfig } from "./test/vitest/vitest.shared.config.ts"; |
| 34 | + import { createTuiPtyVitestConfig } from "./test/vitest/vitest.tui-pty.config.ts"; |
| 35 | + const defaults = []; |
| 36 | + for (const config of ${JSON.stringify(reporterConfigs)}) { |
| 37 | + const root = config.startsWith("ui/") ? path.resolve("ui") : process.cwd(); |
| 38 | + const options = { root, config: path.resolve(config) }; |
| 39 | + const normal = await resolveConfig(options); |
| 40 | + const cli = parseCLI(["vitest", "--reporter=json"]).options; |
| 41 | + const override = await resolveConfig({ ...cli, ...options }); |
| 42 | + defaults.push({ config, reporters: normal.vitestConfig.reporters, cli: override.vitestConfig.reporters }); |
| 43 | + } |
| 44 | + const customConfig = { |
| 45 | + ...sharedVitestConfig, |
| 46 | + test: { |
| 47 | + ...sharedVitestConfig.test, |
| 48 | + reporters: [["json", { outputFile: "custom-report.json" }]], |
| 49 | + }, |
| 50 | + }; |
| 51 | + const custom = await resolveConfig({ config: false }, customConfig); |
| 52 | + const customCli = await resolveConfig({ |
| 53 | + ...parseCLI(["vitest", "--reporter=json", "--reporter=json"]).options, |
| 54 | + config: false, |
| 55 | + }, customConfig); |
| 56 | + const injectedPty = await resolveConfig({ config: false }, createTuiPtyVitestConfig({ |
| 57 | + GITHUB_ACTIONS: process.env.GITHUB_ACTIONS === "true" ? "false" : "true", |
| 58 | + })); |
| 59 | + console.log("REPORTER_RESOLUTION " + JSON.stringify({ |
| 60 | + defaults, |
| 61 | + custom: custom.vitestConfig.reporters, |
| 62 | + customCli: customCli.vitestConfig.reporters, |
| 63 | + injectedPty: injectedPty.vitestConfig.reporters, |
| 64 | + })); |
| 65 | + `, |
| 66 | + { |
| 67 | + imports: ["tsx"], |
| 68 | + env: { ...process.env, AI_AGENT: "vitest-reporter-test", GITHUB_ACTIONS: githubActions }, |
| 69 | + timeout: DEFAULT_VITEST_TEST_TIMEOUT_MS, |
| 70 | + }, |
| 71 | + ); |
| 72 | + expect(result.error, result.stderr).toBeUndefined(); |
| 73 | + expect(result.signal, result.stderr).toBeNull(); |
| 74 | + expect(result.status, result.stderr).toBe(0); |
| 75 | + const report = result.stdout |
| 76 | + .split("\n") |
| 77 | + .find((line) => line.startsWith("REPORTER_RESOLUTION ")); |
| 78 | + expect(report, result.stdout).toBeDefined(); |
| 79 | + const resolved = JSON.parse( |
| 80 | + report!.slice("REPORTER_RESOLUTION ".length), |
| 81 | + ) as ReporterResolution; |
| 82 | + const expected = githubActions === "true" ? ["verbose", "github-actions"] : ["verbose"]; |
| 83 | + for (const { config, reporters, cli } of resolved.defaults) { |
| 84 | + expect( |
| 85 | + reporters.map(([name]) => name), |
| 86 | + config, |
| 87 | + ).toEqual(expected); |
| 88 | + expect(cli, `${config} CLI override`).toEqual([["json", {}]]); |
| 89 | + } |
| 90 | + expect(resolved.defaults).toHaveLength(reporterConfigs.length); |
| 91 | + expect(resolved.custom).toEqual([["json", { outputFile: "custom-report.json" }]]); |
| 92 | + expect(resolved.customCli).toEqual(resolved.custom); |
| 93 | + expect(resolved.injectedPty.map(([name]) => name)).toEqual( |
| 94 | + githubActions === "true" ? ["verbose"] : ["verbose", "github-actions"], |
| 95 | + ); |
| 96 | + }, |
| 97 | + ); |
| 98 | +}); |
0 commit comments