progress.js 2.1 KB

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