UNPKG

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