Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/WebSocketConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 15 additions & 1 deletion test/shared/setup.mjs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 13 additions & 1 deletion test/unit/core/utils-enhanced.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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');
}
});

Expand Down
5 changes: 5 additions & 0 deletions test/unit/core/utils.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down Expand Up @@ -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;
}
Expand Down