analogger 1.4.0 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/unit.cjs DELETED
@@ -1,455 +0,0 @@
1
- const chai = require("chai");
2
- const assertArrays = require("chai-arrays")
3
- const expect = chai.expect;
4
- const sinon = require("sinon");
5
-
6
- let alert, sandbox;
7
-
8
- // Arrange
9
- const myStub = {
10
- myMethod: () => { }
11
- }
12
-
13
-
14
- const spies = require('chai-spies');
15
-
16
- chai.use(spies);
17
- chai.use(assertArrays)
18
-
19
- // sut
20
- const {anaLogger} = require("../src/cjs/ana-logger.cjs");
21
- const {LOG_CONTEXTS, LOG_TARGETS} = require("../example/cjs/contexts-def.cjs");
22
-
23
- describe('AnaLogger', function ()
24
- {
25
- before(() =>
26
- {
27
- anaLogger.setContexts(LOG_CONTEXTS);
28
- anaLogger.setTargets(LOG_TARGETS);
29
- anaLogger.setActiveTarget(LOG_TARGETS.DEV3)
30
- anaLogger.removeOverride({error: true})
31
- })
32
-
33
- beforeEach(() =>
34
- {
35
- alert = sinon.spy()
36
- chai.spy.on(myStub, 'myMethod');
37
-
38
- anaLogger.resetLogHistory()
39
- anaLogger.keepLogHistory()
40
- anaLogger.resetLogFormatter();
41
- })
42
-
43
- afterEach(()=>
44
- {
45
- chai.spy.restore(myStub.myMethod)
46
- })
47
-
48
- describe('#isContextValid()', function ()
49
- {
50
- it('should be true when a valid context object is passed', function ()
51
- {
52
- // Arrange
53
- const context = LOG_CONTEXTS.TEST
54
- // Act
55
- const result = anaLogger.isContextValid(context)
56
- // Assert
57
- expect(result).to.be.true
58
- });
59
-
60
- it('should be false when an invalid context object is passed', function ()
61
- {
62
- // Arrange
63
- const context = {}
64
- // Act
65
- const result = anaLogger.isContextValid(context)
66
- // Assert
67
- expect(result).to.be.false
68
- });
69
-
70
- it('should be false when a null context is passed', function ()
71
- {
72
- // Act
73
- const result = anaLogger.isContextValid(null)
74
- // Assert
75
- expect(result).to.be.false
76
- });
77
- });
78
-
79
- describe('#setOptions()', function ()
80
- {
81
- it('should have an option to silent the log', function ()
82
- {
83
- anaLogger.setOptions({silent: true})
84
- const options = anaLogger.getOptions()
85
- expect(options.silent).to.be.true
86
- });
87
- });
88
-
89
- describe('#log()', function ()
90
- {
91
- it('should emulate console.log', function ()
92
- {
93
- // Arrange
94
- anaLogger.setOptions({silent: false, hideError: false})
95
-
96
- // Act
97
- anaLogger.log(`Test Log example C1`);
98
- const output = anaLogger.getLogHistory()
99
-
100
- // Assert
101
- expect(output).to.contain(`Test Log example C1`)
102
- });
103
-
104
- it('should understand values passed with context', function ()
105
- {
106
- // Act
107
- anaLogger.log(LOG_CONTEXTS.C1, `Test Log example C1`);
108
-
109
- // Assert
110
- expect(anaLogger.getLogHistory()).to.contain(`Test Log example C1`)
111
- });
112
-
113
- it('should understand values passed with context as value of object', function ()
114
- {
115
- // Act
116
- anaLogger.log({context: LOG_CONTEXTS.C1}, `Test Log example C1`);
117
-
118
- // Assert
119
- expect(anaLogger.getLogHistory()).to.contain(`Test Log example C1`)
120
- });
121
-
122
- it('should understand values passed with context defined as null', function ()
123
- {
124
- // Act
125
- anaLogger.log({context: null}, `Test Log example C1`);
126
-
127
- // Assert
128
- expect(anaLogger.getLogHistory()).to.contain(`Test Log example C1`)
129
- });
130
-
131
- it('should populate history even though the hidelog option is on', function ()
132
- {
133
- anaLogger.setOptions({hideLog: true})
134
-
135
- // Act
136
- anaLogger.log({context: LOG_CONTEXTS.C1, lid: 123456789233}, `The hidden log`);
137
-
138
- // Assert
139
- expect(anaLogger.getLogHistory()).to.contain(`The hidden log`)
140
- });
141
-
142
- it('should not capture or display log from another defined target', function ()
143
- {
144
- anaLogger.setActiveTarget(LOG_TARGETS.DEV3)
145
- anaLogger.log({target: LOG_TARGETS.DEV1}, `I am for DEV1`);
146
-
147
- // Assert
148
- expect(anaLogger.getLogHistory()).to.not.contain(`I am for DEV1`)
149
- });
150
-
151
- it('should capture logs when no active target is set', function ()
152
- {
153
- anaLogger.setActiveTarget(null)
154
- anaLogger.log({target: LOG_TARGETS.DEV3}, `I am for DEV3`);
155
-
156
- // Assert
157
- expect(anaLogger.getLogHistory()).to.contain(`I am for DEV3`)
158
- });
159
-
160
- it('should capture logs from the same target', function ()
161
- {
162
- anaLogger.setActiveTarget(LOG_TARGETS.DEV3)
163
- anaLogger.log({target: LOG_TARGETS.DEV3}, `I am for DEV3`);
164
-
165
- // Assert
166
- expect(anaLogger.getLogHistory()).to.contain(`I am for DEV3`)
167
- });
168
-
169
- it('should capture logs from the same target', function ()
170
- {
171
- anaLogger.log(LOG_CONTEXTS.TEST2, `I am for DEV3`);
172
-
173
- // Assert
174
- expect(anaLogger.getLogHistory()).to.contain(`I am for DEV3`)
175
- });
176
-
177
- it('should truncate some text when too long', function ()
178
- {
179
- // Act
180
- anaLogger.log({
181
- context: LOG_CONTEXTS.C1,
182
- lid : 123456789233
183
- }, `The super long Log ID (lid) will be truncated`);
184
-
185
- // Assert
186
- expect(anaLogger.getLogHistory()).to.contain(`C1: (12... )`)
187
- });
188
-
189
-
190
- });
191
-
192
- describe('#error()', function ()
193
- {
194
- it('should not show up when hideError mode is on', function ()
195
- {
196
- // Arrange
197
- anaLogger.setOptions({hideError: true})
198
-
199
- // Act
200
- anaLogger.error(`Test Log example C1`);
201
-
202
- // Assert
203
- expect(anaLogger.getLogHistory()).to.not.contain(`Test Log example C1`)
204
- });
205
-
206
- it('should not show up when hideError mode is off', function ()
207
- {
208
- // Arrange
209
- anaLogger.setOptions({hideError: false})
210
-
211
- // Act
212
- anaLogger.error(`Test Log example C1`);
213
-
214
- // Assert
215
- expect(anaLogger.getLogHistory()).to.contain(`Test Log example C1`)
216
- });
217
- });
218
-
219
- describe('#info()', function ()
220
- {
221
- it('should display some log', function ()
222
- {
223
- anaLogger.info(`Hello from info`)
224
- expect(anaLogger.getLogHistory()).to.contain(`Hello from info`)
225
- });
226
- });
227
-
228
- describe('#warn()', function ()
229
- {
230
- it('should display some warn', function ()
231
- {
232
- anaLogger.warn(`Hello from warn`)
233
- expect(anaLogger.getLogHistory()).to.contain(`Hello from warn`)
234
- });
235
- });
236
-
237
- describe('#alert()', function ()
238
- {
239
- it('should not fail on alert', function ()
240
- {
241
- anaLogger.alert(`Hello from alert`, {aaa: 1012})
242
- expect(anaLogger.getLogHistory()).to.contain(`Hello from alert`)
243
- });
244
-
245
- describe("in a non-Node environment", function ()
246
- {
247
- beforeEach(function ()
248
- {
249
- sandbox = sinon.createSandbox();
250
- });
251
-
252
-
253
- it('should be called', function ()
254
- {
255
- sandbox
256
- .stub(anaLogger, "isNode")
257
- .withArgs("Hello from alert")
258
- .returns(
259
- true
260
- );
261
-
262
- chai.expect(() => anaLogger.alert(`Hello from alert`)).to.throw(`alert is not defined`);
263
- });
264
-
265
- afterEach(function ()
266
- {
267
- sandbox.restore();
268
- });
269
- })
270
-
271
- });
272
-
273
- describe('#assert()', function ()
274
- {
275
- it('should evaluate condition expressions', function ()
276
- {
277
- const result = anaLogger.assert(1 === 1)
278
- expect(result).to.be.true
279
- });
280
-
281
- it('should detect failing condition expressions', function ()
282
- {
283
- const result = anaLogger.assert(1 === 2)
284
- expect(result).to.be.false
285
- });
286
-
287
- it('should evaluate function expressions', function ()
288
- {
289
- const result = anaLogger.assert(() => true, true)
290
- expect(result).to.be.true
291
- });
292
-
293
- it('should fail when function expressions fail', function ()
294
- {
295
- const result = anaLogger.assert(() => false, true)
296
- expect(result).to.be.false
297
- });
298
-
299
- it('should evaluate more complex function expressions', function ()
300
- {
301
- const result = anaLogger.assert((a, b) => a === b, true, 2, 2)
302
- expect(result).to.be.true
303
- });
304
-
305
- it('should not break the code when an invalid function is passed', function ()
306
- {
307
- expect(anaLogger.assert(() => nonExistentFunctionThatIsCalledAnyway(), true, 2, 2)
308
- ).to.be.false
309
- });
310
- });
311
-
312
- describe('#overrideConsole()', function ()
313
- {
314
- it('should override the console behaviour', function ()
315
- {
316
- anaLogger.overrideConsole({error: true})
317
- console.log(`Test 1`)
318
- expect(anaLogger.getLogHistory()).to.contain(`Test 1`)
319
- });
320
- });
321
-
322
- describe('#setLogFormat()', function ()
323
- {
324
- /**
325
- * We use a spy here, but things would have been straightforward with a simple "done" + async on the "it"
326
- */
327
- it('should replace the default formatter function with the given callback when invoking console.log', function ()
328
- {
329
- beforeEach(function ()
330
- {
331
- })
332
-
333
- afterEach(function ()
334
- {
335
- chai.spy.restore(myStub.myMethod)
336
- })
337
-
338
- anaLogger.setLogFormat(
339
- myStub.myMethod
340
- );
341
-
342
- console.log(LOG_CONTEXTS.C1, `Test Log example C4 with new format`);
343
- expect(myStub.myMethod).to.have.been.called;
344
- });
345
-
346
- it("should reset the formatter to its first value", () =>
347
- {
348
- anaLogger.setLogFormat(
349
- () => "If you see this the test has failed"
350
- );
351
- anaLogger.resetLogFormatter();
352
- anaLogger.log(LOG_CONTEXTS.C1, `Test Log example C4 with new format`);
353
- expect(anaLogger.getLogHistory()).to.contain(`C1: ( )`)
354
- })
355
-
356
- it("should reject invalid formatters", () =>
357
- {
358
- const res = anaLogger.setLogFormat(null);
359
- expect(res).to.be.false
360
- })
361
- });
362
-
363
- describe('#releaseLogHistory()', function ()
364
- {
365
- it('should not keep log history', function ()
366
- {
367
- anaLogger.releaseLogHistory()
368
- anaLogger.log(`Hello you`)
369
- expect(anaLogger.getLogHistory().length).to.equal(0)
370
- });
371
- });
372
-
373
- describe('#getLogHistory()', function ()
374
- {
375
- it('should return history as a string', function ()
376
- {
377
- anaLogger.log(`Hello you`)
378
- const arr = anaLogger.getLogHistory()
379
- expect(arr).to.be.string
380
- });
381
-
382
- it('should return history as an array', function ()
383
- {
384
- anaLogger.log(`Hello you`)
385
- const arr = anaLogger.getLogHistory(false)
386
- expect(arr).to.be.array()
387
- });
388
-
389
- it('should return history as an array', function ()
390
- {
391
- anaLogger.log(`Hello you`)
392
- const arr = anaLogger.getLogHistory(false)
393
- expect(arr).to.be.array()
394
- });
395
- });
396
-
397
- describe('#setErrorHandler()', function ()
398
- {
399
- it('should replace the error manager', function ()
400
- {
401
- anaLogger.setActiveTarget(LOG_TARGETS.USER)
402
- anaLogger.error({target: LOG_TARGETS.USER}, `Test Error Log`);
403
- });
404
-
405
- it('should replace the error manager', function ()
406
- {
407
- anaLogger.setErrorHandler(
408
- myStub.myMethod
409
- );
410
-
411
- console.error(`Test Error Log`);
412
- expect(myStub.myMethod).to.have.been.called;
413
- });
414
-
415
- it('should replace the error manager targeting the user', function ()
416
- {
417
- anaLogger.setActiveTarget(LOG_TARGETS.USER)
418
- anaLogger.setErrorHandler(
419
- myStub.myMethod
420
- );
421
-
422
- anaLogger.error(`Test Error Log`);
423
- expect(myStub.myMethod).to.have.been.called;
424
- });
425
-
426
- });
427
-
428
- describe('#setErrorHandlerForUserTarget()', function ()
429
- {
430
- it('should replace the error manager targetting the user', function ()
431
- {
432
- anaLogger.setActiveTarget(LOG_TARGETS.USER)
433
- anaLogger.setErrorHandlerForUserTarget(
434
- myStub.myMethod
435
- );
436
-
437
- anaLogger.error(`Test Error Log`);
438
- expect(myStub.myMethod).to.have.been.called;
439
- });
440
-
441
- it('should replace the error manager targeting the user', function ()
442
- {
443
- anaLogger.setActiveTarget(LOG_TARGETS.USER)
444
- anaLogger.setErrorHandlerForUserTarget(
445
- myStub.myMethod
446
- );
447
-
448
- anaLogger.error(`Test Error Log`);
449
- expect(myStub.myMethod).to.have.been.called;
450
- });
451
-
452
- });
453
-
454
-
455
- });