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.
package/bin/_mocha CHANGED
@@ -7,52 +7,141 @@
7
7
  * Module dependencies.
8
8
  */
9
9
 
10
- var program = require('commander');
11
- var path = require('path');
12
- var fs = require('fs');
13
- var resolve = path.resolve;
14
- var exists = fs.existsSync || path.existsSync;
15
- var Mocha = require('../');
16
- var utils = Mocha.utils;
17
- var interfaceNames = Object.keys(Mocha.interfaces);
18
- var join = path.join;
19
- var cwd = process.cwd();
20
- var getOptions = require('./options');
21
- var mocha = new Mocha();
10
+ const program = require('commander');
11
+ const path = require('path');
12
+ const fs = require('fs');
13
+ const resolve = path.resolve;
14
+ const exists = fs.existsSync;
15
+ const Mocha = require('../');
16
+ const utils = Mocha.utils;
17
+ const interfaceNames = Object.keys(Mocha.interfaces);
18
+ const join = path.join;
19
+ const cwd = process.cwd();
20
+ const getOptions = require('./options');
21
+ const mocha = new Mocha();
22
22
 
23
23
  /**
24
24
  * Save timer references to avoid Sinon interfering (see GH-237).
25
25
  */
26
26
 
27
- var Date = global.Date;
28
- var setTimeout = global.setTimeout;
29
- var setInterval = global.setInterval;
30
- var clearTimeout = global.clearTimeout;
31
- var clearInterval = global.clearInterval;
27
+ const Date = global.Date;
28
+ const setTimeout = global.setTimeout;
29
+ const setInterval = global.setInterval;
30
+ const clearTimeout = global.clearTimeout;
31
+ const clearInterval = global.clearInterval;
32
+
33
+ /**
34
+ * Exits Mocha when tests + code under test has finished execution (default)
35
+ * @param {number} code - Exit code; typically # of failures
36
+ */
37
+ const exitLater = code => {
38
+ process.on('exit', () => {
39
+ process.exit(Math.min(code, 255));
40
+ });
41
+ };
42
+
43
+ /**
44
+ * Exits Mocha when Mocha itself has finished execution, regardless of
45
+ * what the tests or code under test is doing.
46
+ * @param {number} code - Exit code; typically # of failures
47
+ */
48
+ const exit = code => {
49
+ const clampedCode = Math.min(code, 255);
50
+ let draining = 0;
51
+
52
+ // Eagerly set the process's exit code in case stream.write doesn't
53
+ // execute its callback before the process terminates.
54
+ process.exitCode = clampedCode;
55
+
56
+ // flush output for Node.js Windows pipe bug
57
+ // https://github.com/joyent/node/issues/6247 is just one bug example
58
+ // https://github.com/visionmedia/mocha/issues/333 has a good discussion
59
+ const done = () => {
60
+ if (!(draining--)) {
61
+ process.exit(clampedCode);
62
+ }
63
+ };
64
+
65
+ const streams = [process.stdout, process.stderr];
66
+
67
+ streams.forEach(stream => {
68
+ // submit empty write request and wait for completion
69
+ draining += 1;
70
+ stream.write('', done);
71
+ });
72
+
73
+ done();
74
+ };
75
+
76
+ /**
77
+ * Parse list.
78
+ */
79
+ const list = str => str.split(/ *, */);
80
+
81
+ /**
82
+ * Parse multiple flag.
83
+ */
84
+ const collect = (val, memo) => memo.concat(val);
85
+
86
+ /**
87
+ * Hide the cursor.
88
+ */
89
+ const hideCursor = () => {
90
+ process.stdout.write('\u001b[?25l');
91
+ };
92
+
93
+ /**
94
+ * Show the cursor.
95
+ */
96
+ const showCursor = () => {
97
+ process.stdout.write('\u001b[?25h');
98
+ };
99
+
100
+ /**
101
+ * Stop play()ing.
102
+ */
103
+ const stop = () => {
104
+ process.stdout.write('\u001b[2K');
105
+ clearInterval(play.timer);
106
+ };
107
+
108
+ /**
109
+ * Play the given array of strings.
110
+ */
111
+ const play = (arr, interval) => {
112
+ const len = arr.length;
113
+ interval = interval || 100;
114
+ let i = 0;
115
+
116
+ play.timer = setInterval(() => {
117
+ const str = arr[i++ % len];
118
+ process.stdout.write(`\u001b[0G${str}`);
119
+ }, interval);
120
+ };
32
121
 
