-
Notifications
You must be signed in to change notification settings - Fork 268
/
log_history.test.ts
88 lines (77 loc) · 2.07 KB
/
log_history.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
import { jest } from '@jest/globals';
import { createDebugOnlyLogger, enableLogs } from './debug.js';
import { LogHistory } from './log_history.js';
jest.useFakeTimers({ doNotFake: ['performance'] });
describe('log history', () => {
let debug: (msg: string) => void;
let logHistory: LogHistory;
const timestamp = new Date().toISOString();
const name = 'test:a';
beforeEach(() => {
debug = createDebugOnlyLogger(name);
enableLogs(name);
logHistory = new LogHistory();
});
it('keeps debug logs', () => {
logHistory.enable();
expect(logHistory.getLogs()).toEqual([]);
debug('0');
debug('1');
debug('2');
expect(logHistory.getLogs()).toEqual([
[timestamp, name, '0'],
[timestamp, name, '1'],
[timestamp, name, '2'],
]);
});
it('does not keep logs if not enabled', () => {
debug('0');
debug('1');
expect(logHistory.getLogs()).toEqual([]);
});
it('returns last n logs', () => {
logHistory.enable();
expect(logHistory.getLogs()).toEqual([]);
debug('0');
debug('1');
debug('2');
debug('3');
debug('4');
expect(logHistory.getLogs(2)).toEqual([
[timestamp, name, '3'],
[timestamp, name, '4'],
]);
});
it('only keeps logs with enabled namespace', () => {
logHistory.enable();
const name2 = 'test:b';
const debug2 = createDebugOnlyLogger(name2);
debug('0');
debug2('zero');
expect(logHistory.getLogs()).toEqual([[timestamp, name, '0']]);
enableLogs(`${name},${name2}`);
debug('1');
debug2('one');
expect(logHistory.getLogs()).toEqual([
[timestamp, name, '0'],
[timestamp, name, '1'],
[timestamp, name2, 'one'],
]);
});
it('clears all logs', () => {
logHistory.enable();
debug('0');
debug('1');
debug('2');
logHistory.clear();
expect(logHistory.getLogs()).toEqual([]);
});
it('clears first n logs', () => {
logHistory.enable();
debug('0');
debug('1');
debug('2');
logHistory.clear(2);
expect(logHistory.getLogs()).toEqual([[timestamp, name, '2']]);
});
});