Skip to content

Commit cdd572b

Browse files
committedMay 18, 2020
Add a test for the pretty formatter
1 parent 04c6486 commit cdd572b

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"devDependencies": {
1111
"insist": "^0.2.3",
1212
"mocha": "^2.0",
13+
"strip-ansi": "^3.0.0",
1314
"tv4": "^1.1.0"
1415
},
1516
"scripts": {

‎test/pretty_formatter.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
'use strict'
6+
7+
const assert = require('assert');
8+
const stripAnsi = require('strip-ansi');
9+
10+
const NAMESPACE = 'mozlog_pretty'
11+
12+
const mozlog = require('../');
13+
14+
var lastLog;
15+
var writer = {
16+
write: function(str) {
17+
lastLog = stripAnsi(str);
18+
return true;
19+
}
20+
};
21+
22+
const fxaLog = mozlog({
23+
app: NAMESPACE,
24+
fmt: 'pretty',
25+
stream: writer,
26+
});
27+
28+
var logger = fxaLog('test.pretty');
29+
30+
/*global describe,it,beforeEach,afterEach*/
31+
32+
describe('pretty formatter', function() {
33+
it('should serialize objects to JSON strings', function() {
34+
var obj = { foo: { bar: 'baz' }, quux: 'baz' };
35+
logger.info('json', obj);
36+
assert.equal(lastLog, 'mozlog_pretty.test.pretty.INFO: json {"foo":{"bar":"baz"},"quux":"baz"}\n');
37+
});
38+
39+
it('should serialize circular objects', function() {
40+
var obj = { foo: {} };
41+
obj.foo.bar = obj;
42+
logger.info('jsoncircles', obj);
43+
assert.equal(lastLog, 'mozlog_pretty.test.pretty.INFO: jsoncircles {"foo":{"bar":"[Circular]"}}\n');
44+
});
45+
46+
it('should serialize errors', function() {
47+
var err1 = new Error('foo');
48+
logger.error('errors', err1);
49+
assert(lastLog.startsWith('mozlog_pretty.test.pretty.ERROR: errors Error: foo'));
50+
});
51+
52+
it('should serialize nested errors', function() {
53+
var err1 = new Error('foo');
54+
logger.error('errors', { obj: err1 });
55+
assert.equal(lastLog, 'mozlog_pretty.test.pretty.ERROR: errors {"obj":"Error: foo"}\n');
56+
});
57+
58+
it('should coerce messages to strings', function() {
59+
var out = logger.warn('message', 42);
60+
assert.equal(lastLog, 'mozlog_pretty.test.pretty.WARN: message 42\n');
61+
});
62+
});

‎test/schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const assert = require('insist');
88
const tv4 = require('tv4');
99
const intel = require('intel');
1010
const HEKA_SCHEMA = require('./schema.json');
11-
const NAMESPACE = 'mozlog'
11+
const NAMESPACE = 'mozlog_schema'
1212

1313
const mozlog = require('../');
1414

0 commit comments

Comments
 (0)
Please sign in to comment.