log4js-node/test/tap/LoggingEvent-test.js
LoneRifle 29c92211f2 fix(#789): use flatted in place of CircularJSON
CircularJSON is now deprecated, so replace it with its successor
and use it in the sole location where we used CircularJSON, ie
in the LoggingEvent serialization/deserialization functions

* npm - install flatted, remove circular-json
* LoggingEvent - use flatted for serialization/deserialization
* tests - acknowledge that flatted (and circular-json) should not
  parse anything other than their respective serialized payloads
  and use flatted when parsing/stringifying test data
2018-11-04 15:32:28 +08:00

44 lines
1.6 KiB
JavaScript

const flatted = require('flatted');
const test = require('tap').test;
const LoggingEvent = require('../../lib/LoggingEvent');
const levels = require('../../lib/levels');
test('LoggingEvent', (batch) => {
batch.test('should serialise to flatted', (t) => {
const event = new LoggingEvent('cheese', levels.DEBUG, ['log message'], { user: 'bob' });
// set the event date to a known value
event.startTime = new Date(Date.UTC(2018, 1, 4, 18, 30, 23, 10));
const rehydratedEvent = flatted.parse(event.serialise());
t.equal(rehydratedEvent.startTime, '2018-02-04T18:30:23.010Z');
t.equal(rehydratedEvent.categoryName, 'cheese');
t.equal(rehydratedEvent.level.levelStr, 'DEBUG');
t.equal(rehydratedEvent.data.length, 1);
t.equal(rehydratedEvent.data[0], 'log message');
t.equal(rehydratedEvent.context.user, 'bob');
t.end();
});
batch.test('should deserialise from flatted', (t) => {
const dehydratedEvent = flatted.stringify({
startTime: '2018-02-04T10:25:23.010Z',
categoryName: 'biscuits',
level: {
levelStr: 'INFO'
},
data: ['some log message', { x: 1 }],
context: { thing: 'otherThing' }
});
const event = LoggingEvent.deserialise(dehydratedEvent);
t.type(event, LoggingEvent);
t.same(event.startTime, new Date(Date.UTC(2018, 1, 4, 10, 25, 23, 10)));
t.equal(event.categoryName, 'biscuits');
t.same(event.level, levels.INFO);
t.equal(event.data[0], 'some log message');
t.equal(event.data[1].x, 1);
t.equal(event.context.thing, 'otherThing');
t.end();
});
batch.end();
});