-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
118 lines (106 loc) · 2.71 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require('mocha');
const Assert = require('assert');
const Sinon = require('sinon');
const Logger = require('./dist');
const levels = Logger.levels.filter((level)=> {
return (level !== Logger.levels.fatal);
});
describe('Logger', function () {
const {console} = global;
let logger;
beforeEach(function () {
for (const level of levels) {
Sinon.spy(console, level);
}
logger = new Logger({
name: 'fucky',
hello: 10
});
Logger.config = '*|trace';
});
afterEach(function () {
for (const level of levels) {
console[level].restore();
}
});
function getLastCallArgs ({level}) {
const fn = (level === 'fatal') ? console.error : console[level];
const call = fn.getCall(0);
return call ? call.args : null;
}
function assertLastCallArgs ({level, output}) {
const args = getLastCallArgs({level});
delete args[1].time;
Assert.deepEqual(args, output);
}
describe('should support', function () {
for (const level of Logger.levels) {
it(`.${level}`, function () {
logger[level]('hello');
assertLastCallArgs({
level,
output: [
'hello',
{
hello: 10,
message: 'hello',
name: 'fucky'
}
]
});
});
}
});
describe('.child', function () {
it('should create child with augmented context', function () {
const child = logger.child({barf: 'borf'});
child.info('donky');
assertLastCallArgs({
level: 'info',
output: [
'donky',
{
name: 'fucky',
hello: 10,
barf: 'borf',
message: 'donky'
}
]
});
});
});
describe('should handle logging errors', function () {
it('single error argument', function () {
const error = new Error('Honk');
logger.error(error);
const args = getLastCallArgs({
level: 'error'
});
const message = args[0];
const body = args[1];
Assert.equal(message, 'Honk');
Assert.equal(body.hello, 10);
});
it('custom error message', function () {
const error = new Error('Honk');
logger.error('Womp', error);
const args = getLastCallArgs({
level: 'error'
});
const message = args[0];
const body = args[1];
Assert.equal(message, 'Womp');
Assert.equal(body.hello, 10);
});
});
describe('should disable and stuff', function () {
it('should disable on level', function () {
Logger.config = '*|fatal';
logger.info('Funk!');
const args = getLastCallArgs({
level: 'info'
});
Assert.equal(args, null);
});
});
});