summaryrefslogtreecommitdiff
path: root/tst/pretty_json_console.test.ts
blob: f4043adfff67bb33fb74ffe77abffbed545a00c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { ANSI, LogLevel, PrettyJsonConsoleLogger } from '../lib/index';

describe('trace/log/pretty_json_console (PrettyJsonConsoleLogger)', () => {
    test('logs to console.log for non-error', () => {
        const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
        const err = jest.spyOn(console, 'error').mockImplementation(() => undefined);

        const logger = new PrettyJsonConsoleLogger();
        logger.log(LogLevel.INFO, 'hello');

        expect(log).toHaveBeenCalledTimes(1);
        expect(err).not.toHaveBeenCalled();

        const payload = log.mock.calls[0][0] as string;
        expect(payload).toContain('"level": "INFO"');
        expect(payload).toContain('"trace":');
        expect(payload).toContain(ANSI.RESET);

        log.mockRestore();
        err.mockRestore();
    });

    test('logs to console.error for ERROR', () => {
        const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
        const err = jest.spyOn(console, 'error').mockImplementation(() => undefined);

        const logger = new PrettyJsonConsoleLogger();
        logger.log(LogLevel.ERROR, 'boom');

        expect(err).toHaveBeenCalledTimes(1);
        expect(log).not.toHaveBeenCalled();

        const payload = err.mock.calls[0][0] as string;
        expect(payload.startsWith(ANSI.BRIGHT_RED)).toBe(true);
        expect(payload.endsWith(`${ANSI.RESET}\n`)).toBe(true);

        log.mockRestore();
        err.mockRestore();
    });
});