This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LogService.test.ts
145 lines (108 loc) · 4.38 KB
/
LogService.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
// Copyright (c) 2023. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { MockLogger } from "./logger/mock/MockLogger";
import { LogLevel } from "./types/LogLevel";
import { LogService } from "./LogService";
import { Logger } from "./types/Logger";
import { ContextLogger } from "./logger/context/ContextLogger";
describe('LogService', () => {
let mockLogger: Logger;
let prevLogger : Logger;
let prevLogLevel: LogLevel;
let spyDebug : any;
let spyInfo : any;
let spyWarn : any;
let spyError : any;
beforeEach(() => {
prevLogger = LogService.getLogger();
prevLogLevel = LogService.getLogLevel();
mockLogger = new MockLogger();
spyDebug = jest.spyOn(mockLogger, 'debug').mockImplementation(() => {});
spyInfo = jest.spyOn(mockLogger, 'info').mockImplementation(() => {});
spyWarn = jest.spyOn(mockLogger, 'warn').mockImplementation(() => {});
spyError = jest.spyOn(mockLogger, 'error').mockImplementation(() => {});
LogService.setLogger(mockLogger);
LogService.setLogLevel(LogLevel.DEBUG);
});
afterEach( () => {
spyDebug.mockRestore();
spyInfo.mockRestore();
spyWarn.mockRestore();
spyError.mockRestore();
LogService.setLogLevel(prevLogLevel);
LogService.setLogger(prevLogger)
});
describe('#setLogLevel', () => {
it('sets the log level to the specified value', () => {
LogService.setLogLevel(LogLevel.INFO);
expect(LogService.getLogLevel()).toEqual(LogLevel.INFO);
});
});
describe('#getLogLevel', () => {
it('returns the current log level', () => {
expect(LogService.getLogLevel()).toEqual(LogLevel.DEBUG);
});
});
describe('#getLogLevelString', () => {
it('returns the string representation of the current log level', () => {
expect(LogService.getLogLevelString()).toEqual('DEBUG');
});
});
describe('#setLogger', () => {
it('sets the logger to the specified value', () => {
const newLogger = new MockLogger();
LogService.setLogger(newLogger);
expect(LogService['_logger']).toEqual(newLogger);
});
it('throws an error when the logger is not defined', () => {
expect(() => LogService.setLogger(undefined as any)).toThrow(TypeError);
});
});
describe('#debug', () => {
it('logs messages with log level DEBUG', () => {
LogService.debug('test message');
expect(mockLogger.debug).toHaveBeenCalledWith('test message');
});
it('does not log messages with log level INFO', () => {
LogService.setLogLevel(LogLevel.INFO);
LogService.debug('test message');
expect(mockLogger.debug).not.toHaveBeenCalled();
});
});
describe('#info', () => {
it('logs messages with log level INFO', () => {
LogService.setLogLevel(LogLevel.INFO);
LogService.info('test message');
expect(mockLogger.info).toHaveBeenCalledWith('test message');
});
it('does not log messages with log level WARN', () => {
LogService.setLogLevel(LogLevel.WARN);
LogService.info('test message');
expect(mockLogger.info).not.toHaveBeenCalled();
});
});
describe('#warn', () => {
it('logs messages with log level WARN', () => {
LogService.setLogLevel(LogLevel.WARN);
LogService.warn('test message');
expect(mockLogger.warn).toHaveBeenCalledWith('test message');
});
it('does not log messages with log level ERROR', () => {
LogService.setLogLevel(LogLevel.ERROR);
LogService.warn('test message');
expect(mockLogger.warn).not.toHaveBeenCalled();
});
});
describe('#error', () => {
it('logs messages with log level ERROR', () => {
LogService.setLogLevel(LogLevel.ERROR);
LogService.error('test message');
expect(mockLogger.error).toHaveBeenCalledWith('test message');
});
});
describe('#createLogger', () => {
it('returns a new ContextLogger instance', () => {
const contextLogger = LogService.createLogger('test');
expect(contextLogger).toBeInstanceOf(ContextLogger);
});
});
});