UNPKG

7.01 kBJavaScriptView Raw
1/*
2 * console-test.js: Tests for instances of the Console transport
3 *
4 * (C) 2010 Charlie Robbins
5 * MIT LICENSE
6 *
7 */
8
9var path = require('path'),
10 vows = require('vows'),
11 assert = require('assert'),
12 winston = require('../../lib/winston'),
13 helpers = require('../helpers'),
14 stdMocks = require('std-mocks');
15
16var npmTransport = new (winston.transports.Console)(),
17 syslogTransport = new (winston.transports.Console)({ levels: winston.config.syslog.levels }),
18 alignTransport = new (winston.transports.Console)({ showLevel: true, align: true }),
19 defaultTransport = new (winston.transports.Console)(),
20 rawTransport = new (winston.transports.Console)({ level: 'verbose', raw: true }),
21 debugStdoutTransport = new (winston.transports.Console)({ debugStdout: true }),
22 stderrLevelsTransport = new (winston.transports.Console)({ stderrLevels: ['info', 'warn'] }),
23 customLevels = {
24 alpha: 0,
25 beta: 1,
26 gamma: 2,
27 delta: 3,
28 epsilon: 4,
29 },
30 customLevelsAndStderrTransport = new (winston.transports.Console)({
31 levels: customLevels,
32 stderrLevels: ['delta', 'epsilon']
33 }),
34 noStderrTransport = new (winston.transports.Console)({ stderrLevels: [] });
35
36vows.describe('winston/transports/console').addBatch({
37 "An instance of the Console Transport": {
38 "with showLevel off": {
39 topic : function() {
40 npmTransport.showLevel = false;
41 stdMocks.use();
42 npmTransport.log('info', 'Le message', { meta: true }, this.callback);
43 },
44 "should not have level prepended": function () {
45 stdMocks.restore();
46 var output = stdMocks.flush(),
47 line = output.stdout[0];
48
49 assert.equal(line, 'Le message meta=true\n');
50 }
51 }
52 }
53}).addBatch({
54 "An instance of the Console Transport": {
55 "with showLevel on": {
56 topic : function() {
57 npmTransport.showLevel = true;
58 stdMocks.use();
59 npmTransport.log('info', '');
60 },
61 "should have level prepended": function () {
62 stdMocks.restore();
63 var output = stdMocks.flush(),
64 line = output.stdout[0];
65
66 assert.equal(line, 'info: \n');
67 }
68 },
69 }
70}).addBatch({
71 "An instance of the Console Transport": {
72 "with npm levels": {
73 "should have the proper methods defined": function () {
74 helpers.assertConsole(npmTransport);
75 },
76 "the log() method": helpers.testNpmLevels(npmTransport, "should respond with true", function (ign, err, logged) {
77 assert.isNull(err);
78 assert.isTrue(logged);
79 })
80 },
81 "with syslog levels": {
82 "should have the proper methods defined": function () {
83 helpers.assertConsole(syslogTransport);
84 },
85 "the log() method": helpers.testSyslogLevels(syslogTransport, "should respond with true", function (ign, err, logged) {
86 assert.isNull(err);
87 assert.isTrue(logged);
88 })
89 },
90 "with end-of-line": {
91 topic : function() {
92 npmTransport.eol = 'X';
93 stdMocks.use();
94 npmTransport.log('info', 'Le message', { meta: true }, this.callback);
95 },
96 "should have end-of-line character appended": function () {
97 stdMocks.restore();
98 var output = stdMocks.flush(),
99 line = output.stdout[0];
100 console.dir(line);
101
102 assert.equal(line, 'info: Le message meta=trueX');
103 }
104 }
105 }
106}).addBatch({
107 "An instance of the Console Transport with the align option on": {
108 topic : function() {
109 stdMocks.use();
110 alignTransport.log('info', '');
111 },
112 "should have logs aligned": function () {
113 stdMocks.restore();
114 var output = stdMocks.flush(),
115 line = output.stdout[0];
116
117 assert.equal(line, 'info\011: \n');
118 }
119 }
120}).addBatch({
121 "with align off": {
122 topic : function() {
123 alignTransport.align = false;
124 stdMocks.use();
125 alignTransport.log('info', '');
126 },
127 "should not have logs aligned": function () {
128 stdMocks.restore();
129 var output = stdMocks.flush(),
130 line = output.stdout[0];
131
132 assert.equal(line, 'info: \n');
133 }
134 }
135}).addBatch({
136 'An instance of a raw Console transport': {
137 'logging to stdout': {
138 topic: function () {
139 stdMocks.use();
140 rawTransport.log('verbose', 'hello there');
141 }, 'should output json with message property': function () {
142 stdMocks.restore();
143 var output = stdMocks.flush();
144 assert.ok(output.stdout[0].indexOf('"message":"hello there"') > -1);
145 }
146 }
147 }
148}).addBatch({
149 "An instance of the Console Transport with no options": {
150 "should set stderrLevels to 'error' and 'debug' by default": helpers.assertStderrLevels(
151 defaultTransport,
152 ['error', 'debug']
153 ),
154 "should log only 'error' and 'debug' to stderr": helpers.testLoggingToStreams(
155 winston.config.npm.levels, defaultTransport, ['debug', 'error'], stdMocks
156 )
157 }
158}).addBatch({
159 "An instance of the Console Transport with debugStdout set": {
160 "should throw an Error if stderrLevels is set": helpers.assertOptionsThrow(
161 { debugStdout: true, stderrLevels: ['debug'] },
162 "Error: Cannot set debugStdout and stderrLevels together"
163 ),
164 "should set stderrLevels to 'error' by default": helpers.assertStderrLevels(
165 debugStdoutTransport,
166 ['error']
167 ),
168 "should log only the 'error' level to stderr": helpers.testLoggingToStreams(
169 winston.config.npm.levels, debugStdoutTransport, ['error'], stdMocks
170 )
171 }
172}).addBatch({
173 "An instance of the Console Transport with stderrLevels set": {
174 "should throw an Error if stderrLevels is set but not an Array": helpers.assertOptionsThrow(
175 { debugStdout: false, stderrLevels: new String('Not an Array') },
176 "Error: Cannot set stderrLevels to type other than Array"
177 ),
178 "should throw an Error if stderrLevels contains non-string elements": helpers.assertOptionsThrow(
179 { debugStdout: false, stderrLevels: ["good", /^invalid$/, "valid"] },
180 "Error: Cannot have non-string elements in stderrLevels Array"
181 ),
182 "should correctly set stderrLevels": helpers.assertStderrLevels(
183 stderrLevelsTransport,
184 ['info', 'warn']
185 ),
186 "should log only the levels in stderrLevels to stderr": helpers.testLoggingToStreams(
187 winston.config.npm.levels, stderrLevelsTransport, ['info', 'warn'], stdMocks
188 )
189 }
190}).addBatch({
191 "An instance of the Console Transport with stderrLevels set to an empty array": {
192 "should log only to stdout, and not to stderr": helpers.testLoggingToStreams(
193 winston.config.npm.levels, noStderrTransport, [], stdMocks
194 )
195 }
196}).addBatch({
197 "An instance of the Console Transport with custom levels and stderrLevels set": {
198 "should log only the levels in stderrLevels to stderr": helpers.testLoggingToStreams(
199 customLevels, customLevelsAndStderrTransport, ['delta', 'epsilon'], stdMocks
200 )
201 }
202}).export(module);