mocha 3.5.3 → 5.0.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
+ }
@@ -5,7 +5,6 @@
5
5
  */
6
6
 
7
7
  var Base = require('./base');
8
- var JSON = require('json3');
9
8
 
10
9
  /**
11
10
  * Expose `List`.
@@ -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 () {
@@ -43,14 +43,29 @@ function XUnit (runner, options) {
43
43
  var tests = [];
44
44
  var self = this;
45
45
 
46
- if (options && options.reporterOptions && options.reporterOptions.output) {
47
- if (!fs.createWriteStream) {
48
- throw new Error('file output not supported in browser');
46
+ // the name of the test suite, as it will appear in the resulting XML file
47
+ var suiteName;
48
+
49
+ // the default name of the test suite if none is provided
50
+ var DEFAULT_SUITE_NAME = 'Mocha Tests';
51
+
52
+ if (options && options.reporterOptions) {
53
+ if (options.reporterOptions.output) {
54
+ if (!fs.createWriteStream) {
55
+ throw new Error('file output not supported in browser');
56
+ }
57
+
58
+ mkdirp.sync(path.dirname(options.reporterOptions.output));
59
+ self.fileStream = fs.createWriteStream(options.reporterOptions.output);
49
60
  }
50
- mkdirp.sync(path.dirname(options.reporterOptions.output));
51
- self.fileStream = fs.createWriteStream(options.reporterOptions.output);
61
+
62
+ // get the suite name from the reporter options (if provided)
63
+ suiteName = options.reporterOptions.suiteName;
52
64
  }
53
65
 
66
+ // fall back to the default suite name
67
+ suiteName = suiteName || DEFAULT_SUITE_NAME;
68
+
54
69
  runner.on('pending', function (test) {
55
70
  tests.push(test);
56
71
  });
@@ -65,7 +80,7 @@ function XUnit (runner, options) {
65
80
 
66
81
  runner.on('end', function () {
67
82
  self.write(tag('testsuite', {
68
- name: 'Mocha Tests',
83
+ name: suiteName,
69
84
  tests: stats.tests,
70
85
  failures: stats.failures,
71
86
  errors: stats.failures,
package/lib/runnable.js CHANGED
@@ -5,12 +5,10 @@
5
5
  */
6
6
 
7
7
  var EventEmitter = require('events').EventEmitter;
8
- var JSON = require('json3');
9
8
  var Pending = require('./pending');
10
9
  var debug = require('debug')('mocha:runnable');
11
10
  var milliseconds = require('./ms');
12
11
  var utils = require('./utils');
13
- var create = require('lodash.create');
14
12
 
15
13
  /**
16
14
  * Save timer references to avoid Sinon interfering (see GH-237).
@@ -64,9 +62,7 @@ function Runnable (title, fn) {
64
62
  /**
65
63
  * Inherit from `EventEmitter.prototype`.
66
64
  */
67
- Runnable.prototype = create(EventEmitter.prototype, {
68
- constructor: Runnable
69
- });
65
+ utils.inherits(Runnable, EventEmitter);
70
66
 
71
67
  /**
72
68
  * Set & get timeout `ms`.
@@ -95,14 +91,14 @@ Runnable.prototype.timeout = function (ms) {
95
91
  };
96
92
 
97
93
  /**
98
- * Set & get slow `ms`.
94
+ * Set or get slow `ms`.
99
95
  *
100
96
  * @api private
101
97
  * @param {number|string} ms
102
98
  * @return {Runnable|number} ms or Runnable instance.
103
99
  */
104
100
  Runnable.prototype.slow = function (ms) {
105
- if (typeof ms === 'undefined') {
101
+ if (!arguments.length || typeof ms === 'undefined') {
106
102
  return this._slow;
107
103
  }
108
104
  if (typeof ms === 'string') {
@@ -148,7 +144,7 @@ Runnable.prototype.isPending = function () {
148
144
  };
149
145
 
150
146
  /**
151
- * Set number of retries.
147
+ * Set or get number of retries.
152
148
  *
153
149
  * @api private
154
150
  */
@@ -160,7 +156,7 @@ Runnable.prototype.retries = function (n) {
160
156
  };
161
157
 
162
158
  /**
163
- * Get current retry
159
+ * Set or get current retry
164
160
  *
165
161
  * @api private
166
162
  */
@@ -179,7 +175,17 @@ Runnable.prototype.currentRetry = function (n) {
179
175
  * @return {string}
180
176
  */
181
177
  Runnable.prototype.fullTitle = function () {
182
- return this.parent.fullTitle() + ' ' + this.title;
178
+ return this.titlePath().join(' ');
179
+ };
180
+
181
+ /**
182
+ * Return the title path generated by concatenating the parent's title path with the title.
183
+ *
184
+ * @api public
185
+ * @return {string}
186
+ */
187
+ Runnable.prototype.titlePath = function () {
188
+ return this.parent.titlePath().concat([this.title]);
183
189
  };
184
190
 
185
191
  /**
@@ -236,7 +242,7 @@ Runnable.prototype.resetTimeout = function () {
236
242
  };
237
243
 
238
244
  /**
239
- * Whitelist a list of globals for this test run.
245
+ * Set or get a list of whitelisted globals for this test run.
240
246
  *
241
247
  * @api private
242
248
  * @param {string[]} globals