From 38b523d2c0c27824dada9cfc2194b9d6118732f3 Mon Sep 17 00:00:00 2001 From: Bojan Kucera Date: Sat, 7 Jun 2025 12:22:45 -0400 Subject: [PATCH] fixed logger tests --- libs/logger/test/advanced.test.ts | 18 ++++++++---------- libs/logger/test/basic.test.ts | 7 ++++--- libs/logger/test/integration.test.ts | 8 +++++--- libs/logger/test/setup.ts | 7 ++++--- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/libs/logger/test/advanced.test.ts b/libs/logger/test/advanced.test.ts index e6e82bf..889c2ae 100644 --- a/libs/logger/test/advanced.test.ts +++ b/libs/logger/test/advanced.test.ts @@ -6,7 +6,7 @@ */ import { describe, it, expect, beforeEach, afterEach } from 'bun:test'; -import { Logger } from '../src'; +import { Logger, shutdownLoggers } from '../src'; import { loggerTestHelpers } from './setup'; describe('Advanced Logger Features', () => { @@ -16,14 +16,10 @@ describe('Advanced Logger Features', () => { beforeEach(() => { testLoggerInstance = loggerTestHelpers.createTestLogger('advanced-features'); logger = testLoggerInstance.logger; - }); - - afterEach(() => { + }); afterEach(async () => { testLoggerInstance.clearCapturedLogs(); - // Force garbage collection to clean up any potential circular references - if (global.gc) { - global.gc(); - } + // Clear any global logger cache + await shutdownLoggers(); }); describe('Complex Metadata Handling', () => { @@ -148,8 +144,7 @@ describe('Advanced Logger Features', () => { expect(logs.length).toBe(1); expect(logs[0].context).toBe('batch processing'); }); - - it('should handle error objects with circular references', () => { + it('should handle error objects with circular references', () => { const errorWithCircular: any = { name: 'CircularError', message: 'Circular reference error' }; // Create a simple circular reference errorWithCircular.self = errorWithCircular; @@ -162,6 +157,9 @@ describe('Advanced Logger Features', () => { const logs = testLoggerInstance.getCapturedLogs(); expect(logs.length).toBe(1); expect(logs[0].level).toBe('error'); + + // Clean up circular reference to prevent memory issues + delete errorWithCircular.self; }); }); describe('Performance and Edge Cases', () => { diff --git a/libs/logger/test/basic.test.ts b/libs/logger/test/basic.test.ts index 824e68e..b82e4d7 100644 --- a/libs/logger/test/basic.test.ts +++ b/libs/logger/test/basic.test.ts @@ -5,7 +5,7 @@ */ import { describe, it, expect, beforeEach, afterEach } from 'bun:test'; -import { Logger, getLogger, createLogger } from '../src'; +import { Logger, getLogger, createLogger, shutdownLoggers } from '../src'; import { loggerTestHelpers } from './setup'; describe('Basic Logger Tests', () => { @@ -16,9 +16,10 @@ describe('Basic Logger Tests', () => { testLoggerInstance = loggerTestHelpers.createTestLogger('utils-test'); logger = testLoggerInstance.logger; }); - - afterEach(() => { + afterEach(async () => { testLoggerInstance.clearCapturedLogs(); + // Clear any global logger cache + await shutdownLoggers(); }); describe('Logger Factory Functions', () => { diff --git a/libs/logger/test/integration.test.ts b/libs/logger/test/integration.test.ts index ee4ad7c..ccf9073 100644 --- a/libs/logger/test/integration.test.ts +++ b/libs/logger/test/integration.test.ts @@ -8,7 +8,8 @@ import { describe, it, expect, beforeEach, afterEach } from 'bun:test'; import { Logger, createLogger, - getLogger + getLogger, + shutdownLoggers } from '../src'; import { loggerTestHelpers } from './setup'; @@ -20,9 +21,10 @@ describe('Logger Integration Tests', () => { testLoggerInstance = loggerTestHelpers.createTestLogger('integration-test'); logger = testLoggerInstance.logger; }); - - afterEach(() => { + afterEach(async () => { testLoggerInstance.clearCapturedLogs(); + // Clear any global logger cache + await shutdownLoggers(); }); describe('Core Logger Functionality', () => { diff --git a/libs/logger/test/setup.ts b/libs/logger/test/setup.ts index 055b802..6f7eb74 100644 --- a/libs/logger/test/setup.ts +++ b/libs/logger/test/setup.ts @@ -5,7 +5,7 @@ * Provides utilities and mocks for testing logging operations. */ -import { Logger, LogMetadata } from '../src'; +import { Logger, LogMetadata, shutdownLoggers } from '../src'; import { afterAll, afterEach, beforeAll, beforeEach } from 'bun:test'; // Store original console methods @@ -178,8 +178,9 @@ beforeAll(() => { }); // Clean up after each test -afterEach(() => { - // loggerTestHelpers.clearCapturedLogs(); // REMOVE this if it targeted a global array +afterEach(async () => { + // Clear logger cache to prevent state pollution between tests + await shutdownLoggers(); }); // Restore everything after tests