Testing with SyntropyLog
Use the syntropylog/testing entry point for mocks and helpers. No framework initialization or shutdown in tests; no Redis, HTTP, or broker in the core package.
What the package provides
From syntropylog/testing:
createTestHelper(spyFn?)— Returns{ mockSyntropyLog, beforeEach, afterEach }. UsemockSyntropyLogin place of the realsyntropyLogin tests.createSyntropyLogMock(spyFn?)— Creates a mock framework instance (getLogger, getContextManager, etc.).createServiceWithMock(ServiceClass, mockSyntropyLog)— Instantiates a service with the mock injected.SpyTransport— Transport that captures log entries in memory for assertions.MockContextManager— In-memory context manager for tests.
The core package does not include Redis, HTTP, or broker mocks. Use your own mocks or stubs for those.
No init in tests (zero boilerplate for tests)
No init/shutdown in tests:
import { createTestHelper } from 'syntropylog/testing';
const testHelper = createTestHelper(vi.fn); // or jest.fn
describe('UserService', () => {
beforeEach(() => testHelper.beforeEach());
it('should create user', async () => {
const service = createServiceWithMock(UserService, testHelper.mockSyntropyLog);
const result = await service.createUser({ name: 'John' });
expect(result).toHaveProperty('userId');
});
});
Spy function injection
Pass your test framework’s spy so mocks behave consistently:
// Vitest
const testHelper = createTestHelper(vi.fn);
// Jest
const testHelper = createTestHelper(jest.fn);
Capturing logs with SpyTransport
Use SpyTransport to assert on log output:
import { SpyTransport, createSyntropyLogMock } from 'syntropylog/testing';
const spyTransport = new SpyTransport();
const mock = createSyntropyLogMock();
// Attach spyTransport to the mock logger as needed for your test setup
// Then assert:
// expect(spyTransport.getEntries()).toContainEqual(expect.objectContaining({ level: 'info', message: '...' }));
Runnable examples (syntropylog-examples)
Testing examples 13–16 in the repo:
13-testing-patterns— Vitest14-testing-patterns-jest— Jest15-testing-serializers— Serializers16-testing-transports-concepts— Transport concepts
See the Examples page for the full list (00–17).
Testing guides in this site
- Testing with Vitest
- Testing with Jest
- Testing Redis / cache context
- Testing serializers
- Testing transport concepts
Best practices
- Use
createTestHelper()orcreateSyntropyLogMock()so tests don’t depend on real init/shutdown. - Test behavior and outcomes, not framework internals.
- Use
SpyTransportwhen you need to assert on log entries. - Keep production and testing imports separate:
syntropylogfor app code,syntropylog/testingfor tests.
Related
- Getting Started
- API Reference
- syntropylog-examples (00–17; see README for the list)