33
122
  /**
34
123
  * Files.
35
124
  */
36
125
 
37
- var files = [];
126
+ let files = [];
38
127
 
39
128
  /**
40
129
  * Globals.
41
130
  */
42
131
 
43
- var globals = [];
132
+ let globals = [];
44
133
 
45
134
  /**
46
135
  * Requires.
47
136
  */
48
137
 
49
- var requires = [];
138
+ const requires = [];
50
139
 
51
140
  /**
52
141
  * Images.
53
142
  */
54
143
 
55
- var images = {
144
+ const images = {
56
145
  fail: path.join(__dirname, '..', 'images', 'error.png'),
57
146
  pass: path.join(__dirname, '..', 'images', 'ok.png')
58
147
  };
@@ -78,7 +167,7 @@ program
78
167
  .option('-r, --require <name>', 'require the given module')
79
168
  .option('-s, --slow <ms>', '"slow" test threshold in milliseconds [75]')
80
169
  .option('-t, --timeout <ms>', 'set test-case timeout in milliseconds [2000]')
81
- .option('-u, --ui <name>', 'specify user-interface (' + interfaceNames.join('|') + ')', 'bdd')
170
+ .option('-u, --ui <name>', `specify user-interface (${interfaceNames.join('|')})`, 'bdd')
82
171
  .option('-w, --watch', 'watch files for changes')
83
172
  .option('--check-leaks', 'check for global variable leaks')
84
173
  .option('--full-trace', 'display the full stack trace')
@@ -90,11 +179,12 @@ program
90
179
  .option('--preserve-symlinks', 'Instructs the module loader to preserve symbolic links when resolving and caching modules')
91
180
  .option('--icu-data-dir', 'include ICU data')
92
181
  .option('--inline-diffs', 'display actual/expected differences inline within each string')
182
+ .option('--no-diff', 'do not show a diff on failure')
93
183
  .option('--inspect', 'activate devtools in chrome')
94
184
  .option('--inspect-brk', 'activate devtools in chrome and break on the first line')
95
185
  .option('--interfaces', 'display available interfaces')
96
186
  .option('--no-deprecation', 'silence deprecation warnings')
97
- .option('--no-exit', 'require a clean shutdown of the event loop: mocha will not call process.exit')
187
+ .option('--exit', 'force shutdown of the event loop after test run: mocha will call process.exit')
98
188
  .option('--no-timeouts', 'disables timeouts, given implicitly with --debug')
99
189
  .option('--no-warnings', 'silence all node process warnings')
100
190
  .option('--opts <path>', 'specify opts path', 'test/mocha.opts')
@@ -114,7 +204,8 @@ program
114
204
  .option('--delay', 'wait for async suite definition')
115
205
  .option('--allow-uncaught', 'enable uncaught errors to propagate')
116
206
  .option('--forbid-only', 'causes test marked with only to fail the suite')
117
- .option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite');
207
+ .option('--forbid-pending', 'causes pending tests and test marked with skip to fail the suite')
208
+ .option('--file <file>', 'include a file to be ran during the suite', collect, []);
118
209
 
119
210
  program._name = 'mocha';
120
211
 
@@ -123,12 +214,12 @@ program._name = 'mocha';
123
214
  program
124
215
  .command('init <path>')
125
216
  .description('initialize a client-side mocha setup at <path>')
126
- .action(function (path) {
127
- var mkdir = require('mkdirp');
217
+ .action(path => {
218
+ const mkdir = require('mkdirp');
128
219
  mkdir.sync(path);
129
- var css = fs.readFileSync(join(__dirname, '..', 'mocha.css'));
130
- var js = fs.readFileSync(join(__dirname, '..', 'mocha.js'));
131
- var tmpl = fs.readFileSync(join(__dirname, '..', 'lib/template.html'));
220
+ const css = fs.readFileSync(join(__dirname, '..', 'mocha.css'));
221
+ const js = fs.readFileSync(join(__dirname, '..', 'mocha.js'));
222
+ const tmpl = fs.readFileSync(join(__dirname, '..', 'lib/template.html'));
132
223
  fs.writeFileSync(join(path, 'mocha.css'), css);
133
224
  fs.writeFileSync(join(path, 'mocha.js'), js);
134
225
  fs.writeFileSync(join(path, 'tests.js'), '');
@@ -138,13 +229,13 @@ program
138
229
 
139
230
  // --globals
140
231
 
141
- program.on('globals', function (val) {
232
+ program.on('option:globals', val => {
142
233
  globals = globals.concat(list(val));
143
234
  });
144
235
 
145
236
  // --reporters
146
237
 
147
- program.on('reporters', function () {
238
+ program.on('option:reporters', () => {
148
239
  console.log();
149
240
  console.log(' dot - dot matrix');
150
241
  console.log(' doc - html documentation');
@@ -165,10 +256,10 @@ program.on('reporters', function () {
165
256
 
166
257
  // --interfaces
167
258
 
168
- program.on('interfaces', function () {
259
+ program.on('option:interfaces', () => {
169
260
  console.log('');
170
- interfaceNames.forEach(function (interfaceName) {
171
- console.log(' ' + interfaceName);
261
+ interfaceNames.forEach(interfaceName => {
262
+ console.log(` ${interfaceName}`);
172
263
  });
173
264
  console.log('');
174
265
  process.exit();
@@ -178,8 +269,8 @@ program.on('interfaces', function () {
178
269
 
179
270
  module.paths.push(cwd, join(cwd, 'node_modules'));
180
271
 
181
- program.on('require', function (mod) {
182
- var abs = exists(mod) || exists(mod + '.js');
272
+ program.on('option:require', mod => {
273
+ const abs = exists(mod) || exists(`${mod}.js`);
183
274
  if (abs) {
184
275
  mod = resolve(mod);
185
276
  }
@@ -201,12 +292,12 @@ Error.stackTraceLimit = Infinity; // TODO: config
201
292
 
202
293
  // reporter options
203
294
 
204
- var reporterOptions = {};
295
+ const reporterOptions = {};
205
296
  if (program.reporterOptions !== undefined) {
206
- program.reporterOptions.split(',').forEach(function (opt) {
207
- var L = opt.split('=');
297
+ program.reporterOptions.split(',').forEach(opt => {
298
+ const L = opt.split('=');
208
299
  if (L.length > 2 || L.length === 0) {
209
- throw new Error("invalid reporter option '" + opt + "'");
300
+ throw new Error(`invalid reporter option '${opt}'`);
210
301
  } else if (L.length === 2) {
211
302
  reporterOptions[L[0]] = L[1];
212
303
  } else {
@@ -221,14 +312,14 @@ mocha.reporter(program.reporter, reporterOptions);
221
312
 
222
313
  // load reporter
223
314
 
224
- var Reporter = null;
315
+ let Reporter = null;
225
316
  try {
226
- Reporter = require('../lib/reporters/' + program.reporter);
317
+ Reporter = require(`../lib/reporters/${program.reporter}`);
227
318
  } catch (err) {
228
319
  try {
229
320
  Reporter = require(program.reporter);
230
321
  } catch (err2) {
231
- throw new Error('reporter "' + program.reporter + '" does not exist');
322
+ throw new Error(`reporter "${program.reporter}" does not exist`);
232
323
  }
233
324
  }
234
325
 
@@ -250,6 +341,12 @@ if (program.inlineDiffs) {
250
341
  mocha.useInlineDiffs(true);
251
342
  }
252
343
 
344
+ // --no-diff
345
+
346
+ if (process.argv.indexOf('--no-diff') !== -1) {
347
+ mocha.hideDiff(true);
348
+ }
349
+
253
350
  // --slow <ms>
254
351
 
255
352
  if (program.slow) {
@@ -346,11 +443,15 @@ if (program.forbidPending) mocha.forbidPending();
346
443
 
347
444
  // custom compiler support
348
445
 
349
- var extensions = ['js'];
350
- program.compilers.forEach(function (c) {
351
- var idx = c.indexOf(':');
352
- var ext = c.slice(0, idx);
353
- var mod = c.slice(idx + 1);
446
+ if (program.compilers.length > 0) {
447
+ require('util').deprecate(() => {},
448
+ '"--compilers" will be removed in a future version of Mocha; see https://git.io/vdcSr for more info')();
449
+ }
450
+ const extensions = ['js'];
451
+ program.compilers.forEach(c => {
452
+ const idx = c.indexOf(':');
453
+ const ext = c.slice(0, idx);
454
+ let mod = c.slice(idx + 1);
354
455
 
355
456
  if (mod[0] === '.') {
356
457
  mod = join(process.cwd(), mod);
@@ -362,7 +463,7 @@ program.compilers.forEach(function (c) {
362
463
 
363
464
  // requires
364
465
 
365
- requires.forEach(function (mod) {
466
+ requires.forEach(mod => {
366
467
  require(mod);
367
468
  });
368
469
 
@@ -372,7 +473,7 @@ mocha.ui(program.ui);
372
473
 
373
474
  // args
374
475
 
375
- var args = program.args;
476
+ const args = program.args;
376
477
 
377
478
  // default files to test/*.{js,coffee}
378
479
 
@@ -380,13 +481,13 @@ if (!args.length) {
380
481
  args.push('test');
381
482
  }
382
483
 
383
- args.forEach(function (arg) {
384
- var newFiles;
484
+ args.forEach(arg => {
485
+ let newFiles;
385
486
  try {
386
487
  newFiles = utils.lookupFiles(arg, extensions, program.recursive);
387
488
  } catch (err) {
388
489
  if (err.message.indexOf('cannot resolve path') === 0) {
389
- console.error('Warning: Could not find any test files matching pattern: ' + arg);
490
+ console.error(`Warning: Could not find any test files matching pattern: ${arg}`);
390
491
  return;
391
492
  }
392
493
 
@@ -402,39 +503,40 @@ if (!files.length) {
402
503
  }
403
504
 
404
505
  // resolve
405
-
406
- files = files.map(function (path) {
407
- return resolve(path);
408
- });
506
+ let fileArgs = program.file.map(path => resolve(path));
507
+ files = files.map(path => resolve(path));
409
508
 
410
509
  if (program.sort) {
411
510
  files.sort();
412
511
  }
413
512
 
513
+ // add files given through --file to be ran first
514
+ files = fileArgs.concat(files);
515
+
414
516
  // --watch
415
517
 
416
- var runner;
417
- var loadAndRun;
418
- var purge;
419
- var rerun;
518
+ let runner;
519
+ let loadAndRun;
520
+ let purge;
521
+ let rerun;
420
522
 
421
523
  if (program.watch) {
422
524
  console.log();
423
525
  hideCursor();
424
- process.on('SIGINT', function () {
526
+ process.on('SIGINT', () => {
425
527
  showCursor();
426
528
  console.log('\n');
427
529
  process.exit(130);
428
530
  });
429
531
 
430
- var watchFiles = utils.files(cwd, [ 'js' ].concat(program.watchExtensions));
431
- var runAgain = false;
532
+ const watchFiles = utils.files(cwd, [ 'js' ].concat(program.watchExtensions));
533
+ let runAgain = false;
432
534
 
433
- loadAndRun = function loadAndRun () {
535
+ loadAndRun = () => {
434
536
  try {
435
537
  mocha.files = files;
436
538
  runAgain = false;
437
- runner = mocha.run(function () {
539
+ runner = mocha.run(() => {
438
540
  runner = null;
439
541
  if (runAgain) {
440
542
  rerun();
@@ -445,15 +547,15 @@ if (program.watch) {
445
547
  }
446
548
  };
447
549
 
448
- purge = function purge () {
449
- watchFiles.forEach(function (file) {
550
+ purge = () => {
551
+ watchFiles.forEach(file => {
450
552
  delete require.cache[file];
451
553
  });
452
554
  };
453
555
 
454
556
  loadAndRun();
455
557
 
456
- rerun = function rerun () {
558
+ rerun = () => {
457
559
  purge();
458
560
  stop();
459
561
  if (!program.grep) {
@@ -465,7 +567,7 @@ if (program.watch) {
465
567
  loadAndRun();
466
568
  };
467
569
 
468
- utils.watch(watchFiles, function () {
570
+ utils.watch(watchFiles, () => {
469
571
  runAgain = true;
470
572
  if (runner) {
471
573
  runner.abort();
@@ -480,41 +582,7 @@ if (program.watch) {
480
582
  runner = mocha.run(program.exit ? exit : exitLater);
481
583
  }
482
584
 
483
- function exitLater (code) {
484
- process.on('exit', function () {
485
- process.exit(Math.min(code, 255));
486
- });
487
- }
488
-
489
- function exit (code) {
490
- var clampedCode = Math.min(code, 255);
491
-
492
- // Eagerly set the process's exit code in case stream.write doesn't
493
- // execute its callback before the process terminates.
494
- process.exitCode = clampedCode;
495
-
496
- // flush output for Node.js Windows pipe bug
497
- // https://github.com/joyent/node/issues/6247 is just one bug example
498
- // https://github.com/visionmedia/mocha/issues/333 has a good discussion
499
- function done () {
500
- if (!(draining--)) {
501
- process.exit(clampedCode);
502
- }
503
- }
504
-
505
- var draining = 0;
506
- var streams = [process.stdout, process.stderr];
507
-
508
- streams.forEach(function (stream) {
509
- // submit empty write request and wait for completion
510
- draining += 1;
511
- stream.write('', done);
512
- });
513
-
514
- done();
515
- }
516
-
517
- process.on('SIGINT', function () {
585
+ process.on('SIGINT', () => {
518
586
  runner.abort();
519
587
 
520
588
  // This is a hack:
@@ -522,51 +590,3 @@ process.on('SIGINT', function () {
522
590
  // The amount of failures will be emitted as error code later
523
591
  runner.failures = 130;
524
592
  });
525
-
526
- /**
527
- * Parse list.
528
- */
529
-
530
- function list (str) {
531
- return str.split(/ *, */);
532
- }
533
-
534
- /**
535
- * Hide the cursor.
536
- */
537
-
538
- function hideCursor () {
539
- process.stdout.write('\u001b[?25l');
540
- }
541
-
542
- /**
543
- * Show the cursor.
544
- */
545
-
546
- function showCursor () {
547
- process.stdout.write('\u001b[?25h');
548
- }
549
-
550
- /**
551
- * Stop play()ing.
552
- */
553
-
554
- function stop () {
555
- process.stdout.write('\u001b[2K');
556
- clearInterval(play.timer);
557
- }
558
-
559
- /**
560
- * Play the given array of strings.
561
- */
562
-
563
- function play (arr, interval) {
564
- var len = arr.length;
565
- interval = interval || 100;
566
- var i = 0;
567
-
568
- play.timer = setInterval(function () {
569
- var str = arr[i++ % len];
570
- process.stdout.write('\u001b[0G' + str);
571
- }, interval);
572
- }
package/bin/mocha CHANGED
@@ -7,17 +7,17 @@
7
7
  * when found, before invoking the "real" _mocha(1) executable.
8
8
  */
9
9
 
10
- var spawn = require('child_process').spawn;
11
- var path = require('path');
12
- var getOptions = require('./options');
13
- var args = [path.join(__dirname, '_mocha')];
10
+ const spawn = require('child_process').spawn;
11
+ const path = require('path');
12
+ const getOptions = require('./options');
13
+ const args = [path.join(__dirname, '_mocha')];
14
14
 
15
15
  // Load mocha.opts into process.argv
16
16
  // Must be loaded here to handle node-specific options
17
17
  getOptions();
18
18
 
19
- process.argv.slice(2).forEach(function (arg) {
20
- var flag = arg.split('=')[0];
19
+ process.argv.slice(2).forEach(arg => {
20
+ const flag = arg.split('=')[0];
21
21
 
22
22
  switch (flag) {
23
23
  case '-d':
@@ -69,9 +69,11 @@ process.argv.slice(2).forEach(function (arg) {
69
69
  }
70
70
  });
71
71
 
72
- var proc = spawn(process.execPath, args, { stdio: 'inherit' });
73
- proc.on('exit', function (code, signal) {
74
- process.on('exit', function () {
72
+ const proc = spawn(process.execPath, args, {
73
+ stdio: 'inherit'
74
+ });
75
+ proc.on('exit', (code, signal) => {
76
+ process.on('exit', () => {
75
77
  if (signal) {
76
78
  process.kill(process.pid, signal);
77
79
  } else {
@@ -81,7 +83,7 @@ proc.on('exit', function (code, signal) {
81
83
  });
82
84
 
83
85
  // terminate children.
84
- process.on('SIGINT', function () {
86
+ process.on('SIGINT', () => {
85
87
  proc.kill('SIGINT'); // calls runner.abort()
86
88
  proc.kill('SIGTERM'); // if that didn't work, we're probably in an infinite loop, so make it die.
87
89
  });
package/bin/options.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * Dependencies.
5
5
  */
6
6
 
7
- var fs = require('fs');
7
+ const fs = require('fs');
8
8
 
9
9
  /**
10
10
  * Export `getOptions`.
@@ -17,18 +17,20 @@ module.exports = getOptions;
17
17
  */
18
18
 
19
19
  function getOptions () {
20
- var optsPath = process.argv.indexOf('--opts') === -1
20
+ if (process.argv.length === 3 && (process.argv[2] === '-h' || process.argv[2] === '--help')) {
21
+ return;
22
+ }
23
+
24
+ const optsPath = process.argv.indexOf('--opts') === -1
21
25
  ? 'test/mocha.opts'
22
26
  : process.argv[process.argv.indexOf('--opts') + 1];
23
27
 
24
28
  try {
25
- var opts = fs.readFileSync(optsPath, 'utf8')
29
+ const opts = fs.readFileSync(optsPath, 'utf8')
26
30
  .replace(/\\\s/g, '%20')
27
31
  .split(/\s/)
28
32
  .filter(Boolean)
29
- .map(function (value) {
30
- return value.replace(/%20/g, ' ');
31
- });
33
+ .map(value => value.replace(/%20/g, ' '));
32
34
 
33
35
  process.argv = process.argv
34
36
  .slice(0, 2)
package/browser-entry.js CHANGED
@@ -7,7 +7,7 @@
7
7
  * Shim process.stdout.
8
8
  */
9
9
 
10
- process.stdout = require('browser-stdout')();
10
+ process.stdout = require('browser-stdout')({level: false});
11
11
 
12
12
  var Mocha = require('./lib/mocha');
13
13
 
@@ -45,7 +45,7 @@ process.removeListener = function (e, fn) {
45
45
  } else {
46
46
  global.onerror = function () {};
47
47
  }
48
- var i = Mocha.utils.indexOf(uncaughtExceptionHandlers, fn);
48
+ var i = uncaughtExceptionHandlers.indexOf(fn);
49
49
  if (i !== -1) {
50
50
  uncaughtExceptionHandlers.splice(i, 1);
51
51
  }
@@ -103,7 +103,7 @@ Mocha.Runner.immediately = function (callback) {
103
103
  * only receive the 'message' attribute of the Error.
104
104
  */
105
105
  mocha.throwError = function (err) {
106
- Mocha.utils.forEach(uncaughtExceptionHandlers, function (fn) {
106
+ uncaughtExceptionHandlers.forEach(function (fn) {
107
107
  fn(err);
108
108
  });
109
109
  throw err;
File without changes
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ // just stub out growl
4
+
5
+ module.exports = require('../utils').noop;