console.test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 'use strict';
  2. /*
  3. * console-test.js: Tests for instances of the Console transport
  4. *
  5. * (C) 2010 Charlie Robbins
  6. * MIT LICENSE
  7. *
  8. */
  9. const path = require('path');
  10. const assume = require('assume');
  11. const { LEVEL, MESSAGE } = require('triple-beam');
  12. const winston = require('../../lib/winston');
  13. const helpers = require('../helpers');
  14. const stdMocks = require('std-mocks');
  15. const defaultLevels = winston.config.npm.levels;
  16. const transports = {
  17. defaults: new winston.transports.Console(),
  18. noStderr: new winston.transports.Console({ stderrLevels: [] }),
  19. stderrLevels: new winston.transports.Console({
  20. stderrLevels: ['info', 'error']
  21. }),
  22. consoleWarnLevels: new winston.transports.Console({
  23. consoleWarnLevels: ['warn', 'debug']
  24. }),
  25. eol: new winston.transports.Console({ eol: 'X' }),
  26. syslog: new winston.transports.Console({
  27. levels: winston.config.syslog.levels
  28. }),
  29. customLevelStderr: new winston.transports.Console({
  30. levels: {
  31. alpha: 0,
  32. beta: 1,
  33. gamma: 2,
  34. delta: 3,
  35. epsilon: 4,
  36. },
  37. stderrLevels: ['delta', 'epsilon']
  38. })
  39. };
  40. /**
  41. * Returns a function that asserts the `transport` has the specified
  42. * logLevels values in the appropriate logLevelsName member.
  43. *
  44. * @param {TransportStream} transport Transport to assert against
  45. * @param {Array} logLevels Set of levels assumed to exist for the specified map
  46. * @param {String} logLevelsName The name of the array/map that holdes the log leveles values (ie: 'stderrLevels', 'consoleWarnLevels')
  47. * @return {function} Assertion function to execute comparison
  48. */
  49. function assertLogLevelsValues(transport, logLevels, logLevelsName = 'stderrLevels') {
  50. return function () {
  51. assume(JSON.stringify(Object.keys(transport[logLevelsName]).sort()))
  52. .equals(JSON.stringify(logLevels.sort()));
  53. };
  54. }
  55. describe('Console transport', function () {
  56. describe('with defaults', function () {
  57. it('logs all levels to stdout', function () {
  58. stdMocks.use();
  59. transports.defaults.levels = defaultLevels;
  60. Object.keys(defaultLevels)
  61. .forEach(function (level) {
  62. const info = {
  63. [LEVEL]: level,
  64. message: `This is level ${level}`,
  65. level
  66. };
  67. info[MESSAGE] = JSON.stringify(info);
  68. transports.defaults.log(info);
  69. });
  70. stdMocks.restore();
  71. var output = stdMocks.flush();
  72. assume(output.stderr).is.an('array');
  73. assume(output.stderr).length(0);
  74. assume(output.stdout).is.an('array');
  75. assume(output.stdout).length(7);
  76. });
  77. it("should set stderrLevels to [] by default", assertLogLevelsValues(
  78. transports.defaults,
  79. [],
  80. 'stderrLevels'
  81. ));
  82. });
  83. describe('throws an appropriate error when', function () {
  84. it("if stderrLevels is set, but not an Array { stderrLevels: 'Not an Array' }", function () {
  85. assume(function () {
  86. let throwing = new winston.transports.Console({
  87. stderrLevels: 'Not an Array'
  88. })
  89. }).throws(/Cannot make set from type other than Array of string elements/);
  90. });
  91. it("if stderrLevels contains non-string elements { stderrLevels: ['good', /^invalid$/, 'valid']", function () {
  92. assume(function () {
  93. let throwing = new winston.transports.Console({
  94. stderrLevels: ['good', /^invalid$/, 'valid']
  95. })
  96. }).throws(/Cannot make set from type other than Array of string elements/);
  97. });
  98. });
  99. it("{ stderrLevels: ['info', 'error'] } logs to them appropriately", assertLogLevelsValues(
  100. transports.stderrLevels,
  101. ['info', 'error'],
  102. 'stderrLevels'
  103. ));
  104. it("{ consoleWarnLevels: ['warn', 'debug'] } logs to them appropriately", assertLogLevelsValues(
  105. transports.consoleWarnLevels,
  106. ['warn', 'debug'],
  107. 'consoleWarnLevels'
  108. ));
  109. it('{ eol } adds a custom EOL delimiter', function (done) {
  110. stdMocks.use();
  111. transports.eol.log({ [MESSAGE]: 'info: testing. 1 2 3...' }, function () {
  112. stdMocks.restore();
  113. var output = stdMocks.flush(),
  114. line = output.stdout[0];
  115. assume(line).equal('info: testing. 1 2 3...X');
  116. done();
  117. });
  118. });
  119. });
  120. require('abstract-winston-transport')({
  121. name: 'Console',
  122. Transport: winston.transports.Console
  123. });
  124. // vows.describe('winston/transports/console').addBatch({
  125. // "An instance of the Console Transport": {
  126. // "with syslog levels": {
  127. // "should have the proper methods defined": function () {
  128. // helpers.assertConsole(syslogTransport);
  129. // },
  130. // "the log() method": helpers.testSyslogLevels(syslogTransport, "should respond with true", function (ign, err, logged) {
  131. // assert.isNull(err);
  132. // assert.isTrue(logged);
  133. // })
  134. // }
  135. // }
  136. // }).addBatch({
  137. // "An instance of the Console Transport with no options": {
  138. // "should log only 'error' and 'debug' to stderr": helpers.testLoggingToStreams(
  139. // winston.config.npm.levels, defaultTransport, ['debug', 'error'], stdMocks
  140. // )
  141. // }
  142. // }).addBatch({
  143. // "An instance of the Console Transport with debugStdout set": {
  144. // "should set stderrLevels to 'error' by default": helpers.assertStderrLevels(
  145. // debugStdoutTransport,
  146. // ['error']
  147. // ),
  148. // "should log only the 'error' level to stderr": helpers.testLoggingToStreams(
  149. // winston.config.npm.levels, debugStdoutTransport, ['error'], stdMocks
  150. // )
  151. // }
  152. // }).addBatch({
  153. // "An instance of the Console Transport with stderrLevels set": {
  154. // "should log only the levels in stderrLevels to stderr": helpers.testLoggingToStreams(
  155. // winston.config.npm.levels, stderrLevelsTransport, ['info', 'warn'], stdMocks
  156. // )
  157. // }
  158. // }).addBatch({
  159. // "An instance of the Console Transport with stderrLevels set to an empty array": {
  160. // "should log only to stdout, and not to stderr": helpers.testLoggingToStreams(
  161. // winston.config.npm.levels, noStderrTransport, [], stdMocks
  162. // )
  163. // }
  164. // }).addBatch({
  165. // "An instance of the Console Transport with custom levels and stderrLevels set": {
  166. // "should log only the levels in stderrLevels to stderr": helpers.testLoggingToStreams(
  167. // customLevels, customLevelsAndStderrTransport, ['delta', 'epsilon'], stdMocks
  168. // )
  169. // }
  170. // }).export(module);