mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-30 10:48:05 +02:00
fix(logger): properly stringify objects for logging (#6384)
* fix(logger): properly stringify objects for logging * Add tests
This commit is contained in:
parent
cb747025e8
commit
a9810db1cc
3 changed files with 124 additions and 9 deletions
|
@ -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 [
|
||||
"[36m[1m[INFO][22m[39m {\\"a\\":1}",
|
||||
],
|
||||
Array [
|
||||
"[36m[1m[INFO][22m[39m undefined",
|
||||
],
|
||||
Array [
|
||||
"[36m[1m[INFO][22m[39m 1,2,3",
|
||||
],
|
||||
Array [
|
||||
"[36m[1m[INFO][22m[39m 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 [
|
||||
"[33m[1m[WARNING][22m {\\"a\\":1}[39m",
|
||||
],
|
||||
Array [
|
||||
"[33m[1m[WARNING][22m undefined[39m",
|
||||
],
|
||||
Array [
|
||||
"[33m[1m[WARNING][22m 1,2,3[39m",
|
||||
],
|
||||
Array [
|
||||
"[33m[1m[WARNING][22m Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)[39m",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
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 [
|
||||
"[31m[1m[ERROR][22m {\\"a\\":1}[39m",
|
||||
],
|
||||
Array [
|
||||
"[31m[1m[ERROR][22m undefined[39m",
|
||||
],
|
||||
Array [
|
||||
"[31m[1m[ERROR][22m 1,2,3[39m",
|
||||
],
|
||||
Array [
|
||||
"[31m[1m[ERROR][22m Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)[39m",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
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 [
|
||||
"[32m[1m[SUCCESS][22m[39m {\\"a\\":1}",
|
||||
],
|
||||
Array [
|
||||
"[32m[1m[SUCCESS][22m[39m undefined",
|
||||
],
|
||||
Array [
|
||||
"[32m[1m[SUCCESS][22m[39m 1,2,3",
|
||||
],
|
||||
Array [
|
||||
"[32m[1m[SUCCESS][22m[39m Sat Nov 13 2021 00:00:00 GMT+0000 (Coordinated Universal Time)",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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)
|
||||
}`,
|
||||
);
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue