mocha 5.1.1 → 5.2.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.
package/lib/utils.js CHANGED
@@ -30,8 +30,8 @@ exports.inherits = require('util').inherits;
30
30
  * @param {string} html
31
31
  * @return {string}
32
32
  */
33
- exports.escape = function (html) {
34
- return he.encode(String(html), { useNamedReferences: false });
33
+ exports.escape = function(html) {
34
+ return he.encode(String(html), {useNamedReferences: false});
35
35
  };
36
36
 
37
37
  /**
@@ -41,7 +41,7 @@ exports.escape = function (html) {
41
41
  * @param {Object} obj
42
42
  * @return {boolean}
43
43
  */
44
- exports.isString = function (obj) {
44
+ exports.isString = function(obj) {
45
45
  return typeof obj === 'string';
46
46
  };
47
47
 
@@ -53,11 +53,11 @@ exports.isString = function (obj) {
53
53
  * @param {Array} files
54
54
  * @param {Function} fn
55
55
  */
56
- exports.watch = function (files, fn) {
57
- var options = { interval: 100 };
58
- files.forEach(function (file) {
56
+ exports.watch = function(files, fn) {
57
+ var options = {interval: 100};
58
+ files.forEach(function(file) {
59
59
  debug('file %s', file);
60
- fs.watchFile(file, options, function (curr, prev) {
60
+ fs.watchFile(file, options, function(curr, prev) {
61
61
  if (prev.mtime < curr.mtime) {
62
62
  fn(file);
63
63
  }
@@ -72,7 +72,7 @@ exports.watch = function (files, fn) {
72
72
  * @param {string} path
73
73
  * @return {boolean}
74
74
  */
75
- function ignored (path) {
75
+ function ignored(path) {
76
76
  return !~ignore.indexOf(path);
77
77
  }
78
78
 
@@ -85,15 +85,16 @@ function ignored (path) {
85
85
  * @param {Array} [ret=[]]
86
86
  * @return {Array}
87
87
  */
88
- exports.files = function (dir, ext, ret) {
88
+ exports.files = function(dir, ext, ret) {
89
89
  ret = ret || [];
90
90
  ext = ext || ['js'];
91
91
 
92
92
  var re = new RegExp('\\.(' + ext.join('|') + ')$');
93
93
 
94
- fs.readdirSync(dir)
94
+ fs
95
+ .readdirSync(dir)
95
96
  .filter(ignored)
96
- .forEach(function (path) {
97
+ .forEach(function(path) {
97
98
  path = join(dir, path);
98
99
  if (fs.lstatSync(path).isDirectory()) {
99
100
  exports.files(path, ext, ret);
@@ -112,7 +113,7 @@ exports.files = function (dir, ext, ret) {
112
113
  * @param {string} str
113
114
  * @return {string}
114
115
  */
115
- exports.slug = function (str) {
116
+ exports.slug = function(str) {
116
117
  return str
117
118
  .toLowerCase()
118
119
  .replace(/ +/g, '-')
@@ -125,15 +126,22 @@ exports.slug = function (str) {
125
126
  * @param {string} str
126
127
  * @return {string}
127
128
  */
128
- exports.clean = function (str) {
129
+ exports.clean = function(str) {
129
130
  str = str
130
- .replace(/\r\n?|[\n\u2028\u2029]/g, '\n').replace(/^\uFEFF/, '')
131
+ .replace(/\r\n?|[\n\u2028\u2029]/g, '\n')
132
+ .replace(/^\uFEFF/, '')
131
133
  // (traditional)-> space/name parameters body (lambda)-> parameters body multi-statement/single keep body content
132
- .replace(/^function(?:\s*|\s+[^(]*)\([^)]*\)\s*\{((?:.|\n)*?)\s*\}$|^\([^)]*\)\s*=>\s*(?:\{((?:.|\n)*?)\s*\}|((?:.|\n)*))$/, '$1$2$3');
134
+ .replace(
135
+ /^function(?:\s*|\s+[^(]*)\([^)]*\)\s*\{((?:.|\n)*?)\s*\}$|^\([^)]*\)\s*=>\s*(?:\{((?:.|\n)*?)\s*\}|((?:.|\n)*))$/,
136
+ '$1$2$3'
137
+ );
133
138
 
134
139
  var spaces = str.match(/^\n?( *)/)[1].length;
135
140
  var tabs = str.match(/^\n?(\t*)/)[1].length;
136
- var re = new RegExp('^\n?' + (tabs ? '\t' : ' ') + '{' + (tabs || spaces) + '}', 'gm');
141
+ var re = new RegExp(
142
+ '^\n?' + (tabs ? '\t' : ' ') + '{' + (tabs || spaces) + '}',
143
+ 'gm'
144
+ );
137
145
 
138
146
  str = str.replace(re, '');
139
147
 
@@ -147,17 +155,20 @@ exports.clean = function (str) {
147
155
  * @param {string} qs
148
156
  * @return {Object}
149
157
  */
150
- exports.parseQuery = function (qs) {
151
- return qs.replace('?', '').split('&').reduce(function (obj, pair) {
152
- var i = pair.indexOf('=');
153
- var key = pair.slice(0, i);
154
- var val = pair.slice(++i);
155
-
156
- // Due to how the URLSearchParams API treats spaces
157
- obj[key] = decodeURIComponent(val.replace(/\+/g, '%20'));
158
-
159
- return obj;
160
- }, {});
158
+ exports.parseQuery = function(qs) {
159
+ return qs
160
+ .replace('?', '')
161
+ .split('&')
162
+ .reduce(function(obj, pair) {
163
+ var i = pair.indexOf('=');
164
+ var key = pair.slice(0, i);
165
+ var val = pair.slice(++i);
166
+
167
+ // Due to how the URLSearchParams API treats spaces
168
+ obj[key] = decodeURIComponent(val.replace(/\+/g, '%20'));
169
+
170
+ return obj;
171
+ }, {});
161
172
  };
162
173
 
163
174
  /**
@@ -167,7 +178,7 @@ exports.parseQuery = function (qs) {
167
178
  * @param {string} js
168
179
  * @return {string}
169
180
  */
170
- function highlight (js) {
181
+ function highlight(js) {
171
182
  return js
172
183
  .replace(/</g, '&lt;')
173
184
  .replace(/>/g, '&gt;')
@@ -175,8 +186,14 @@ function highlight (js) {
175
186
  .replace(/('.*?')/gm, '<span class="string">$1</span>')
176
187
  .replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
177
188
  .replace(/(\d+)/gm, '<span class="number">$1</span>')
178
- .replace(/\bnew[ \t]+(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
179
- .replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>');
189
+ .replace(
190
+ /\bnew[ \t]+(\w+)/gm,
191
+ '<span class="keyword">new</span> <span class="init">$1</span>'
192
+ )
193
+ .replace(
194
+ /\b(function|new|throw|return|var|if|else)\b/gm,
195
+ '<span class="keyword">$1</span>'
196
+ );
180
197
  }
181
198
 
182
199
  /**
@@ -185,7 +202,7 @@ function highlight (js) {
185
202
  * @api private
186
203
  * @param {string} name
187
204
  */
188
- exports.highlightTags = function (name) {
205
+ exports.highlightTags = function(name) {
189
206
  var code = document.getElementById('mocha').getElementsByTagName(name);
190
207
  for (var i = 0, len = code.length; i < len; ++i) {
191
208
  code[i].innerHTML = highlight(code[i].innerHTML);
@@ -206,7 +223,7 @@ exports.highlightTags = function (name) {
206
223
  * @param {string} typeHint The type of the value
207
224
  * @returns {string}
208
225
  */
209
- function emptyRepresentation (value, typeHint) {
226
+ function emptyRepresentation(value, typeHint) {
210
227
  switch (typeHint) {
211
228
  case 'function':
212
229
  return '[Function]';
@@ -240,7 +257,7 @@ function emptyRepresentation (value, typeHint) {
240
257
  * type(global) // 'global'
241
258
  * type(new String('foo') // 'object'
242
259
  */
243
- var type = exports.type = function type (value) {
260
+ var type = (exports.type = function type(value) {
244
261
  if (value === undefined) {
245
262
  return 'undefined';
246
263
  } else if (value === null) {
@@ -248,10 +265,11 @@ var type = exports.type = function type (value) {
248
265
  } else if (Buffer.isBuffer(value)) {
249
266
  return 'buffer';
250
267
  }
251
- return Object.prototype.toString.call(value)
268
+ return Object.prototype.toString
269
+ .call(value)
252
270
  .replace(/^\[.+\s(.+?)]$/, '$1')
253
271
  .toLowerCase();
254
- };
272
+ });
255
273
 
256
274
  /**
257
275
  * Stringify `value`. Different behavior depending on type of value:
@@ -268,21 +286,23 @@ var type = exports.type = function type (value) {
268
286
  * @param {*} value
269
287
  * @return {string}
270
288
  */
271
- exports.stringify = function (value) {
289
+ exports.stringify = function(value) {
272
290
  var typeHint = type(value);
273
291
 
274
292
  if (!~['object', 'array', 'function'].indexOf(typeHint)) {
275
293
  if (typeHint === 'buffer') {
276
294
  var json = Buffer.prototype.toJSON.call(value);
277
295
  // Based on the toJSON result
278
- return jsonStringify(json.data && json.type ? json.data : json, 2)
279
- .replace(/,(\n|$)/g, '$1');
296
+ return jsonStringify(
297
+ json.data && json.type ? json.data : json,
298
+ 2
299
+ ).replace(/,(\n|$)/g, '$1');
280
300
  }
281
301
 
282
302
  // IE7/IE8 has a bizarre String constructor; needs to be coerced
283
303
  // into an array and back to obj.
284
304
  if (typeHint === 'string' && typeof value === 'object') {
285
- value = value.split('').reduce(function (acc, char, idx) {
305
+ value = value.split('').reduce(function(acc, char, idx) {
286
306
  acc[idx] = char;
287
307
  return acc;
288
308
  }, {});
@@ -294,7 +314,10 @@ exports.stringify = function (value) {
294
314
 
295
315
  for (var prop in value) {
296
316
  if (Object.prototype.hasOwnProperty.call(value, prop)) {
297
- return jsonStringify(exports.canonicalize(value, null, typeHint), 2).replace(/,(\n|$)/g, '$1');
317
+ return jsonStringify(
318
+ exports.canonicalize(value, null, typeHint),
319
+ 2
320
+ ).replace(/,(\n|$)/g, '$1');
298
321
  }
299
322
  }
300
323
 
@@ -310,7 +333,7 @@ exports.stringify = function (value) {
310
333
  * @param {number=} depth
311
334
  * @returns {*}
312
335
  */
313
- function jsonStringify (object, spaces, depth) {
336
+ function jsonStringify(object, spaces, depth) {
314
337
  if (typeof spaces === 'undefined') {
315
338
  // primitive types
316
339
  return _stringify(object);
@@ -320,13 +343,16 @@ function jsonStringify (object, spaces, depth) {
320
343
  var space = spaces * depth;
321
344
  var str = Array.isArray(object) ? '[' : '{';
322
345
  var end = Array.isArray(object) ? ']' : '}';
323
- var length = typeof object.length === 'number' ? object.length : Object.keys(object).length;
346
+ var length =
347
+ typeof object.length === 'number'
348
+ ? object.length
349
+ : Object.keys(object).length;
324
350
  // `.repeat()` polyfill
325
- function repeat (s, n) {
351
+ function repeat(s, n) {
326
352
  return new Array(n).join(s);
327
353
  }
328
354
 
329
- function _stringify (val) {
355
+ function _stringify(val) {
330
356
  switch (type(val)) {
331
357
  case 'null':
332
358
  case 'undefined':
@@ -340,9 +366,10 @@ function jsonStringify (object, spaces, depth) {
340
366
  case 'regexp':
341
367
  case 'symbol':
342
368
  case 'number':
343
- val = val === 0 && (1 / val) === -Infinity // `-0`
344
- ? '-0'
345
- : val.toString();
369
+ val =
370
+ val === 0 && 1 / val === -Infinity // `-0`
371
+ ? '-0'
372
+ : val.toString();
346
373
  break;
347
374
  case 'date':
348
375
  var sDate = isNaN(val.getTime()) ? val.toString() : val.toISOString();
@@ -355,9 +382,10 @@ function jsonStringify (object, spaces, depth) {
355
382
  val = '[Buffer: ' + jsonStringify(json, 2, depth + 1) + ']';
356
383
  break;
357
384
  default:
358
- val = (val === '[Function]' || val === '[Circular]')
359
- ? val
360
- : JSON.stringify(val); // string
385
+ val =
386
+ val === '[Function]' || val === '[Circular]'
387
+ ? val
388
+ : JSON.stringify(val); // string
361
389
  }
362
390
  return val;
363
391
  }
@@ -367,15 +395,19 @@ function jsonStringify (object, spaces, depth) {
367
395
  continue; // not my business
368
396
  }
369
397
  --length;
370
- str += '\n ' + repeat(' ', space) +
398
+ str +=
399
+ '\n ' +
400
+ repeat(' ', space) +
371
401
  (Array.isArray(object) ? '' : '"' + i + '": ') + // key
372
402
  _stringify(object[i]) + // value
373
403
  (length ? ',' : ''); // comma
374
404
  }
375
405
 
376
- return str +
406
+ return (
407
+ str +
377
408
  // [], {}
378
- (str.length !== 1 ? '\n' + repeat(' ', --space) + end : end);
409
+ (str.length !== 1 ? '\n' + repeat(' ', --space) + end : end)
410
+ );
379
411
  }
380
412
 
381
413
  /**
@@ -397,13 +429,13 @@ function jsonStringify (object, spaces, depth) {
397
429
  * @param {string} [typeHint] Type hint
398
430
  * @return {(Object|Array|Function|string|undefined)}
399
431
  */
400
- exports.canonicalize = function canonicalize (value, stack, typeHint) {
432
+ exports.canonicalize = function canonicalize(value, stack, typeHint) {
401
433
  var canonicalizedObj;
402
434
  /* eslint-disable no-unused-vars */
403
435
  var prop;
404
436
  /* eslint-enable no-unused-vars */
405
437
  typeHint = typeHint || type(value);
406
- function withStack (value, fn) {
438
+ function withStack(value, fn) {
407
439
  stack.push(value);
408
440
  fn();
409
441
  stack.pop();
@@ -422,8 +454,8 @@ exports.canonicalize = function canonicalize (value, stack, typeHint) {
422
454
  canonicalizedObj = value;
423
455
  break;
424
456
  case 'array':
425
- withStack(value, function () {
426
- canonicalizedObj = value.map(function (item) {
457
+ withStack(value, function() {
458
+ canonicalizedObj = value.map(function(item) {
427
459
  return exports.canonicalize(item, stack);
428
460
  });
429
461
  });
@@ -442,10 +474,12 @@ exports.canonicalize = function canonicalize (value, stack, typeHint) {
442
474
  /* falls through */
443
475
  case 'object':
444
476
  canonicalizedObj = canonicalizedObj || {};
445
- withStack(value, function () {
446
- Object.keys(value).sort().forEach(function (key) {
447
- canonicalizedObj[key] = exports.canonicalize(value[key], stack);
448
- });
477
+ withStack(value, function() {
478
+ Object.keys(value)
479
+ .sort()
480
+ .forEach(function(key) {
481
+ canonicalizedObj[key] = exports.canonicalize(value[key], stack);
482
+ });
449
483
  });
450
484
  break;
451
485
  case 'date':
@@ -473,7 +507,7 @@ exports.canonicalize = function canonicalize (value, stack, typeHint) {
473
507
  * @param {boolean} recursive Whether or not to recurse into subdirectories.
474
508
  * @return {string[]} An array of paths.
475
509
  */
476
- exports.lookupFiles = function lookupFiles (filepath, extensions, recursive) {
510
+ exports.lookupFiles = function lookupFiles(filepath, extensions, recursive) {
477
511
  var files = [];
478
512
 
479
513
  if (!fs.existsSync(filepath)) {
@@ -498,7 +532,7 @@ exports.lookupFiles = function lookupFiles (filepath, extensions, recursive) {
498
532
  return;
499
533
  }
500
534
 
501
- fs.readdirSync(filepath).forEach(function (file) {
535
+ fs.readdirSync(filepath).forEach(function(file) {
502
536
  file = path.join(filepath, file);
503
537
  try {
504
538
  var stat = fs.statSync(file);
@@ -512,6 +546,11 @@ exports.lookupFiles = function lookupFiles (filepath, extensions, recursive) {
512
546
  // ignore error
513
547
  return;
514
548
  }
549
+ if (!extensions) {
550
+ throw new Error(
551
+ 'extensions parameter required when filepath is a directory'
552
+ );
553
+ }
515
554
  var re = new RegExp('\\.(?:' + extensions.join('|') + ')$');
516
555
  if (!stat.isFile() || !re.test(file) || path.basename(file)[0] === '.') {
517
556
  return;
@@ -528,8 +567,10 @@ exports.lookupFiles = function lookupFiles (filepath, extensions, recursive) {
528
567
  * @return {Error}
529
568
  */
530
569
 
531
- exports.undefinedError = function () {
532
- return new Error('Caught undefined error, did you throw without specifying what?');
570
+ exports.undefinedError = function() {
571
+ return new Error(
572
+ 'Caught undefined error, did you throw without specifying what?'
573
+ );
533
574
  };
534
575
 
535
576
  /**
@@ -539,7 +580,7 @@ exports.undefinedError = function () {
539
580
  * @return {Error}
540
581
  */
541
582
 
542
- exports.getError = function (err) {
583
+ exports.getError = function(err) {
543
584
  return err || exports.undefinedError();
544
585
  };
545
586
 
@@ -552,9 +593,9 @@ exports.getError = function (err) {
552
593
  * (i.e: strip Mocha and internal node functions from stack trace).
553
594
  * @returns {Function}
554
595
  */
555
- exports.stackTraceFilter = function () {
596
+ exports.stackTraceFilter = function() {
556
597
  // TODO: Replace with `process.browser`
557
- var is = typeof document === 'undefined' ? { node: true } : { browser: true };
598
+ var is = typeof document === 'undefined' ? {node: true} : {browser: true};
558
599
  var slash = path.sep;
559
600
  var cwd;
560
601
  if (is.node) {
@@ -562,30 +603,35 @@ exports.stackTraceFilter = function () {
562
603
  } else {
563
604
  cwd = (typeof location === 'undefined'
564
605
  ? window.location
565
- : location).href.replace(/\/[^/]*$/, '/');
606
+ : location
607
+ ).href.replace(/\/[^/]*$/, '/');
566
608
  slash = '/';
567
609
  }
568
610
 
569
- function isMochaInternal (line) {
570
- return (~line.indexOf('node_modules' + slash + 'mocha' + slash)) ||
571
- (~line.indexOf('node_modules' + slash + 'mocha.js')) ||
572
- (~line.indexOf('bower_components' + slash + 'mocha.js')) ||
573
- (~line.indexOf(slash + 'mocha.js'));
611
+ function isMochaInternal(line) {
612
+ return (
613
+ ~line.indexOf('node_modules' + slash + 'mocha' + slash) ||
614
+ ~line.indexOf('node_modules' + slash + 'mocha.js') ||
615
+ ~line.indexOf('bower_components' + slash + 'mocha.js') ||
616
+ ~line.indexOf(slash + 'mocha.js')
617
+ );
574
618
  }
575
619
 
576
- function isNodeInternal (line) {
577
- return (~line.indexOf('(timers.js:')) ||
578
- (~line.indexOf('(events.js:')) ||
579
- (~line.indexOf('(node.js:')) ||
580
- (~line.indexOf('(module.js:')) ||
581
- (~line.indexOf('GeneratorFunctionPrototype.next (native)')) ||
582
- false;
620
+ function isNodeInternal(line) {
621
+ return (
622
+ ~line.indexOf('(timers.js:') ||
623
+ ~line.indexOf('(events.js:') ||
624
+ ~line.indexOf('(node.js:') ||
625
+ ~line.indexOf('(module.js:') ||
626
+ ~line.indexOf('GeneratorFunctionPrototype.next (native)') ||
627
+ false
628
+ );
583
629
  }
584
630
 
585
- return function (stack) {
631
+ return function(stack) {
586
632
  stack = stack.split('\n');
587
633
 
588
- stack = stack.reduce(function (list, line) {
634
+ stack = stack.reduce(function(list, line) {
589
635
  if (isMochaInternal(line)) {
590
636
  return list;
591
637
  }
@@ -613,7 +659,7 @@ exports.stackTraceFilter = function () {
613
659
  * @param {*} value
614
660
  * @returns {boolean} Whether or not `value` is a Promise
615
661
  */
616
- exports.isPromise = function isPromise (value) {
662
+ exports.isPromise = function isPromise(value) {
617
663
  return typeof value === 'object' && typeof value.then === 'function';
618
664
  };
619
665
 
@@ -621,4 +667,4 @@ exports.isPromise = function isPromise (value) {
621
667
  * It's a noop.
622
668
  * @api
623
669
  */
624
- exports.noop = function () {};
670
+ exports.noop = function() {};