/
logger.test.js
85 lines (61 loc) · 2.37 KB
/
logger.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
import Logger from '../src/logger';
describe('Logger', () => {
jest.spyOn(console, 'log');
afterEach(() => {
jest.clearAllMocks();
Logger.instance = null;
});
test('can skip when verbose is false', () => {
const testLogger = new Logger('syft.js', false),
message = 'hello';
expect(testLogger.verbose).toBe(false);
expect(console.log.mock.calls.length).toBe(0);
testLogger.log(message);
expect(console.log.mock.calls.length).toBe(0);
});
test('singleton instance should be used with verbose false', () => {
const testLogger = new Logger('syft.js', false);
const testLogger_1 = new Logger('syft.js', true);
expect(testLogger).toEqual(testLogger_1);
expect(testLogger.verbose).toBe(false);
expect(testLogger_1.verbose).toBe(false);
expect(console.log.mock.calls.length).toBe(0);
testLogger.log('hello singleton!!');
testLogger_1.log('hello singleton!!');
expect(console.log.mock.calls.length).toBe(0);
});
test('singleton instance should be used with verbose true', () => {
const testLogger = new Logger('syft.js', true);
const testLogger_1 = new Logger('syft.js', false);
expect(testLogger).toEqual(testLogger_1);
expect(testLogger.verbose).toBe(true);
expect(testLogger_1.verbose).toBe(true);
expect(console.log.mock.calls.length).toBe(0);
testLogger.log('hello singleton!!');
testLogger_1.log('hello singleton!!');
expect(console.log.mock.calls.length).toBe(2);
});
test('can log under verbose mode', () => {
const testLogger = new Logger('syft.js', true),
message = 'hello';
expect(testLogger.verbose).toBe(true);
expect(console.log.mock.calls.length).toBe(0);
const currentTime = Date.now();
testLogger.log(message);
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toBe(
`${currentTime}: syft.js - ${message}`
);
});
test('can log with data', () => {
const testLogger = new Logger('syft.js', true),
message = 'hello',
myObj = { awesome: true };
expect(testLogger.verbose).toBe(true);
expect(console.log.mock.calls.length).toBe(0);
testLogger.log(message, myObj);
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toContain(`: syft.js - ${message}`);
expect(console.log.mock.calls[0][1]).toStrictEqual(myObj);
});
});