diff --git a/lib/WebSocketConnection.js b/lib/WebSocketConnection.js index 377118f0..62f42bb0 100644 --- a/lib/WebSocketConnection.js +++ b/lib/WebSocketConnection.js @@ -313,7 +313,10 @@ class WebSocketConnection extends EventEmitter { this.emit('error', error); } this.socket.destroy(); - this._debug.printOutput(); + // Only print debug output in non-test environments to avoid cluttering test output + if (process.env.NODE_ENV !== 'test') { + this._debug.printOutput(); + } } handleSocketEnd() { diff --git a/test/shared/setup.mjs b/test/shared/setup.mjs index f00a742c..e316427d 100644 --- a/test/shared/setup.mjs +++ b/test/shared/setup.mjs @@ -1,23 +1,37 @@ import { beforeEach, afterEach, vi } from 'vitest'; import { stopAllServers } from '../helpers/test-server.mjs'; +import debug from 'debug'; // Increase max listeners to avoid warnings when running many tests with child processes // Vitest adds exit/beforeExit listeners for each test file with spawned processes process.setMaxListeners(30); +// Disable debug output during tests unless explicitly enabled +if (!process.env.DEBUG) { + debug.disable(); +} + // Global test setup for each test file beforeEach(() => { // Clear all mocks and timers vi.clearAllTimers(); vi.clearAllMocks(); + + // Note: We don't disable debug in beforeEach as some tests need to enable it + // Tests that enable DEBUG should clean up properly in their own afterEach }); afterEach(async () => { // Restore all mocks vi.restoreAllMocks(); - + // Clean up any test servers await stopAllServers(); + + // Re-disable debug after each test to prevent leakage + if (!process.env.DEBUG) { + debug.disable(); + } }); // Set up global test configuration diff --git a/test/unit/core/utils-enhanced.test.mjs b/test/unit/core/utils-enhanced.test.mjs index fbcbae37..f2ec89a9 100644 --- a/test/unit/core/utils-enhanced.test.mjs +++ b/test/unit/core/utils-enhanced.test.mjs @@ -9,6 +9,7 @@ */ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import * as utils from '../../../lib/utils.js'; +import debug from 'debug'; describe('Utils Module - Enhanced Coverage', () => { describe('BufferingLogger.printOutput() behavior', () => { @@ -20,8 +21,12 @@ describe('Utils Module - Enhanced Coverage', () => { }); afterEach(() => { + debug.disable(); if (originalDebugEnv !== undefined) { process.env.DEBUG = originalDebugEnv; + if (originalDebugEnv) { + debug.enable(originalDebugEnv); + } } else { delete process.env.DEBUG; } @@ -62,8 +67,12 @@ describe('Utils Module - Enhanced Coverage', () => { }); afterEach(() => { + debug.disable(); if (originalDebugEnv !== undefined) { process.env.DEBUG = originalDebugEnv; + if (originalDebugEnv) { + debug.enable(originalDebugEnv); + } } else { delete process.env.DEBUG; } @@ -81,9 +90,12 @@ describe('Utils Module - Enhanced Coverage', () => { expect(mockLog).toHaveBeenCalled(); // Verify the format includes timestamp and uniqueID + // printOutput calls logFunction.apply(global, args) where args includes: + // [formatString, date, uniqueID, ...originalArgs] const firstCall = mockLog.mock.calls[0]; expect(firstCall).toBeDefined(); - expect(firstCall[0]).toContain('test-id'); + // The uniqueID should be in args[2] (after formatString and date) + expect(firstCall[2]).toBe('test-id'); } }); diff --git a/test/unit/core/utils.test.mjs b/test/unit/core/utils.test.mjs index 91271ce2..79175d10 100644 --- a/test/unit/core/utils.test.mjs +++ b/test/unit/core/utils.test.mjs @@ -11,6 +11,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { EventEmitter } from 'events'; import * as utils from '../../../lib/utils.js'; +import debug from 'debug'; describe('Utils Module', () => { describe('noop()', () => { @@ -251,8 +252,12 @@ describe('Utils Module', () => { }); afterEach(() => { + debug.disable(); if (originalDebugEnv !== undefined) { process.env.DEBUG = originalDebugEnv; + if (originalDebugEnv) { + debug.enable(originalDebugEnv); + } } else { delete process.env.DEBUG; }