mocha 4.0.1 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,498 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Module dependencies.
5
+ */
6
+
7
+ var tty = require('tty');
8
+ var diff = require('diff');
9
+ var ms = require('../ms');
10
+ var utils = require('../utils');
11
+ var supportsColor = process.browser ? null : require('supports-color');
12
+
13
+ /**
14
+ * Expose `Base`.
15
+ */
16
+
17
+ exports = module.exports = Base;
18
+
19
+ /**
20
+ * Save timer references to avoid Sinon interfering.
21
+ * See: https://github.com/mochajs/mocha/issues/237
22
+ */
23
+
24
+ /* eslint-disable no-unused-vars, no-native-reassign */
25
+ var Date = global.Date;
26
+ var setTimeout = global.setTimeout;
27
+ var setInterval = global.setInterval;
28
+ var clearTimeout = global.clearTimeout;
29
+ var clearInterval = global.clearInterval;
30
+ /* eslint-enable no-unused-vars, no-native-reassign */
31
+
32
+ /**
33
+ * Check if both stdio streams are associated with a tty.
34
+ */
35
+
36
+ var isatty = tty.isatty(1) && tty.isatty(2);
37
+
38
+ /**
39
+ * Enable coloring by default, except in the browser interface.
40
+ */
41
+
42
+ exports.useColors = !process.browser && (supportsColor || (process.env.MOCHA_COLORS !== undefined));
43
+
44
+ /**
45
+ * Inline diffs instead of +/-
46
+ */
47
+
48
+ exports.inlineDiffs = false;
49
+
50
+ /**
51
+ * Default color map.
52
+ */
53
+
54
+ exports.colors = {
55
+ pass: 90,
56
+ fail: 31,
57
+ 'bright pass': 92,
58
+ 'bright fail': 91,
59
+ 'bright yellow': 93,
60
+ pending: 36,
61
+ suite: 0,
62
+ 'error title': 0,
63
+ 'error message': 31,
64
+ 'error stack': 90,
65
+ checkmark: 32,
66
+ fast: 90,
67
+ medium: 33,
68
+ slow: 31,
69
+ green: 32,
70
+ light: 90,
71
+ 'diff gutter': 90,
72
+ 'diff added': 32,
73
+ 'diff removed': 31
74
+ };
75
+
76
+ /**
77
+ * Default symbol map.
78
+ */
79
+
80
+ exports.symbols = {
81
+ ok: '✓',
82
+ err: '✖',
83
+ dot: '․',
84
+ comma: ',',
85
+ bang: '!'
86
+ };
87
+
88
+ // With node.js on Windows: use symbols available in terminal default fonts
89
+ if (process.platform === 'win32') {
90
+ exports.symbols.ok = '\u221A';
91
+ exports.symbols.err = '\u00D7';
92
+ exports.symbols.dot = '.';
93
+ }
94
+
95
+ /**
96
+ * Color `str` with the given `type`,
97
+ * allowing colors to be disabled,
98
+ * as well as user-defined color
99
+ * schemes.
100
+ *
101
+ * @param {string} type
102
+ * @param {string} str
103
+ * @return {string}
104
+ * @api private
105
+ */
106
+ var color = exports.color = function (type, str) {
107
+ if (!exports.useColors) {
108
+ return String(str);
109
+ }
110
+ return '\u001b[' + exports.colors[type] + 'm' + str + '\u001b[0m';
111
+ };
112
+
113
+ /**
114
+ * Expose term window size, with some defaults for when stderr is not a tty.
115
+ */
116
+
117
+ exports.window = {
118
+ width: 75
119
+ };
120
+
121
+ if (isatty) {
122
+ exports.window.width = process.stdout.getWindowSize
123
+ ? process.stdout.getWindowSize(1)[0]
124
+ : tty.getWindowSize()[1];
125
+ }
126
+
127
+ /**
128
+ * Expose some basic cursor interactions that are common among reporters.
129
+ */
130
+
131
+ exports.cursor = {
132
+ hide: function () {
133
+ isatty && process.stdout.write('\u001b[?25l');
134
+ },
135
+
136
+ show: function () {
137
+ isatty && process.stdout.write('\u001b[?25h');
138
+ },
139
+
140
+ deleteLine: function () {
141
+ isatty && process.stdout.write('\u001b[2K');
142
+ },
143
+
144
+ beginningOfLine: function () {
145
+ isatty && process.stdout.write('\u001b[0G');
146
+ },
147
+
148
+ CR: function () {
149
+ if (isatty) {
150
+ exports.cursor.deleteLine();
151
+ exports.cursor.beginningOfLine();
152
+ } else {
153
+ process.stdout.write('\r');
154
+ }
155
+ }
156
+ };
157
+
158
+ function showDiff (err) {
159
+ return err && err.showDiff !== false && sameType(err.actual, err.expected) && err.expected !== undefined;
160
+ }
161
+
162
+ function stringifyDiffObjs (err) {
163
+ if (!utils.isString(err.actual) || !utils.isString(err.expected)) {
164
+ err.actual = utils.stringify(err.actual);
165
+ err.expected = utils.stringify(err.expected);
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Output the given `failures` as a list.
171
+ *
172
+ * @param {Array} failures
173
+ * @api public
174
+ */
175
+
176
+ exports.list = function (failures) {
177
+ console.log();
178
+ failures.forEach(function (test, i) {
179
+ // format
180
+ var fmt = color('error title', ' %s) %s:\n') +
181
+ color('error message', ' %s') +
182
+ color('error stack', '\n%s\n');
183
+
184
+ // msg
185
+ var msg;
186
+ var err = test.err;
187
+ var message;
188
+ if (err.message && typeof err.message.toString === 'function') {
189
+ message = err.message + '';
190
+ } else if (typeof err.inspect === 'function') {
191
+ message = err.inspect() + '';
192
+ } else {
193
+ message = '';
194
+ }
195
+ var stack = err.stack || message;
196
+ var index = message ? stack.indexOf(message) : -1;
197
+
198
+ if (index === -1) {
199
+ msg = message;
200
+ } else {
201
+ index += message.length;
202
+ msg = stack.slice(0, index);
203
+ // remove msg from stack
204
+ stack = stack.slice(index + 1);
205
+ }
206
+
207
+ // uncaught
208
+ if (err.uncaught) {
209
+ msg = 'Uncaught ' + msg;
210
+ }
211
+
212
+ // explicitly show diff
213
+ <<<<<<< HEAD
214
+ if (showDiff(err)) {
215
+ stringifyDiffObjs(err);
216
+ =======
217
+ if (exports.hideDiff !== true && err.showDiff !== false && sameType(actual, expected) && expected !== undefined) {
218
+ escape = false;
219
+ if (!(utils.isString(actual) && utils.isString(expected))) {
220
+ err.actual = actual = utils.stringify(actual);
221
+ err.expected = expected = utils.stringify(expected);
222
+ }
223
+
224
+ >>>>>>> Add --no-diff option (fixes mochajs/mocha#2514)
225
+ fmt = color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
226
+ var match = message.match(/^([^:]+): expected/);
227
+ msg = '\n ' + color('error message', match ? match[1] : msg);
228
+
229
+ if (exports.inlineDiffs) {
230
+ msg += inlineDiff(err);
231
+ } else {
232
+ msg += unifiedDiff(err);
233
+ }
234
+ }
235
+
236
+ // indent stack trace
237
+ stack = stack.replace(/^/gm, ' ');
238
+
239
+ // indented test title
240
+ var testTitle = '';
241
+ test.titlePath().forEach(function (str, index) {
242
+ if (index !== 0) {
243
+ testTitle += '\n ';
244
+ }
245
+ for (var i = 0; i < index; i++) {
246
+ testTitle += ' ';
247
+ }
248
+ testTitle += str;
249
+ });
250
+
251
+ console.log(fmt, (i + 1), testTitle, msg, stack);
252
+ });
253
+ };
254
+
255
+ /**
256
+ * Initialize a new `Base` reporter.
257
+ *
258
+ * All other reporters generally
259
+ * inherit from this reporter, providing
260
+ * stats such as test duration, number
261
+ * of tests passed / failed etc.
262
+ *
263
+ * @param {Runner} runner
264
+ * @api public
265
+ */
266
+
267
+ function Base (runner) {
268
+ var stats = this.stats = { suites: 0, tests: 0, passes: 0, pending: 0, failures: 0 };
269
+ var failures = this.failures = [];
270
+
271
+ if (!runner) {
272
+ return;
273
+ }
274
+ this.runner = runner;
275
+
276
+ runner.stats = stats;
277
+
278
+ runner.on('start', function () {
279
+ stats.start = new Date();
280
+ });
281
+
282
+ runner.on('suite', function (suite) {
283
+ stats.suites = stats.suites || 0;
284
+ suite.root || stats.suites++;
285
+ });
286
+
287
+ runner.on('test end', function () {
288
+ stats.tests = stats.tests || 0;
289
+ stats.tests++;
290
+ });
291
+
292
+ runner.on('pass', function (test) {
293
+ stats.passes = stats.passes || 0;
294
+
295
+ if (test.duration > test.slow()) {
296
+ test.speed = 'slow';
297
+ } else if (test.duration > test.slow() / 2) {
298
+ test.speed = 'medium';
299
+ } else {
300
+ test.speed = 'fast';
301
+ }
302
+
303
+ stats.passes++;
304
+ });
305
+
306
+ runner.on('fail', function (test, err) {
307
+ stats.failures = stats.failures || 0;
308
+ stats.failures++;
309
+ if (showDiff(err)) {
310
+ stringifyDiffObjs(err);
311
+ }
312
+ test.err = err;
313
+ failures.push(test);
314
+ });
315
+
316
+ runner.on('end', function () {
317
+ stats.end = new Date();
318
+ stats.duration = new Date() - stats.start;
319
+ });
320
+
321
+ runner.on('pending', function () {
322
+ stats.pending++;
323
+ });
324
+ }
325
+
326
+ /**
327
+ * Output common epilogue used by many of
328
+ * the bundled reporters.
329
+ *
330
+ * @api public
331
+ */
332
+ Base.prototype.epilogue = function () {
333
+ var stats = this.stats;
334
+ var fmt;
335
+
336
+ console.log();
337
+
338
+ // passes
339
+ fmt = color('bright pass', ' ') +
340
+ color('green', ' %d passing') +
341
+ color('light', ' (%s)');
342
+
343
+ console.log(fmt,
344
+ stats.passes || 0,
345
+ ms(stats.duration));
346
+
347
+ // pending
348
+ if (stats.pending) {
349
+ fmt = color('pending', ' ') +
350
+ color('pending', ' %d pending');
351
+
352
+ console.log(fmt, stats.pending);
353
+ }
354
+
355
+ // failures
356
+ if (stats.failures) {
357
+ fmt = color('fail', ' %d failing');
358
+
359
+ console.log(fmt, stats.failures);
360
+
361
+ Base.list(this.failures);
362
+ console.log();
363
+ }
364
+
365
+ console.log();
366
+ };
367
+
368
+ /**
369
+ * Pad the given `str` to `len`.
370
+ *
371
+ * @api private
372
+ * @param {string} str
373
+ * @param {string} len
374
+ * @return {string}
375
+ */
376
+ function pad (str, len) {
377
+ str = String(str);
378
+ return Array(len - str.length + 1).join(' ') + str;
379
+ }
380
+
381
+ /**
382
+ * Returns an inline diff between 2 strings with coloured ANSI output
383
+ *
384
+ * @api private
385
+ * @param {Error} err with actual/expected
386
+ * @return {string} Diff
387
+ */
388
+ function inlineDiff (err) {
389
+ var msg = errorDiff(err);
390
+
391
+ // linenos
392
+ var lines = msg.split('\n');
393
+ if (lines.length > 4) {
394
+ var width = String(lines.length).length;
395
+ msg = lines.map(function (str, i) {
396
+ return pad(++i, width) + ' |' + ' ' + str;
397
+ }).join('\n');
398
+ }
399
+
400
+ // legend
401
+ msg = '\n' +
402
+ color('diff removed', 'actual') +
403
+ ' ' +
404
+ color('diff added', 'expected') +
405
+ '\n\n' +
406
+ msg +
407
+ '\n';
408
+
409
+ // indent
410
+ msg = msg.replace(/^/gm, ' ');
411
+ return msg;
412
+ }
413
+
414
+ /**
415
+ * Returns a unified diff between two strings.
416
+ *
417
+ * @api private
418
+ * @param {Error} err with actual/expected
419
+ * @return {string} The diff.
420
+ */
421
+ function unifiedDiff (err) {
422
+ var indent = ' ';
423
+ function cleanUp (line) {
424
+ if (line[0] === '+') {
425
+ return indent + colorLines('diff added', line);
426
+ }
427
+ if (line[0] === '-') {
428
+ return indent + colorLines('diff removed', line);
429
+ }
430
+ if (line.match(/@@/)) {
431
+ return '--';
432
+ }
433
+ if (line.match(/\\ No newline/)) {
434
+ return null;
435
+ }
436
+ return indent + line;
437
+ }
438
+ function notBlank (line) {
439
+ return typeof line !== 'undefined' && line !== null;
440
+ }
441
+ var msg = diff.createPatch('string', err.actual, err.expected);
442
+ var lines = msg.split('\n').splice(5);
443
+ return '\n ' +
444
+ colorLines('diff added', '+ expected') + ' ' +
445
+ colorLines('diff removed', '- actual') +
446
+ '\n\n' +
447
+ lines.map(cleanUp).filter(notBlank).join('\n');
448
+ }
449
+
450
+ /**
451
+ * Return a character diff for `err`.
452
+ *
453
+ * @api private
454
+ * @param {Error} err
455
+ * @return {string}
456
+ */
457
+ function errorDiff (err) {
458
+ return diff.diffWordsWithSpace(err.actual, err.expected).map(function (str) {
459
+ if (str.added) {
460
+ return colorLines('diff added', str.value);
461
+ }
462
+ if (str.removed) {
463
+ return colorLines('diff removed', str.value);
464
+ }
465
+ return str.value;
466
+ }).join('');
467
+ }
468
+
469
+ /**
470
+ * Color lines for `str`, using the color `name`.
471
+ *
472
+ * @api private
473
+ * @param {string} name
474
+ * @param {string} str
475
+ * @return {string}
476
+ */
477
+ function colorLines (name, str) {
478
+ return str.split('\n').map(function (str) {
479
+ return color(name, str);
480
+ }).join('\n');
481
+ }
482
+
483
+ /**
484
+ * Object#toString reference.
485
+ */
486
+ var objToString = Object.prototype.toString;
487
+
488
+ /**
489
+ * Check that a / b have the same type.
490
+ *
491
+ * @api private
492
+ * @param {Object} a
493
+ * @param {Object} b
494
+ * @return {boolean}
495
+ */
496
+ function sameType (a, b) {
497
+ return objToString.call(a) === objToString.call(b);
498
+ }
@@ -39,11 +39,13 @@ function Progress (runner, options) {
39
39
 
40
40
  // default chars
41
41
  options = options || {};
42
- options.open = options.open || '[';
43
- options.complete = options.complete || '▬';
44
- options.incomplete = options.incomplete || Base.symbols.dot;
45
- options.close = options.close || ']';
46
- options.verbose = false;
42
+ var reporterOptions = options.reporterOptions || {};
43
+
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;
47
49
 
48
50
  // tests started
49
51
  runner.on('start', function () {
package/lib/runnable.js CHANGED
@@ -91,14 +91,14 @@ Runnable.prototype.timeout = function (ms) {
91
91
  };
92
92
 
93
93
  /**
94
- * Set & get slow `ms`.
94
+ * Set or get slow `ms`.
95
95
  *
96
96
  * @api private
97
97
  * @param {number|string} ms
98
98
  * @return {Runnable|number} ms or Runnable instance.
99
99
  */
100
100
  Runnable.prototype.slow = function (ms) {
101
- if (typeof ms === 'undefined') {
101
+ if (!arguments.length || typeof ms === 'undefined') {
102
102
  return this._slow;
103
103
  }
104
104
  if (typeof ms === 'string') {
@@ -144,7 +144,7 @@ Runnable.prototype.isPending = function () {
144
144
  };
145
145
 
146
146
  /**
147
- * Set number of retries.
147
+ * Set or get number of retries.
148
148
  *
149
149
  * @api private
150
150
  */
@@ -156,7 +156,7 @@ Runnable.prototype.retries = function (n) {
156
156
  };
157
157
 
158
158
  /**
159
- * Get current retry
159
+ * Set or get current retry
160
160
  *
161
161
  * @api private
162
162
  */
@@ -242,7 +242,7 @@ Runnable.prototype.resetTimeout = function () {
242
242
  };
243
243
 
244
244
  /**
245
- * Whitelist a list of globals for this test run.
245
+ * Set or get a list of whitelisted globals for this test run.
246
246
  *
247
247
  * @api private
248
248
  * @param {string[]} globals
package/lib/suite.js CHANGED
@@ -92,7 +92,7 @@ Suite.prototype.clone = function () {
92
92
  };
93
93
 
94
94
  /**
95
- * Set timeout `ms` or short-hand such as "2s".
95
+ * Set or get timeout `ms` or short-hand such as "2s".
96
96
  *
97
97
  * @api private
98
98
  * @param {number|string} ms
@@ -114,7 +114,7 @@ Suite.prototype.timeout = function (ms) {
114
114
  };
115
115
 
116
116
  /**
117
- * Set number of times to retry a failed test.
117
+ * Set or get number of times to retry a failed test.
118
118
  *
119
119
  * @api private
120
120
  * @param {number|string} n
@@ -130,7 +130,7 @@ Suite.prototype.retries = function (n) {
130
130
  };
131
131
 
132
132
  /**
133
- * Set timeout to `enabled`.
133
+ * Set or get timeout to `enabled`.
134
134
  *
135
135
  * @api private
136
136
  * @param {boolean} enabled
@@ -146,7 +146,7 @@ Suite.prototype.enableTimeouts = function (enabled) {
146
146
  };
147
147
 
148
148
  /**
149
- * Set slow `ms` or short-hand such as "2s".
149
+ * Set or get slow `ms` or short-hand such as "2s".
150
150
  *
151
151
  * @api private
152
152
  * @param {number|string} ms
@@ -165,7 +165,7 @@ Suite.prototype.slow = function (ms) {
165
165
  };
166
166
 
167
167
  /**
168
- * Sets whether to bail after first error.
168
+ * Set or get whether to bail after first error.
169
169
  *
170
170
  * @api private
171
171
  * @param {boolean} bail
package/lib/utils.js CHANGED
@@ -476,7 +476,6 @@ exports.canonicalize = function canonicalize (value, stack, typeHint) {
476
476
  */
477
477
  exports.lookupFiles = function lookupFiles (path, extensions, recursive) {
478
478
  var files = [];
479
- var re = new RegExp('\\.(' + extensions.join('|') + ')$');
480
479
 
481
480
  if (!exists(path)) {
482
481
  if (exists(path + '.js')) {
@@ -514,6 +513,7 @@ exports.lookupFiles = function lookupFiles (path, extensions, recursive) {
514
513
  // ignore error
515
514
  return;
516
515
  }
516
+ var re = new RegExp('\\.(?:' + extensions.join('|') + ')$');
517
517
  if (!stat.isFile() || !re.test(file) || basename(file)[0] === '.') {
518
518
  return;
519
519
  }