-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.spec.js
123 lines (107 loc) · 3.36 KB
/
logger.spec.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
119
120
121
122
123
const logger = require('./logger');
function getRegExp(msg, prefix) {
if (msg) {
msg = ' ' + msg;
}
return new RegExp(`^${(prefix ? prefix + ' ' : '')}\\[[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}(?::[0-9]{2}){2}.[0-9]{2}\\]${msg ? msg : ''}$`);
}
describe('logger', () => {
const originalLog = console.log;
const originalLevel = logger.getLogLevel();
let logOutput = [];
beforeAll(() => console.log = (msg) => logOutput.push(msg));
beforeEach(() => {
logOutput = []
logger.setLogLevel(originalLevel);
});
afterAll(() => console.log = originalLog);
test('logExpression string', () => {
logger.logExpression('test');
expect(logOutput).toHaveLength(1);
expect(logOutput[0]).toMatch(
getRegExp('test')
);
});
test('logExpression object', () => {
const obj = {test: true};
logger.logExpression(obj);
expect(logOutput).toHaveLength(2);
expect(logOutput[0]).toMatch(getRegExp());
expect(logOutput[1]).toMatch(JSON.stringify(obj, null, 2));
});
test('logExpression, prefix', () => {
logger.logExpression('test', 0, 'info');
expect(logOutput).toHaveLength(1);
expect(logOutput[0]).toMatch(getRegExp('test', 'info'));
});
test('logExpression, no prefix, level', () => {
logger.logExpression('test', 0);
logger.logExpression('test 1', 1);
logger.logExpression('test 2', 2);
expect(logOutput).toHaveLength(2);
expect(logOutput[0]).toMatch(
getRegExp('test')
);
expect(logOutput[1]).toMatch(
getRegExp('test 1')
);
});
test('logExpression after setLogLevel', () => {
logger.logExpression('test', 1);
logger.logExpression('test', 2);
expect(logOutput).toHaveLength(1);
logger.setLogLevel(2);
logger.logExpression('test', 2);
expect(logOutput).toHaveLength(2);
});
test('logExpression after setLoggerEnabled', () => {
logger.logExpression('test');
logger.setLoggerEnabled(false);
logger.logExpression('test 1');
expect(logOutput).toHaveLength(1);
logger.setLoggerEnabled(true);
logger.logExpression('test 2');
expect(logOutput).toHaveLength(2);
});
test('setLoggerEnabled with string arg', () => {
logger.setLoggerEnabled('false');
logger.logExpression('test');
expect(logOutput).toHaveLength(0);
logger.setLoggerEnabled('true');
logger.logExpression('test');
expect(logOutput).toHaveLength(1);
});
test('debug', () => {
logger.debug('test');
expect(logOutput).toHaveLength(0);
logger.setLogLevel(2);
logger.debug('test');
expect(logOutput).toHaveLength(1);
expect(logOutput[0]).toContain('[debug]');
expect(logOutput[0]).toMatch(/test$/);
});
test('info', () => {
logger.info('test');
expect(logOutput).toHaveLength(1);
expect(logOutput[0]).toContain('[info ]');
expect(logOutput[0]).toMatch(/test$/);
});
test('warn', () => {
logger.warn('test');
expect(logOutput).toHaveLength(1);
expect(logOutput[0]).toContain('[warn ]');
expect(logOutput[0]).toMatch(/test$/);
});
test('error', () => {
logger.error('test');
expect(logOutput).toHaveLength(1);
expect(logOutput[0]).toContain('[error]');
expect(logOutput[0]).toMatch(/test$/);
});
test('fatal', () => {
logger.fatal('test');
expect(logOutput).toHaveLength(1);
expect(logOutput[0]).toContain('[FATAL]');
expect(logOutput[0]).toMatch(/test$/);
})
});