-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathlogger-factory.test.js
More file actions
259 lines (215 loc) · 8.73 KB
/
logger-factory.test.js
File metadata and controls
259 lines (215 loc) · 8.73 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import createLogger, {PWAKitLogger} from './logger-factory'
describe('PWAKitLogger', () => {
const levels = ['error', 'warn', 'info', 'debug']
const _log = console.log
_log(
'--- NOTE: in this file, `console.log` is mocked. So use `_log` if you need access to the original method.'
)
beforeEach(() => {
console.debug = jest.fn()
console.log = jest.fn()
console.info = jest.fn()
console.warn = jest.fn()
console.error = jest.fn()
jest.resetModules()
})
afterEach(() => {
jest.clearAllMocks()
})
for (const level of levels) {
test(`should log a ${level} message`, () => {
const logger = createLogger({packageName: 'test-package'})
logger[level](`This is a ${level} message`)
expect(console[level]).toHaveBeenCalledWith(
`test-package ${level.toUpperCase()} This is a ${level} message`
)
})
}
test('should use empty packageName if not provided', () => {
const logger = new PWAKitLogger()
logger.info('This is an info message with default packageName')
expect(console.info).toHaveBeenCalledWith(
' INFO This is an info message with default packageName'
)
})
test('should include additional properties in log message', () => {
const logger = createLogger({packageName: 'test-package'})
logger.info('This is an info message', {
namespace: 'testNamespace',
additionalProperties: {key: 'value'}
})
expect(console.info).toHaveBeenCalledWith(
'test-package.testNamespace INFO This is an info message {"key":"value"}'
)
})
test('should not include additionalProperties if it is not provided', () => {
const logger = createLogger({packageName: 'test-package'})
logger.info('This is an info message', {
namespace: 'testNamespace'
})
expect(console.info).toHaveBeenCalledWith(
'test-package.testNamespace INFO This is an info message'
)
})
test('should log only namespace with an empty packageName', () => {
const logger = new PWAKitLogger({packageName: ''})
logger.info('This is an info message', {
namespace: 'testNamespace'
})
expect(console.info).toHaveBeenCalledWith('testNamespace INFO This is an info message')
})
test('should handle log method (alias for info)', () => {
const logger = createLogger({packageName: 'test-package'})
logger.log('This is a log message')
expect(console.info).toHaveBeenCalledWith('test-package INFO This is a log message')
})
describe('serializeError method', () => {
let logger
beforeEach(() => {
logger = createLogger({packageName: 'test-package'})
})
test('should serialize Error objects with name, message, and stack', () => {
const error = new Error('Test error message')
error.stack = 'Error: Test error message\n at test.js:1:1'
logger.info('Error occurred', {
additionalProperties: {error}
})
const expectedErrorObj = {
name: 'Error',
message: 'Test error message',
stack: 'Error: Test error message\n at test.js:1:1'
}
expect(console.info).toHaveBeenCalledWith(
`test-package INFO Error occurred {"error":${JSON.stringify(expectedErrorObj)}}`
)
})
test('should serialize multiple Error objects in the same object', () => {
const error1 = new Error('First error')
error1.stack = 'Error: First error\n at test1.js:1:1'
const error2 = new Error('Second error')
error2.stack = 'Error: Second error\n at test2.js:1:1'
logger.info('Multiple errors occurred', {
additionalProperties: {
primaryError: error1,
secondaryError: error2,
message: 'Both errors occurred'
}
})
const expectedError1 = {
name: 'Error',
message: 'First error',
stack: 'Error: First error\n at test1.js:1:1'
}
const expectedError2 = {
name: 'Error',
message: 'Second error',
stack: 'Error: Second error\n at test2.js:1:1'
}
const expectedObject = {
primaryError: expectedError1,
secondaryError: expectedError2,
message: 'Both errors occurred'
}
expect(console.info).toHaveBeenCalledWith(
`test-package INFO Multiple errors occurred ${JSON.stringify(expectedObject)}`
)
})
test('should handle custom Error types', () => {
class CustomError extends Error {
constructor(message, code) {
super(message)
this.name = 'CustomError'
this.code = code
}
}
const customError = new CustomError('Custom error message', 'ERR_CUSTOM')
customError.stack = 'CustomError: Custom error message\n at custom.js:1:1'
logger.info('Custom error test', {
additionalProperties: {error: customError}
})
const expectedError = {
name: 'CustomError',
message: 'Custom error message',
stack: 'CustomError: Custom error message\n at custom.js:1:1'
}
expect(console.info).toHaveBeenCalledWith(
`test-package INFO Custom error test {"error":${JSON.stringify(expectedError)}}`
)
})
test('should leave non-Error properties unchanged', () => {
const error = new Error('Test error')
error.stack = 'Error: Test error\n at test.js:1:1'
const plainObject = {
message: 'Something went wrong',
error: error,
count: 5,
isValid: true,
data: {nested: 'object'},
items: ['array', 'values']
}
logger.info('Object with error and other properties', {
additionalProperties: plainObject
})
const expectedErrorObj = {
name: 'Error',
message: 'Test error',
stack: 'Error: Test error\n at test.js:1:1'
}
const expectedObject = {
message: 'Something went wrong',
error: expectedErrorObj,
count: 5,
isValid: true,
data: {nested: 'object'},
items: ['array', 'values']
}
expect(console.info).toHaveBeenCalledWith(
`test-package INFO Object with error and other properties ${JSON.stringify(
expectedObject
)}`
)
})
test('should handle objects with no Error properties', () => {
const plainObject = {
message: 'No errors here',
count: 42,
isValid: true,
data: {nested: 'object'}
}
logger.info('Plain object test', {
additionalProperties: plainObject
})
expect(console.info).toHaveBeenCalledWith(
`test-package INFO Plain object test ${JSON.stringify(plainObject)}`
)
})
test('should handle empty objects', () => {
logger.info('Empty object test', {
additionalProperties: {}
})
expect(console.info).toHaveBeenCalledWith('test-package INFO Empty object test {}')
})
test('should handle Error objects without stack property', () => {
const error = new Error('Error without stack')
delete error.stack
logger.info('Error without stack', {
additionalProperties: {error}
})
const expectedError = {
name: 'Error',
message: 'Error without stack',
stack: undefined
}
expect(console.info).toHaveBeenCalledWith(
`test-package INFO Error without stack {"error":${JSON.stringify(expectedError)}}`
)
})
})
describe('logger with TEXT format', () => {})
})