progress.js 2.5 KB

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