UNPKG

syntropylog

Version:

An instance manager with observability for Node.js applications

90 lines 2.56 kB
/** * MockSyntropyLog - Framework Agnostic Mock * * This mock provides a testing-agnostic version of SyntropyLog * that can be used with both Vitest and Jest without conflicts. */ /** * Creates a mock function that works with both Vitest and Jest */ function createMockFn(implementation) { const mockFn = (...args) => { if (implementation) { return implementation(...args); } return undefined; }; // Add mock properties for compatibility mockFn.mockClear = () => { }; mockFn.mockReset = () => { }; mockFn.mockImplementation = (impl) => { return createMockFn(impl); }; mockFn.mockReturnValue = (value) => { return createMockFn(() => value); }; return mockFn; } export class MockSyntropyLog { logger; contextManager; constructor() { this.logger = { info: createMockFn(), error: createMockFn(), warn: createMockFn(), debug: createMockFn(), trace: createMockFn(), }; this.contextManager = { getCorrelationId: createMockFn(() => 'mock-correlation-id'), getTransactionId: createMockFn(() => 'mock-transaction-id'), getCorrelationIdHeaderName: createMockFn(() => 'x-correlation-id'), setCorrelationId: createMockFn(), setTransactionId: createMockFn(), set: createMockFn(), clear: createMockFn(), run: createMockFn(async (fn) => { return await fn(); }), }; } getLogger(name) { return this.logger; } getContextManager() { return this.contextManager; } init(config) { return Promise.resolve(); } shutdown() { return Promise.resolve(); } reset() { // Clear all mock functions Object.values(this.logger).forEach((fn) => { if (typeof fn === 'function' && fn.mockClear) { fn.mockClear(); } }); Object.values(this.contextManager).forEach((fn) => { if (typeof fn === 'function' && fn.mockClear) { fn.mockClear(); } }); } } /** * Creates a test helper that works with any testing framework */ export function createTestHelper() { const mockSyntropyLog = new MockSyntropyLog(); return { mockSyntropyLog, beforeEach: () => { mockSyntropyLog.reset(); }, }; } //# sourceMappingURL=MockSyntropyLog.js.map