]> git.r.bdr.sh - rbdr/cologne/blob - test/formatters/simple.js
Give longer flush timeout
[rbdr/cologne] / test / formatters / simple.js
1 'use strict';
2
3 const Tap = require('tap');
4
5 const SimpleFormatter = require('../../lib/formatters/simple');
6
7 // Prepare the test
8
9 Tap.plan(12);
10
11 const logObject = {
12 _timestamp: BigInt(Date.now()) * 1000000n,
13 _cologneLog: '1.0.0',
14 _from: 'Dummy Logger',
15 _level: 3,
16 _levelString: 'error',
17 message: 'testing stuff!'
18 };
19 const isoDate = (new Date(Number(logObject._timestamp) / 1000000)).toISOString();
20
21 const plainFormatter = new SimpleFormatter();
22 const colorFormatter = new SimpleFormatter({
23 colorize: true
24 });
25
26 /**
27 * TEST: #format() - plain
28 */
29
30 const plainFormattedString = plainFormatter.format(logObject);
31
32 Tap.equal(typeof plainFormattedString, 'string',
33 '#format() should output a string in plain mode');
34
35 Tap.ok(plainFormattedString.match(logObject._from),
36 '#format() should include the from property in plain mode');
37
38 Tap.ok(plainFormattedString.match(isoDate),
39 '#format() should include the timestamp property in iso format in plain mode');
40
41 Tap.ok(plainFormattedString.match(logObject._levelString),
42 '#format() should include the level string property in plain mode');
43
44 Tap.ok(plainFormattedString.match(logObject.message),
45 '#format() should include the message property in plain mode');
46
47 /**
48 * TEST: #format() - colorized
49 */
50
51 const colorFormattedString = colorFormatter.format(logObject);
52
53 Tap.equal(typeof colorFormattedString, 'string',
54 '#format() should output a string in color mode');
55
56 Tap.ok(colorFormattedString.match(logObject._from),
57 '#format() should include the from property in color mode');
58
59 Tap.ok(colorFormattedString.match(isoDate),
60 '#format() should include the timestamp property in iso format in color mode');
61
62 Tap.ok(colorFormattedString.match(logObject._levelString),
63 '#format() should include the level string property in color mode');
64
65 Tap.ok(colorFormattedString.match(logObject.message),
66 '#format() should include the message property in color mode');
67
68 Tap.equal(colorFormattedString.split(String.fromCharCode(27) + '[31m').length, 2,
69 '#format() should colorize the string');
70
71 Tap.equal(colorFormattedString.split(String.fromCharCode(27) + '[0m').length, 2,
72 '#format() should colorize only a bit of the string');