node-bunyan-lite/test/cycles.test.js

91 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2012-09-10 18:24:39 +00:00
/*
* Copyright (c) 2012 Trent Mick. All rights reserved.
*
* Make sure cycles are safe.
*/
var Logger = require('../lib/bunyan.js');
2013-01-07 19:18:12 +00:00
// node-tap API
if (require.cache[__dirname + '/tap4nodeunit.js'])
2013-03-29 00:42:32 +00:00
delete require.cache[__dirname + '/tap4nodeunit.js'];
2013-01-07 19:18:12 +00:00
var tap4nodeunit = require('./tap4nodeunit.js');
var after = tap4nodeunit.after;
var before = tap4nodeunit.before;
var test = tap4nodeunit.test;
2012-09-10 18:24:39 +00:00
var Stream = require('stream').Stream;
var outstr = new Stream;
outstr.writable = true;
var output = [];
outstr.write = function (c) {
2013-03-29 00:42:32 +00:00
output.push(JSON.parse(c + ''));
2012-09-10 18:24:39 +00:00
};
outstr.end = function (c) {
2013-03-29 00:42:32 +00:00
if (c) this.write(c);
this.emit('end');
2012-09-10 18:24:39 +00:00
};
// these are lacking a few fields that will probably never match
var expect =
2013-03-29 00:42:32 +00:00
[
{
'name': 'blammo',
'level': 30,
'msg': 'bango { bang: \'boom\', KABOOM: [Circular] }',
'v': 0
},
{
'name': 'blammo',
'level': 30,
'msg': 'kaboom { bang: \'boom\', KABOOM: [Circular] }',
'v': 0
},
{
'name': 'blammo',
'level': 30,
'bang': 'boom',
'KABOOM': {
'bang': 'boom',
'KABOOM': '[Circular]'
},
'msg': '',
'v': 0
}
];
2012-09-10 18:24:39 +00:00
var log = new Logger({
2013-03-29 00:42:32 +00:00
name: 'blammo',
streams: [
{
type: 'stream',
level: 'info',
stream: outstr
}
]
2012-09-10 18:24:39 +00:00
});
test('cycles', function (t) {
2013-03-29 00:42:32 +00:00
outstr.on('end', function () {
output.forEach(function (o, i) {
// Drop variable parts for comparison.
delete o.hostname;
delete o.pid;
delete o.time;
// Hack object/dict comparison: JSONify.
t.equal(JSON.stringify(o), JSON.stringify(expect[i]),
'log item ' + i + ' matches');
});
t.end();
2012-09-10 18:24:39 +00:00
});
2013-03-29 00:42:32 +00:00
var obj = { bang: 'boom' };
obj.KABOOM = obj;
log.info('bango', obj);
log.info('kaboom', obj.KABOOM);
log.info(obj);
outstr.end();
t.ok('did not throw');
2012-09-10 18:24:39 +00:00
});