-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
157 lines (132 loc) · 4.53 KB
/
index.test.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import sinon from 'sinon';
import assert from 'assert';
import createLogger, { LogLevel, Formatter, defaultReporter, defaultFormatter } from './index';
it('calls .trace on the reporter', () => {
const reporter = sinon.spy();
const logger = createLogger();
logger.setReporter(reporter);
logger.setLevel(LogLevel.TRACE);
logger.trace('a', 'b', 'c');
assert.strictEqual(reporter.callCount, 1);
assert.deepStrictEqual(reporter.lastCall.args, [['a', 'b', 'c'], 'trace', 'trace']);
});
it('calls .debug on the reporter', () => {
const reporter = sinon.spy();
const logger = createLogger();
logger.setReporter(reporter);
logger.setLevel(LogLevel.DEBUG);
logger.debug('a', 'b', 'c');
assert.strictEqual(reporter.callCount, 1);
assert.deepStrictEqual(reporter.lastCall.args, [['a', 'b', 'c'], 'debug', 'debug']);
});
it('calls .info on the reporter', () => {
const reporter = sinon.spy();
const logger = createLogger();
logger.setReporter(reporter);
logger.setLevel(LogLevel.INFO);
logger.info('a', 'b', 'c');
assert.strictEqual(reporter.callCount, 1);
assert.deepStrictEqual(reporter.lastCall.args, [['a', 'b', 'c'], 'info', 'info']);
});
it('calls .log on the reporter on level INFO', () => {
const reporter = sinon.spy();
const logger = createLogger();
logger.setReporter(reporter);
logger.setLevel(LogLevel.INFO);
logger.log('a', 'b', 'c');
assert.strictEqual(reporter.callCount, 1);
assert.deepStrictEqual(reporter.lastCall.args, [['a', 'b', 'c'], 'log', 'info']);
});
it('does not call .log on the reporter on level higher than INFO', () => {
const reporter = sinon.spy();
const logger = createLogger();
logger.setReporter(reporter);
logger.setLevel(LogLevel.WARN);
logger.log('a', 'b', 'c');
assert.strictEqual(reporter.callCount, 0);
});
it('calls .warn on the reporter', () => {
const reporter = sinon.spy();
const logger = createLogger();
logger.setReporter(reporter);
logger.setLevel(LogLevel.INFO);
logger.warn('a', 'b', 'c');
assert.strictEqual(reporter.callCount, 1);
assert.deepStrictEqual(reporter.lastCall.args, [['a', 'b', 'c'], 'warn', 'warn']);
});
it('calls .error on the reporter', () => {
const reporter = sinon.spy();
const logger = createLogger();
logger.setReporter(reporter);
logger.setLevel(LogLevel.INFO);
logger.error('a', 'b', 'c');
assert.strictEqual(reporter.callCount, 1);
assert.deepStrictEqual(reporter.lastCall.args, [['a', 'b', 'c'], 'error', 'error']);
});
it('does not log below log level', () => {
const reporter = sinon.spy();
const logger = createLogger();
logger.setReporter(reporter);
logger.setLevel(LogLevel.WARN);
logger.info('a', 'b', 'c');
assert.strictEqual(reporter.callCount, 0);
});
it('getLevel returns current logLevel', () => {
const logger = createLogger();
logger.setLevel(LogLevel.DEBUG);
assert.strictEqual(logger.getLevel(), LogLevel.DEBUG);
logger.setLevel(LogLevel.ERROR);
assert.strictEqual(logger.getLevel(), LogLevel.ERROR);
});
it('sets default log level to warn', () => {
const logger = createLogger();
assert.strictEqual(logger.getLevel(), LogLevel.WARN);
});
it('allows a formatter to change arguments', () => {
const reporter = sinon.spy();
const formatter: Formatter = (args, methodName, logLevel) => [
`[${logLevel.toUpperCase()}]`,
...args,
`(reporter.${methodName})`
];
const logger = createLogger();
logger.setReporter(reporter);
logger.setFormatter(formatter);
logger.setLevel(LogLevel.INFO);
logger.log('a', 'b', 'c');
assert.strictEqual(reporter.callCount, 1);
assert.deepStrictEqual(reporter.lastCall.args, [
['[INFO]', 'a', 'b', 'c', '(reporter.log)'],
'log',
'info'
]);
});
it('throws when setting unknown log level', () => {
const logger = createLogger();
logger.setLevel(LogLevel.INFO);
assert.throws(() => (logger.setLevel as any)('xxx'));
});
describe('default reporter', () => {
let warnSpy: sinon.SinonSpy;
before(() => {
warnSpy = sinon.spy(console, 'warn');
});
it('uses global console', () => {
const logger = createLogger();
logger.warn('a', 'b', 'c');
assert.deepStrictEqual(warnSpy.lastCall.args, ['a', 'b', 'c']);
});
it('is exported', () => {
const logger = createLogger();
logger.setReporter(defaultReporter);
logger.warn('a', 'b', 'c');
assert.deepStrictEqual(warnSpy.lastCall.args, ['a', 'b', 'c']);
});
after(() => {
warnSpy.restore();
});
});
it('exports the default formatter', () => {
const args = [1, 2, 3];
assert.deepStrictEqual(defaultFormatter(args, 'log', LogLevel.INFO), args);
});