Browse Source

Show console logging in vitests when the --no-silent flag is set (#7467)

By default, all of the tests run in silent mode with monkey-patched the console logging so no console logging will ever appear in test output.
This confuses the agent- sometimes it will add console logging to help it debug things, and it won't see the logs that it expects.

Adds src/utils/vitest-verbosity.ts to handle verbosity resolution and console logging.
Modifies src/vitest.config.ts and webview-ui/vitest.config.ts to integrate the new verbosity control.
Removes manual console suppression from src/vitest.setup.ts and webview-ui/vitest.setup.ts as it's now handled dynamically.

Co-authored-by: Chris Hasson <[email protected]>
Chris Hasson 5 months ago
parent
commit
aee531a143

+ 22 - 0
src/utils/vitest-verbosity.ts

@@ -0,0 +1,22 @@
+export function resolveVerbosity(argv = process.argv, env = process.env) {
+	// Check if --no-silent flag is used (native vitest flag)
+	const cliNoSilent = argv.includes("--no-silent") || argv.includes("--silent=false")
+	const silent = !cliNoSilent // Silent by default
+
+	// Check if verbose reporter is requested
+	const wantsVerboseReporter = argv.some(
+		(a) => a === "--reporter=verbose" || a === "-r=verbose" || a === "--reporter",
+	)
+
+	return {
+		silent,
+		reporters: ["dot", ...(wantsVerboseReporter ? ["verbose"] : [])],
+		onConsoleLog: (_log: string, type: string) => {
+			// When verbose, show everything
+			// When silent, allow errors/warnings and drop info/log/warn noise
+			if (!silent || type === "stderr") return
+
+			return false // Drop info/log/warn noise
+		},
+	}
+}

+ 6 - 2
src/vitest.config.ts

@@ -1,15 +1,19 @@
 import { defineConfig } from "vitest/config"
 import { defineConfig } from "vitest/config"
 import path from "path"
 import path from "path"
+import { resolveVerbosity } from "./utils/vitest-verbosity"
+
+const { silent, reporters, onConsoleLog } = resolveVerbosity()
 
 
 export default defineConfig({
 export default defineConfig({
 	test: {
 	test: {
 		globals: true,
 		globals: true,
 		setupFiles: ["./vitest.setup.ts"],
 		setupFiles: ["./vitest.setup.ts"],
 		watch: false,
 		watch: false,
-		reporters: ["dot"],
-		silent: true,
+		reporters,
+		silent,
 		testTimeout: 20_000,
 		testTimeout: 20_000,
 		hookTimeout: 20_000,
 		hookTimeout: 20_000,
+		onConsoleLog,
 	},
 	},
 	resolve: {
 	resolve: {
 		alias: {
 		alias: {

+ 0 - 16
src/vitest.setup.ts

@@ -15,19 +15,3 @@ export function allowNetConnect(host?: string | RegExp) {
 
 
 // Global mocks that many tests expect.
 // Global mocks that many tests expect.
 global.structuredClone = global.structuredClone || ((obj: any) => JSON.parse(JSON.stringify(obj)))
 global.structuredClone = global.structuredClone || ((obj: any) => JSON.parse(JSON.stringify(obj)))
-
-// Suppress console.log during tests to reduce noise.
-// Keep console.error for actual errors.
-const originalConsoleLog = console.log
-const originalConsoleWarn = console.warn
-const originalConsoleInfo = console.info
-
-console.log = () => {}
-console.warn = () => {}
-console.info = () => {}
-
-afterAll(() => {
-	console.log = originalConsoleLog
-	console.warn = originalConsoleWarn
-	console.info = originalConsoleInfo
-})

+ 6 - 2
webview-ui/vitest.config.ts

@@ -1,15 +1,19 @@
 import { defineConfig } from "vitest/config"
 import { defineConfig } from "vitest/config"
 import path from "path"
 import path from "path"
+import { resolveVerbosity } from "../src/utils/vitest-verbosity"
+
+const { silent, reporters, onConsoleLog } = resolveVerbosity()
 
 
 export default defineConfig({
 export default defineConfig({
 	test: {
 	test: {
 		globals: true,
 		globals: true,
 		setupFiles: ["./vitest.setup.ts"],
 		setupFiles: ["./vitest.setup.ts"],
 		watch: false,
 		watch: false,
-		reporters: ["dot"],
-		silent: true,
+		reporters,
+		silent,
 		environment: "jsdom",
 		environment: "jsdom",
 		include: ["src/**/*.spec.ts", "src/**/*.spec.tsx"],
 		include: ["src/**/*.spec.ts", "src/**/*.spec.tsx"],
+		onConsoleLog,
 	},
 	},
 	resolve: {
 	resolve: {
 		alias: {
 		alias: {

+ 0 - 16
webview-ui/vitest.setup.ts

@@ -50,19 +50,3 @@ Object.defineProperty(window, "matchMedia", {
 
 
 // Mock scrollIntoView which is not available in jsdom
 // Mock scrollIntoView which is not available in jsdom
 Element.prototype.scrollIntoView = vi.fn()
 Element.prototype.scrollIntoView = vi.fn()
-
-// Suppress console.log during tests to reduce noise.
-// Keep console.error for actual errors.
-const originalConsoleLog = console.log
-const originalConsoleWarn = console.warn
-const originalConsoleInfo = console.info
-
-console.log = () => {}
-console.warn = () => {}
-console.info = () => {}
-
-afterAll(() => {
-	console.log = originalConsoleLog
-	console.warn = originalConsoleWarn
-	console.info = originalConsoleInfo
-})