summaryrefslogtreecommitdiff
path: root/tst/pretty_json_console.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tst/pretty_json_console.test.ts')
-rw-r--r--tst/pretty_json_console.test.ts40
1 files changed, 40 insertions, 0 deletions
diff --git a/tst/pretty_json_console.test.ts b/tst/pretty_json_console.test.ts
new file mode 100644
index 0000000..f4043ad
--- /dev/null
+++ b/tst/pretty_json_console.test.ts
@@ -0,0 +1,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();
+ });
+});