fix(logger): properly stringify objects for logging (#6384)

* fix(logger): properly stringify objects for logging

* Add tests
This commit is contained in:
Joshua Chen 2022-01-17 20:46:38 +08:00 committed by GitHub
parent cb747025e8
commit a9810db1cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 9 deletions

View file

@ -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)",
],
]
`);
});
});