]> git.r.bdr.sh - rbdr/cologne/blob - test/loggers/file.js
Address sonarqube issues with tests
[rbdr/cologne] / test / loggers / file.js
1 'use strict';
2
3 const Fs = require('fs');
4
5 const Tap = require('tap');
6
7 const FileLogger = require('../../lib/loggers/file');
8
9 // Prepare the test
10
11 const rawFile = './raw.log';
12 const formatterFile = './formatter.log';
13 const formatterString = 'example';
14
15 const logObjectA = {
16 _timestamp: Date.now() + .134,
17 _cologneLog: '1.0.0',
18 _from: 'Dummy Logger',
19 _level: 6,
20 _levelString: 'info',
21 message: 'MessageOne'
22 };
23
24 const logObjectB = {
25 _timestamp: Date.now() + .134,
26 _cologneLog: '1.0.0',
27 _from: 'Dummy Logger',
28 _level: 6,
29 _levelString: 'info',
30 message: 'MessageTwo'
31 };
32
33 const logs = [logObjectA, logObjectB];
34
35
36 const dummyFormatter = {
37 values: [],
38 format: function (logObject) {
39
40 this.values.push(logObject);
41 return formatterString;
42 }
43 };
44
45 const rawLogger = new FileLogger({
46 file: rawFile
47 });
48 const formatterLogger = new FileLogger({
49 file: formatterFile,
50 formatter: dummyFormatter
51 });
52
53 /**
54 * TEST: #log() - regular
55 */
56
57 rawLogger.log(...logs);
58
59 setTimeout(() => {
60
61 Tap.test('raw file', (t) => {
62
63 Fs.readFile(rawFile, { encoding: 'utf8' }, (_, contents) => {
64
65 const lines = contents.trim().split('\n');
66
67 Tap.equal(lines.length, logs.length,
68 'it should send all params to the file');
69
70 Tap.equal(JSON.stringify(logObjectA), lines[0],
71 'it should log the raw json object');
72
73 Fs.unlink(rawFile, () => {
74
75 t.end();
76 });
77 });
78 });
79 }, 10); // allow for flush
80 /**
81 * TEST: #log() - formatter
82 */
83
84 formatterLogger.log(...logs);
85
86 setTimeout(() => {
87
88 Tap.test('formatted file', (t) => {
89
90 Fs.readFile(formatterFile, { encoding: 'utf8' }, (_, contents) => {
91
92 const lines = contents.trim().split('\n');
93
94 Tap.equal(lines.length, logs.length,
95 'it should send all params to the file');
96
97 Tap.equal(formatterString, lines[0],
98 'it should log the formatted object');
99
100 Fs.unlink(formatterFile, () => {
101
102 t.end();
103 });
104 });
105 });
106 }, 10);