From a9810db1cc09dfe11dba312776d788e147351ec0 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Mon, 17 Jan 2022 20:46:38 +0800 Subject: [PATCH] fix(logger): properly stringify objects for logging (#6384) * fix(logger): properly stringify objects for logging * Add tests --- .../src/__tests__/index.test.ts | 104 ++++++++++++++++++ packages/docusaurus-logger/src/index.ts | 15 ++- .../src/__tests__/index.test.ts | 14 ++- 3 files changed, 124 insertions(+), 9 deletions(-) diff --git a/packages/docusaurus-logger/src/__tests__/index.test.ts b/packages/docusaurus-logger/src/__tests__/index.test.ts index 8436de916c..5ae55907b7 100644 --- a/packages/docusaurus-logger/src/__tests__/index.test.ts +++ b/packages/docusaurus-logger/src/__tests__/index.test.ts @@ -71,3 +71,107 @@ describe('interpolate', () => { ); }); }); + +describe('info', () => { + const consoleMock = jest.spyOn(console, 'info').mockImplementation(() => {}); + test('should print objects', () => { + logger.info({a: 1}); + logger.info(undefined); + logger.info([1, 2, 3]); + logger.info(new Date(2021, 10, 13)); + expect(consoleMock.mock.calls).toMatchInlineSnapshot(` + Array [ + Array [ + "[INFO] {\\"a\\":1}", + ], + Array [ + "[INFO] undefined", + ], + Array [ + "[INFO] 1,2,3", + ], + Array [ + "[INFO] Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)", + ], + ] + `); + }); +}); + +describe('warn', () => { + const consoleMock = jest.spyOn(console, 'warn').mockImplementation(() => {}); + test('should print objects', () => { + logger.warn({a: 1}); + logger.warn(undefined); + logger.warn([1, 2, 3]); + logger.warn(new Date(2021, 10, 13)); + expect(consoleMock.mock.calls).toMatchInlineSnapshot(` + Array [ + Array [ + "[WARNING] {\\"a\\":1}", + ], + Array [ + "[WARNING] undefined", + ], + Array [ + "[WARNING] 1,2,3", + ], + Array [ + "[WARNING] Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)", + ], + ] + `); + }); +}); + +describe('error', () => { + const consoleMock = jest.spyOn(console, 'error').mockImplementation(() => {}); + test('should print objects', () => { + logger.error({a: 1}); + logger.error(undefined); + logger.error([1, 2, 3]); + logger.error(new Date(2021, 10, 13)); + expect(consoleMock.mock.calls).toMatchInlineSnapshot(` + Array [ + Array [ + "[ERROR] {\\"a\\":1}", + ], + Array [ + "[ERROR] undefined", + ], + Array [ + "[ERROR] 1,2,3", + ], + Array [ + "[ERROR] Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)", + ], + ] + `); + }); +}); + +describe('success', () => { + const consoleMock = jest.spyOn(console, 'log').mockImplementation(() => {}); + test('should print objects', () => { + logger.success({a: 1}); + logger.success(undefined); + logger.success([1, 2, 3]); + logger.success(new Date(2021, 10, 13)); + expect(consoleMock.mock.calls).toMatchInlineSnapshot(` + Array [ + Array [ + "[SUCCESS] {\\"a\\":1}", + ], + Array [ + "[SUCCESS] undefined", + ], + Array [ + "[SUCCESS] 1,2,3", + ], + Array [ + "[SUCCESS] Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)", + ], + ] + `); + }); +}); diff --git a/packages/docusaurus-logger/src/index.ts b/packages/docusaurus-logger/src/index.ts index 17e57d38ad..5684cec8fe 100644 --- a/packages/docusaurus-logger/src/index.ts +++ b/packages/docusaurus-logger/src/index.ts @@ -52,6 +52,13 @@ function interpolate( return res; } +function stringify(msg: unknown): string { + if (String(msg) === '[object Object]') { + return JSON.stringify(msg); + } + return String(msg); +} + function info(msg: unknown): void; function info( msg: TemplateStringsArray, @@ -61,7 +68,7 @@ function info(msg: unknown, ...values: InterpolatableValue[]): void { console.info( `${chalk.cyan(chalk.bold('[INFO]'))} ${ values.length === 0 - ? msg + ? stringify(msg) : interpolate(msg as TemplateStringsArray, ...values) }`, ); @@ -76,7 +83,7 @@ function warn(msg: unknown, ...values: InterpolatableValue[]): void { chalk.yellow( `${chalk.bold('[WARNING]')} ${ values.length === 0 - ? msg + ? stringify(msg) : interpolate(msg as TemplateStringsArray, ...values) }`, ), @@ -92,7 +99,7 @@ function error(msg: unknown, ...values: InterpolatableValue[]): void { chalk.red( `${chalk.bold('[ERROR]')} ${ values.length === 0 - ? msg + ? stringify(msg) : interpolate(msg as TemplateStringsArray, ...values) }`, ), @@ -107,7 +114,7 @@ function success(msg: unknown, ...values: InterpolatableValue[]): void { console.log( `${chalk.green(chalk.bold('[SUCCESS]'))} ${ values.length === 0 - ? msg + ? stringify(msg) : interpolate(msg as TemplateStringsArray, ...values) }`, ); diff --git a/packages/docusaurus-utils/src/__tests__/index.test.ts b/packages/docusaurus-utils/src/__tests__/index.test.ts index 93edef9640..dfbeb26021 100644 --- a/packages/docusaurus-utils/src/__tests__/index.test.ts +++ b/packages/docusaurus-utils/src/__tests__/index.test.ts @@ -321,11 +321,15 @@ describe('mapAsyncSequential', () => { const timeTotal = timeAfter - timeBefore; const totalTimeouts = sum(Object.values(itemToTimeout)); - expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts - 5); + expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts - 20); expect(itemMapStartsAt['1']).toBeGreaterThanOrEqual(0); - expect(itemMapStartsAt['2']).toBeGreaterThanOrEqual(itemMapEndsAt['1'] - 5); - expect(itemMapStartsAt['3']).toBeGreaterThanOrEqual(itemMapEndsAt['2'] - 5); + expect(itemMapStartsAt['2']).toBeGreaterThanOrEqual( + itemMapEndsAt['1'] - 20, + ); + expect(itemMapStartsAt['3']).toBeGreaterThanOrEqual( + itemMapEndsAt['2'] - 20, + ); }); }); @@ -353,8 +357,8 @@ describe('findAsyncSequential', () => { expect(findFn).toHaveBeenNthCalledWith(2, '2'); const timeTotal = timeAfter - timeBefore; - expect(timeTotal).toBeGreaterThanOrEqual(95); - expect(timeTotal).toBeLessThan(105); + expect(timeTotal).toBeGreaterThanOrEqual(80); + expect(timeTotal).toBeLessThan(120); }); });