progress.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. /**
  3. * @module Progress
  4. */
  5. /**
  6. * Module dependencies.
  7. */
  8. var Base = require('./base');
  9. var constants = require('../runner').constants;
  10. var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
  11. var EVENT_TEST_END = constants.EVENT_TEST_END;
  12. var EVENT_RUN_END = constants.EVENT_RUN_END;
  13. var inherits = require('../utils').inherits;
  14. var color = Base.color;
  15. var cursor = Base.cursor;
  16. /**
  17. * Expose `Progress`.
  18. */
  19. exports = module.exports = Progress;
  20. /**
  21. * General progress bar color.
  22. */
  23. Base.colors.progress = 90;
  24. /**
  25. * Initialize a new `Progress` bar test reporter.
  26. *
  27. * @public
  28. * @class
  29. * @memberof Mocha.reporters
  30. * @extends Mocha.reporters.Base
  31. * @param {Runner} runner
  32. * @param {Object} options
  33. */
  34. function Progress(runner, options) {
  35. Base.call(this, runner);
  36. var self = this;
  37. var width = (Base.window.width * 0.5) | 0;
  38. var total = runner.total;
  39. var complete = 0;
  40. var lastN = -1;
  41. // default chars
  42. options = options || {};
  43. var reporterOptions = options.reporterOptions || {};
  44. options.open = reporterOptions.open || '[';
  45. options.complete = reporterOptions.complete || '▬';
  46. options.incomplete = reporterOptions.incomplete || Base.symbols.dot;
  47. options.close = reporterOptions.close || ']';
  48. options.verbose = reporterOptions.verbose || false;
  49. // tests started
  50. runner.on(EVENT_RUN_BEGIN, function() {
  51. console.log();
  52. cursor.hide();
  53. });
  54. // tests complete
  55. runner.on(EVENT_TEST_END, function() {
  56. complete++;
  57. var percent = complete / total;
  58. var n = (width * percent) | 0;
  59. var i = width - n;
  60. if (n === lastN && !options.verbose) {
  61. // Don't re-render the line if it hasn't changed
  62. return;
  63. }
  64. lastN = n;
  65. cursor.CR();
  66. process.stdout.write('\u001b[J');
  67. process.stdout.write(color('progress', ' ' + options.open));
  68. process.stdout.write(Array(n).join(options.complete));
  69. process.stdout.write(Array(i).join(options.incomplete));
  70. process.stdout.write(color('progress', options.close));
  71. if (options.verbose) {
  72. process.stdout.write(color('progress', ' ' + complete + ' of ' + total));
  73. }
  74. });
  75. // tests are complete, output some stats
  76. // and the failures if any
  77. runner.once(EVENT_RUN_END, function() {
  78. cursor.show();
  79. console.log();
  80. self.epilogue();
  81. });
  82. }
  83. /**
  84. * Inherit from `Base.prototype`.
  85. */
  86. inherits(Progress, Base);
  87. Progress.description = 'a progress bar';