Skip to content

Commit f29a67c

Browse files
committed
test: custom replacer function feature
1 parent da5d748 commit f29a67c

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

packages/logger/tests/unit/Logger.test.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,144 @@ describe('Class: Logger', () => {
13481348
);
13491349
});
13501350
});
1351+
1352+
describe('Feature: custom replacer function', () => {
1353+
test('it should correctly serialize Set values using the provided jsonReplacerFn', () => {
1354+
const jsonReplacerFn: CustomReplacerFn = (
1355+
key: string,
1356+
value: unknown
1357+
) => (value instanceof Set ? [...value] : value);
1358+
1359+
const logger = new Logger({ jsonReplacerFn });
1360+
const consoleSpy = jest.spyOn(
1361+
logger['console'],
1362+
getConsoleMethod(methodOfLogger)
1363+
);
1364+
const message = `This is an ${methodOfLogger} log with Set value`;
1365+
1366+
const logItem = { value: new Set([1, 2]) };
1367+
1368+
// Act
1369+
logger[methodOfLogger](message, logItem);
1370+
1371+
// Assess
1372+
expect(consoleSpy).toBeCalledTimes(1);
1373+
expect(consoleSpy).toHaveBeenNthCalledWith(
1374+
1,
1375+
JSON.stringify({
1376+
level: methodOfLogger.toUpperCase(),
1377+
message: message,
1378+
sampling_rate: 0,
1379+
service: 'hello-world',
1380+
timestamp: '2016-06-20T12:08:10.000Z',
1381+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
1382+
value: [1, 2],
1383+
})
1384+
);
1385+
});
1386+
1387+
test('it is unable to serialize Set values while using the default jsonReplacerFn', () => {
1388+
const logger = new Logger();
1389+
const consoleSpy = jest.spyOn(
1390+
logger['console'],
1391+
getConsoleMethod(methodOfLogger)
1392+
);
1393+
const message = `This is an ${methodOfLogger} log with Set value`;
1394+
1395+
const logItem = { value: new Set([1, 2]) };
1396+
1397+
// Act
1398+
logger[methodOfLogger](message, logItem);
1399+
1400+
// Assess
1401+
expect(consoleSpy).toBeCalledTimes(1);
1402+
expect(consoleSpy).toHaveBeenNthCalledWith(
1403+
1,
1404+
JSON.stringify({
1405+
level: methodOfLogger.toUpperCase(),
1406+
message: message,
1407+
sampling_rate: 0,
1408+
service: 'hello-world',
1409+
timestamp: '2016-06-20T12:08:10.000Z',
1410+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
1411+
value: {},
1412+
})
1413+
);
1414+
});
1415+
1416+
test('it should correctly serialize Map values using the provided jsonReplacerFn', () => {
1417+
const jsonReplacerFn: CustomReplacerFn = (
1418+
key: string,
1419+
value: unknown
1420+
) => (value instanceof Map ? [...value] : value);
1421+
1422+
const logger = new Logger({ jsonReplacerFn });
1423+
const consoleSpy = jest.spyOn(
1424+
logger['console'],
1425+
getConsoleMethod(methodOfLogger)
1426+
);
1427+
const message = `This is an ${methodOfLogger} log with Map value`;
1428+
1429+
const mappedValue = new Map();
1430+
mappedValue.set('foo', 'bar');
1431+
mappedValue.set('baz', 'qux');
1432+
1433+
const logItem = { value: mappedValue };
1434+
1435+
// Act
1436+
logger[methodOfLogger](message, logItem);
1437+
1438+
// Assess
1439+
expect(consoleSpy).toBeCalledTimes(1);
1440+
expect(consoleSpy).toHaveBeenNthCalledWith(
1441+
1,
1442+
JSON.stringify({
1443+
level: methodOfLogger.toUpperCase(),
1444+
message: message,
1445+
sampling_rate: 0,
1446+
service: 'hello-world',
1447+
timestamp: '2016-06-20T12:08:10.000Z',
1448+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
1449+
value: [
1450+
['foo', 'bar'],
1451+
['baz', 'qux'],
1452+
],
1453+
})
1454+
);
1455+
});
1456+
1457+
test('it is unable to serialize Map values while using the default jsonReplacerFn', () => {
1458+
const logger = new Logger();
1459+
const consoleSpy = jest.spyOn(
1460+
logger['console'],
1461+
getConsoleMethod(methodOfLogger)
1462+
);
1463+
const message = `This is an ${methodOfLogger} log with Map value`;
1464+
const mappedValue = new Map();
1465+
mappedValue.set('foo', 'bar');
1466+
mappedValue.set('baz', 'qux');
1467+
1468+
const logItem = { value: mappedValue };
1469+
1470+
// Act
1471+
logger[methodOfLogger](message, logItem);
1472+
1473+
// Assess
1474+
expect(consoleSpy).toBeCalledTimes(1);
1475+
expect(consoleSpy).toHaveBeenNthCalledWith(
1476+
1,
1477+
JSON.stringify({
1478+
level: methodOfLogger.toUpperCase(),
1479+
message: message,
1480+
sampling_rate: 0,
1481+
service: 'hello-world',
1482+
timestamp: '2016-06-20T12:08:10.000Z',
1483+
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
1484+
value: {},
1485+
})
1486+
);
1487+
});
1488+
});
13511489
}
13521490
);
13531491

0 commit comments

Comments
 (0)