Files
renovate/lib/logger/renovate-logger.spec.ts
Jamie Tanna 5cc5fa6ba7 chore: log a warning when logger isn't initialized (yet) (#41843)
In an alternative to #41842, we can also introduce a warning when the
logger has been called before any initialization, as a way to surface
this misconfiguration.
2026-03-12 13:08:51 +00:00

48 lines
1.7 KiB
TypeScript

import { LOGGER_NOT_INITIALIZED } from '../constants/error-messages.ts';
import { RenovateLogger } from './renovate-logger.ts';
import type { BunyanLogger } from './types.ts';
describe('logger/renovate-logger', () => {
it('throws', () => {
const logger = new RenovateLogger('test', {});
expect(() => logger.childLogger()).toThrow(LOGGER_NOT_INITIALIZED);
});
it('should queue logs until initialized', () => {
const fakeLogger = {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
fatal: vi.fn(),
};
const logger = new RenovateLogger('test', {});
logger.info('test');
expect(fakeLogger.info).toHaveBeenCalledTimes(0);
logger.bunyan = fakeLogger as unknown as BunyanLogger;
expect(fakeLogger.info).toHaveBeenCalledTimes(1);
});
describe('before bunyan is initialized', () => {
it('should log to console', () => {
const consoleSpy = vi.spyOn(console, 'warn');
const logger = new RenovateLogger('test', {});
logger.info('hello before init');
expect(consoleSpy).toHaveBeenCalledWith(
`⚠️ NOTE ⚠️: Renovate's logger has not yet been initialized. If you see no other output, this is a bug`,
);
});
it('should not log more than once', () => {
const consoleSpy = vi.spyOn(console, 'warn');
const logger = new RenovateLogger('test', {});
logger.info('hello before init');
logger.info('hello before init');
logger.info('hello before init');
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
`⚠️ NOTE ⚠️: Renovate's logger has not yet been initialized. If you see no other output, this is a bug`,
);
});
});
});