|
| 1 | +const flushAsyncWork = () => new Promise<void>((resolve) => setImmediate(resolve)); |
| 2 | + |
| 3 | +const loadLogger = () => { |
| 4 | + jest.unmock('../../../src/utils/logger'); |
| 5 | + return (jest.requireActual('../../../src/utils/logger') as typeof import('../../../src/utils/logger')).logger; |
| 6 | +}; |
| 7 | + |
| 8 | +describe('logger', () => { |
| 9 | + let logSpy: jest.SpiedFunction<typeof console.log>; |
| 10 | + let errorSpy: jest.SpiedFunction<typeof console.error>; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + jest.resetModules(); |
| 14 | + jest.clearAllMocks(); |
| 15 | + logSpy = jest.spyOn(console, 'log').mockImplementation(() => undefined); |
| 16 | + errorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + jest.restoreAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('shows the logo when silent mode is disabled', () => { |
| 24 | + const logger = loadLogger(); |
| 25 | + |
| 26 | + logger.showLogo(); |
| 27 | + |
| 28 | + expect(logSpy).toHaveBeenCalledTimes(1); |
| 29 | + expect(logSpy.mock.calls[0][0]).toContain('_'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('suppresses logo and tagged logs when silent mode is enabled', async () => { |
| 33 | + const logger = loadLogger(); |
| 34 | + |
| 35 | + logger.setSilent(true); |
| 36 | + logger.showLogo(); |
| 37 | + logger.log('CLI', 'hidden message'); |
| 38 | + await flushAsyncWork(); |
| 39 | + |
| 40 | + expect(logSpy).not.toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it.each([ |
| 44 | + 'CLI', |
| 45 | + 'Server', |
| 46 | + 'Livereload', |
| 47 | + 'Announcement', |
| 48 | + 'Other', |
| 49 | + ])('writes the padded tag for %s logs', async (tag) => { |
| 50 | + const logger = loadLogger(); |
| 51 | + const prefix = ` ${tag.padEnd(12, ' ')} `; |
| 52 | + |
| 53 | + logger.log(tag as never, 'plain message'); |
| 54 | + await flushAsyncWork(); |
| 55 | + |
| 56 | + expect(logSpy).toHaveBeenCalledWith(expect.stringContaining(`${prefix} plain message`)); |
| 57 | + }); |
| 58 | + |
| 59 | + it('formats URLs as clickable links and forwards extra arguments', async () => { |
| 60 | + const logger = loadLogger(); |
| 61 | + const url = 'https://example.com/docs'; |
| 62 | + const meta = { ok: true }; |
| 63 | + |
| 64 | + logger.log('Announcement', `Open ${url}`, meta); |
| 65 | + await flushAsyncWork(); |
| 66 | + |
| 67 | + expect(logSpy.mock.calls[0][0]).toContain('Announcement'); |
| 68 | + expect(logSpy).toHaveBeenCalledWith( |
| 69 | + expect.stringContaining(`Open \x1b]8;;${url}\x1b\\${url}\x1b]8;;\x1b\\`), |
| 70 | + meta, |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('writes styled errors to stderr', async () => { |
| 75 | + const logger = loadLogger(); |
| 76 | + const detail = { code: 500 }; |
| 77 | + |
| 78 | + logger.error('something failed', detail); |
| 79 | + await flushAsyncWork(); |
| 80 | + |
| 81 | + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Error'), 'something failed', detail); |
| 82 | + }); |
| 83 | +}); |
0 commit comments