Skip to content

Commit 295da9d

Browse files
theturtle32Karim13014claude
authored
Suppress debug output during test runs (#501)
* Suppress debug output during test runs Clean up test output by preventing debug logging from cluttering test results while maintaining full debug functionality in production. Changes: - WebSocketConnection: Only call printOutput() in non-test environments - test/shared/setup.mjs: Disable debug module by default during tests - test/unit/core/utils.test.mjs: Properly cleanup debug state in afterEach - test/unit/core/utils-enhanced.test.mjs: Fix test assertions and cleanup - vitest.config.mjs: Add reporter configuration Benefits: - Clean, readable test output without debug noise - Maintains full debug functionality in production - Tests that specifically need debug can enable it explicitly - All 632 tests passing with zero debug output Before: Tests showed verbose socket events, errors, and debug messages After: Clean test output showing only test results 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Address Gemini code review comments Improvements based on Gemini's medium-priority feedback: 1. Centralize debug module imports: - test/shared/setup.mjs: Use import at top instead of require in hooks - test/unit/core/utils.test.mjs: Add import debug at top, reuse in hooks - test/unit/core/utils-enhanced.test.mjs: Add import debug at top, reuse in hooks 2. Clean up redundant configuration: - vitest.config.mjs: Remove redundant silent and reporters defaults Benefits: - Consistent ES module import style across all test files - DRY principle - import once, reuse throughout the file - Cleaner vitest configuration without unnecessary defaults All 632 tests still passing with zero debug output. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude Code <claude-code@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent e35b2bd commit 295da9d

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

lib/WebSocketConnection.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,10 @@ class WebSocketConnection extends EventEmitter {
313313
this.emit('error', error);
314314
}
315315
this.socket.destroy();
316-
this._debug.printOutput();
316+
// Only print debug output in non-test environments to avoid cluttering test output
317+
if (process.env.NODE_ENV !== 'test') {
318+
this._debug.printOutput();
319+
}
317320
}
318321

319322
handleSocketEnd() {

test/shared/setup.mjs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
import { beforeEach, afterEach, vi } from 'vitest';
22
import { stopAllServers } from '../helpers/test-server.mjs';
3+
import debug from 'debug';
34

45
// Increase max listeners to avoid warnings when running many tests with child processes
56
// Vitest adds exit/beforeExit listeners for each test file with spawned processes
67
process.setMaxListeners(30);
78

9+
// Disable debug output during tests unless explicitly enabled
10+
if (!process.env.DEBUG) {
11+
debug.disable();
12+
}
13+
814
// Global test setup for each test file
915
beforeEach(() => {
1016
// Clear all mocks and timers
1117
vi.clearAllTimers();
1218
vi.clearAllMocks();
19+
20+
// Note: We don't disable debug in beforeEach as some tests need to enable it
21+
// Tests that enable DEBUG should clean up properly in their own afterEach
1322
});
1423

1524
afterEach(async () => {
1625
// Restore all mocks
1726
vi.restoreAllMocks();
18-
27+
1928
// Clean up any test servers
2029
await stopAllServers();
30+
31+
// Re-disable debug after each test to prevent leakage
32+
if (!process.env.DEBUG) {
33+
debug.disable();
34+
}
2135
});
2236

2337
// Set up global test configuration

test/unit/core/utils-enhanced.test.mjs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
1111
import * as utils from '../../../lib/utils.js';
12+
import debug from 'debug';
1213

1314
describe('Utils Module - Enhanced Coverage', () => {
1415
describe('BufferingLogger.printOutput() behavior', () => {
@@ -20,8 +21,12 @@ describe('Utils Module - Enhanced Coverage', () => {
2021
});
2122

2223
afterEach(() => {
24+
debug.disable();
2325
if (originalDebugEnv !== undefined) {
2426
process.env.DEBUG = originalDebugEnv;
27+
if (originalDebugEnv) {
28+
debug.enable(originalDebugEnv);
29+
}
2530
} else {
2631
delete process.env.DEBUG;
2732
}
@@ -62,8 +67,12 @@ describe('Utils Module - Enhanced Coverage', () => {
6267
});
6368

6469
afterEach(() => {
70+
debug.disable();
6571
if (originalDebugEnv !== undefined) {
6672
process.env.DEBUG = originalDebugEnv;
73+
if (originalDebugEnv) {
74+
debug.enable(originalDebugEnv);
75+
}
6776
} else {
6877
delete process.env.DEBUG;
6978
}
@@ -81,9 +90,12 @@ describe('Utils Module - Enhanced Coverage', () => {
8190

8291
expect(mockLog).toHaveBeenCalled();
8392
// Verify the format includes timestamp and uniqueID
93+
// printOutput calls logFunction.apply(global, args) where args includes:
94+
// [formatString, date, uniqueID, ...originalArgs]
8495
const firstCall = mockLog.mock.calls[0];
8596
expect(firstCall).toBeDefined();
86-
expect(firstCall[0]).toContain('test-id');
97+
// The uniqueID should be in args[2] (after formatString and date)
98+
expect(firstCall[2]).toBe('test-id');
8799
}
88100
});
89101

test/unit/core/utils.test.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
1212
import { EventEmitter } from 'events';
1313
import * as utils from '../../../lib/utils.js';
14+
import debug from 'debug';
1415

1516
describe('Utils Module', () => {
1617
describe('noop()', () => {
@@ -251,8 +252,12 @@ describe('Utils Module', () => {
251252
});
252253

253254
afterEach(() => {
255+
debug.disable();
254256
if (originalDebugEnv !== undefined) {
255257
process.env.DEBUG = originalDebugEnv;
258+
if (originalDebugEnv) {
259+
debug.enable(originalDebugEnv);
260+
}
256261
} else {
257262
delete process.env.DEBUG;
258263
}

0 commit comments

Comments
 (0)