]>
Commit | Line | Data |
---|---|---|
b3847a2e RBR |
1 | 'use strict'; |
2 | ||
3 | const Tap = require('tap'); | |
4 | ||
5 | const ConsoleLogger = require('../../lib/loggers/console'); | |
6 | ||
7 | // Prepare the test | |
8 | ||
9 | const dummyFormatter = { | |
10 | values: [], | |
11 | format: function (log) { | |
12 | ||
13 | this.values.push(log); | |
14 | return 'lol'; | |
15 | } | |
16 | }; | |
17 | ||
18 | const dummyConsole = { | |
19 | values: { | |
20 | log: 0, | |
21 | warn: 0, | |
22 | error: 0, | |
23 | info: 0 | |
24 | }, | |
25 | _log: function (type, ...logs) { | |
26 | ||
27 | this.values[type] += logs.length; | |
28 | }, | |
29 | log: function (...logs) { | |
30 | ||
31 | this._log('log', ...logs); | |
32 | }, | |
33 | warn: function (...logs) { | |
34 | ||
35 | this._log('warn', ...logs); | |
36 | }, | |
37 | error: function (...logs) { | |
38 | ||
39 | this._log('error', ...logs); | |
40 | }, | |
41 | info: function (...logs) { | |
42 | ||
43 | this._log('info', ...logs); | |
44 | } | |
45 | }; | |
46 | ||
47 | const logObjectA = { | |
48 | _timestamp: Date.now() + .134, | |
49 | _cologneLog: '1.0.0', | |
50 | _from: 'Dummy Logger', | |
51 | _level: 6, | |
52 | _levelString: 'info', | |
53 | message: 'MessageOne' | |
54 | }; | |
55 | ||
56 | const logObjectB = { | |
57 | _timestamp: Date.now() + .134, | |
58 | _cologneLog: '1.0.0', | |
59 | _from: 'Dummy Logger', | |
60 | _level: 6, | |
61 | _levelString: 'info', | |
62 | message: 'MessageTwo' | |
63 | }; | |
64 | ||
a326e75a | 65 | const exampleLogs = [logObjectA, logObjectB]; |
b3847a2e RBR |
66 | |
67 | const regularLogger = new ConsoleLogger({}); | |
68 | const overrideLogger = new ConsoleLogger({ | |
69 | console: dummyConsole | |
70 | }); | |
71 | const formattedLogger = new ConsoleLogger({ | |
72 | console: dummyConsole, | |
73 | formatter: dummyFormatter | |
74 | }); | |
75 | ||
76 | /** | |
77 | * TEST: #log() - regular | |
78 | */ | |
79 | ||
80 | Tap.equal(regularLogger.console, global.console, | |
81 | 'It should default to the global console'); | |
82 | ||
83 | /** | |
84 | * TEST: #log() - override | |
85 | */ | |
86 | ||
87 | logObjectA._level = 5; | |
88 | logObjectB._level = 6; | |
a326e75a | 89 | overrideLogger.log(...exampleLogs); // should go to info |
b3847a2e RBR |
90 | |
91 | logObjectA._level = 4; | |
92 | logObjectB._level = 4; | |
a326e75a | 93 | overrideLogger.log(...exampleLogs); // should go to warn |
b3847a2e RBR |
94 | |
95 | logObjectA._level = 1; | |
96 | logObjectB._level = 3; | |
a326e75a | 97 | overrideLogger.log(...exampleLogs); // should go to error |
b3847a2e RBR |
98 | |
99 | logObjectA._level = 0; | |
100 | logObjectB._level = 2; | |
a326e75a | 101 | overrideLogger.log(...exampleLogs); // should go to error |
b3847a2e RBR |
102 | |
103 | logObjectA._level = 7; | |
104 | logObjectB._level = 8; | |
a326e75a | 105 | overrideLogger.log(...exampleLogs); // should go to log |
b3847a2e | 106 | |
a326e75a | 107 | Tap.equal(dummyConsole.values.log, exampleLogs.length, |
b3847a2e | 108 | 'It should send debug messages to console\'s #log'); |
a326e75a | 109 | Tap.equal(dummyConsole.values.info, exampleLogs.length, |
b3847a2e | 110 | 'It should send info and notice messages to console\'s #info'); |
a326e75a | 111 | Tap.equal(dummyConsole.values.warn, exampleLogs.length, |
b3847a2e | 112 | 'It should send warn messages to console\'s #warn'); |
a326e75a | 113 | Tap.equal(dummyConsole.values.error, exampleLogs.length * 2, |
b3847a2e RBR |
114 | 'It should send error messages to console\'s #error'); |
115 | ||
116 | /** | |
117 | * TEST: #log() - with formatter | |
118 | */ | |
119 | ||
a326e75a | 120 | formattedLogger.log(...exampleLogs); // should go to log |
b3847a2e | 121 | |
a326e75a | 122 | Tap.similar(dummyFormatter.values, exampleLogs, |
b3847a2e | 123 | 'If available, it should send the objects to the formatter'); |