debug-test.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. var vows = require('vows')
  3. , assert = require('assert')
  4. , sandbox = require('sandboxed-module')
  5. , fakeConsole = {
  6. error: function(format, label, message) {
  7. this.logged = [ format, label, message ];
  8. }
  9. }
  10. , globals = function(debugValue) {
  11. return {
  12. process: {
  13. env: {
  14. 'NODE_DEBUG': debugValue
  15. }
  16. },
  17. console: fakeConsole
  18. };
  19. };
  20. vows.describe('../lib/debug').addBatch({
  21. 'when NODE_DEBUG is set to log4js': {
  22. topic: function() {
  23. var debug = sandbox.require(
  24. '../lib/debug',
  25. { 'globals': globals('log4js') }
  26. );
  27. fakeConsole.logged = [];
  28. debug('cheese')('biscuits');
  29. return fakeConsole.logged;
  30. },
  31. 'it should log to console.error': function(logged) {
  32. assert.equal(logged[0], 'LOG4JS: (%s) %s');
  33. assert.equal(logged[1], 'cheese');
  34. assert.equal(logged[2], 'biscuits');
  35. }
  36. },
  37. 'when NODE_DEBUG is set to not log4js': {
  38. topic: function() {
  39. var debug = sandbox.require(
  40. '../lib/debug',
  41. { globals: globals('other_module') }
  42. );
  43. fakeConsole.logged = [];
  44. debug('cheese')('biscuits');
  45. return fakeConsole.logged;
  46. },
  47. 'it should not log to console.error': function(logged) {
  48. assert.equal(logged.length, 0);
  49. }
  50. },
  51. 'when NODE_DEBUG is not set': {
  52. topic: function() {
  53. var debug = sandbox.require(
  54. '../lib/debug',
  55. { globals: globals(null) }
  56. );
  57. fakeConsole.logged = [];
  58. debug('cheese')('biscuits');
  59. return fakeConsole.logged;
  60. },
  61. 'it should not log to console.error': function(logged) {
  62. assert.equal(logged.length, 0);
  63. }
  64. }
  65. }).exportTo(module);