mocha 6.1.3 → 6.2.2

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.
@@ -9,15 +9,14 @@
9
9
 
10
10
  const fs = require('fs');
11
11
  const path = require('path');
12
- const ansi = require('ansi-colors');
13
12
  const debug = require('debug')('mocha:cli:run:helpers');
14
- const minimatch = require('minimatch');
15
- const Context = require('../context');
16
- const Mocha = require('../mocha');
17
- const utils = require('../utils');
13
+ const watchRun = require('./watch-run');
14
+ const collectFiles = require('./collect-files');
18
15
 
19
16
  const cwd = (exports.cwd = process.cwd());
20
17
 
18
+ exports.watchRun = watchRun;
19
+
21
20
  /**
22
21
  * Exits Mocha when tests + code under test has finished execution (default)
23
22
  * @param {number} code - Exit code; typically # of failures
@@ -65,32 +64,6 @@ const exitMocha = code => {
65
64
  done();
66
65
  };
67
66
 
68
- /**
69
- * Hide the cursor.
70
- * @ignore
71
- * @private
72
- */
73
- const hideCursor = () => {
74
- process.stdout.write('\u001b[?25l');
75
- };
76
-
77
- /**
78
- * Show the cursor.
79
- * @ignore
80
- * @private
81
- */
82
- const showCursor = () => {
83
- process.stdout.write('\u001b[?25h');
84
- };
85
-
86
- /**
87
- * Stop cursor business
88
- * @private
89
- */
90
- const stop = () => {
91
- process.stdout.write('\u001b[2K');
92
- };
93
-
94
67
  /**
95
68
  * Coerce a comma-delimited string (or array thereof) into a flattened array of
96
69
  * strings
@@ -119,185 +92,54 @@ exports.handleRequires = (requires = []) => {
119
92
  };
120
93
 
121
94
  /**
122
- * Smash together an array of test files in the correct order
123
- * @param {Object} [opts] - Options
124
- * @param {string[]} [opts.extension] - File extensions to use
125
- * @param {string[]} [opts.spec] - Files, dirs, globs to run
126
- * @param {string[]} [opts.exclude] - Files, dirs, globs to exclude
127
- * @param {boolean} [opts.recursive=false] - Find files recursively
128
- * @param {boolean} [opts.sort=false] - Sort test files
129
- * @returns {string[]} List of files to test
130
- * @private
131
- */
132
- exports.handleFiles = ({
133
- exclude = [],
134
- extension = [],
135
- file = [],
136
- recursive = false,
137
- sort = false,
138
- spec = []
139
- } = {}) => {
140
- let files = [];
141
- const unmatched = [];
142
- spec.forEach(arg => {
143
- let newFiles;
144
- try {
145
- newFiles = utils.lookupFiles(arg, extension, recursive);
146
- } catch (err) {
147
- if (err.code === 'ERR_MOCHA_NO_FILES_MATCH_PATTERN') {
148
- unmatched.push({message: err.message, pattern: err.pattern});
149
- return;
150
- }
151
-
152
- throw err;
153
- }
154
-
155
- if (typeof newFiles !== 'undefined') {
156
- if (typeof newFiles === 'string') {
157
- newFiles = [newFiles];
158
- }
159
- newFiles = newFiles.filter(fileName =>
160
- exclude.every(pattern => !minimatch(fileName, pattern))
161
- );
162
- }
163
-
164
- files = files.concat(newFiles);
165
- });
166
-
167
- if (!files.length) {
168
- // give full message details when only 1 file is missing
169
- const noneFoundMsg =
170
- unmatched.length === 1
171
- ? `Error: No test files found: ${JSON.stringify(unmatched[0].pattern)}` // stringify to print escaped characters raw
172
- : 'Error: No test files found';
173
- console.error(ansi.red(noneFoundMsg));
174
- process.exit(1);
175
- } else {
176
- // print messages as an warning
177
- unmatched.forEach(warning => {
178
- console.warn(ansi.yellow(`Warning: ${warning.message}`));
179
- });
180
- }
181
-
182
- const fileArgs = file.map(filepath => path.resolve(filepath));
183
- files = files.map(filepath => path.resolve(filepath));
184
-
185
- // ensure we don't sort the stuff from fileArgs; order is important!
186
- if (sort) {
187
- files.sort();
188
- }
189
-
190
- // add files given through --file to be ran first
191
- files = fileArgs.concat(files);
192
- debug('files (in order): ', files);
193
- return files;
194
- };
195
-
196
- /**
197
- * Give Mocha files and tell it to run
95
+ * Collect test files and run mocha instance.
198
96
  * @param {Mocha} mocha - Mocha instance
199
- * @param {Options} [opts] - Options
200
- * @param {string[]} [opts.files] - List of test files
97
+ * @param {Options} [opts] - Command line options
201
98
  * @param {boolean} [opts.exit] - Whether or not to force-exit after tests are complete
99
+ * @param {Object} fileCollectParams - Parameters that control test
100
+ * file collection. See `lib/cli/collect-files.js`.
202
101
  * @returns {Runner}
203
102
  * @private
204
103
  */
205
- exports.singleRun = (mocha, {files = [], exit = false} = {}) => {
104
+ exports.singleRun = (mocha, {exit}, fileCollectParams) => {
105
+ const files = collectFiles(fileCollectParams);
106
+ debug('running tests with files', files);
206
107
  mocha.files = files;
207
108
  return mocha.run(exit ? exitMocha : exitMochaLater);
208
109
  };
209
110
 
210
111
  /**
211
- * Run Mocha in "watch" mode
112
+ * Actually run tests
212
113
  * @param {Mocha} mocha - Mocha instance
213
- * @param {Object} [opts] - Options
214
- * @param {string[]} [opts.extension] - List of extensions to watch
215
- * @param {string|RegExp} [opts.grep] - Grep for test titles
216
- * @param {string} [opts.ui=bdd] - User interface
217
- * @param {string[]} [files] - Array of test files
114
+ * @param {Object} opts - Command line options
218
115
  * @private
219
116
  */
220
- exports.watchRun = (
221
- mocha,
222
- {extension = ['js'], grep = '', ui = 'bdd', files = []} = {}
223
- ) => {
224
- let runner;
225
-
226
- console.log();
227
- hideCursor();
228
- process.on('SIGINT', () => {
229
- showCursor();
230
- console.log('\n');
231
- process.exit(130);
232
- });
233
-
234
- const watchFiles = utils.files(cwd, extension);
235
- let runAgain = false;
236
-
237
- const loadAndRun = () => {
238
- try {
239
- mocha.files = files;
240
- runAgain = false;
241
- runner = mocha.run(() => {
242
- runner = null;
243
- if (runAgain) {
244
- rerun();
245
- }
246
- });
247
- } catch (e) {
248
- console.log(e.stack);
249
- }
117
+ exports.runMocha = (mocha, options) => {
118
+ const {
119
+ watch = false,
120
+ extension = [],
121
+ ui = 'bdd',
122
+ exit = false,
123
+ ignore = [],
124
+ file = [],
125
+ recursive = false,
126
+ sort = false,
127
+ spec = []
128
+ } = options;
129
+
130
+ const fileCollectParams = {
131
+ ignore,
132
+ extension,
133
+ file,
134
+ recursive,
135
+ sort,
136
+ spec
250
137
  };
251
138
 
252
- const purge = () => {
253
- watchFiles.forEach(Mocha.unloadFile);
254
- };
255
-
256
- loadAndRun();
257
-
258
- const rerun = () => {
259
- purge();
260
- stop();
261
- if (!grep) {
262
- mocha.grep(null);
263
- }
264
- mocha.suite = mocha.suite.clone();
265
- mocha.suite.ctx = new Context();
266
- mocha.ui(ui);
267
- loadAndRun();
268
- };
269
-
270
- utils.watch(watchFiles, () => {
271
- runAgain = true;
272
- if (runner) {
273
- runner.abort();
274
- } else {
275
- rerun();
276
- }
277
- });
278
- };
279
-
280
- /**
281
- * Actually run tests
282
- * @param {Mocha} mocha - Mocha instance
283
- * @param {Object} [opts] - Options
284
- * @param {boolean} [opts.watch=false] - Enable watch mode
285
- * @param {string[]} [opts.extension] - List of extensions to watch
286
- * @param {string|RegExp} [opts.grep] - Grep for test titles
287
- * @param {string} [opts.ui=bdd] - User interface
288
- * @param {boolean} [opts.exit=false] - Force-exit Mocha when tests done
289
- * @param {string[]} [files] - Array of test files
290
- * @private
291
- */
292
- exports.runMocha = (
293
- mocha,
294
- {watch = false, extension = ['js'], grep = '', ui = 'bdd', exit = false} = {},
295
- files = []
296
- ) => {
297
139
  if (watch) {
298
- exports.watchRun(mocha, {extension, grep, ui, files});
140
+ watchRun(mocha, {ui}, fileCollectParams);
299
141
  } else {
300
- exports.singleRun(mocha, {files, exit});
142
+ exports.singleRun(mocha, {exit}, fileCollectParams);
301
143
  }
302
144
  };
303
145
 
@@ -14,10 +14,10 @@
14
14
  */
15
15
  exports.types = {
16
16
  array: [
17
- 'exclude',
18
17
  'extension',
19
18
  'file',
20
19
  'global',
20
+ 'ignore',
21
21
  'require',
22
22
  'reporter-option',
23
23
  'spec'
@@ -44,8 +44,18 @@ exports.types = {
44
44
  'sort',
45
45
  'watch'
46
46
  ],
47
- number: ['retries', 'slow', 'timeout'],
48
- string: ['fgrep', 'grep', 'package', 'reporter', 'ui']
47
+ number: ['retries'],
48
+ string: [
49
+ 'config',
50
+ 'fgrep',
51
+ 'grep',
52
+ 'opts',
53
+ 'package',
54
+ 'reporter',
55
+ 'ui',
56
+ 'slow',
57
+ 'timeout'
58
+ ]
49
59
  };
50
60
 
51
61
  /**
@@ -63,6 +73,7 @@ exports.aliases = {
63
73
  global: ['globals'],
64
74
  grep: ['g'],
65
75
  growl: ['G'],
76
+ ignore: ['exclude'],
66
77
  invert: ['i'],
67
78
  'no-colors': ['C'],
68
79
  reporter: ['R'],
package/lib/cli/run.js CHANGED
@@ -16,7 +16,6 @@ const {
16
16
 
17
17
  const {
18
18
  list,
19
- handleFiles,
20
19
  handleRequires,
21
20
  validatePlugin,
22
21
  runMocha
@@ -82,12 +81,6 @@ exports.builder = yargs =>
82
81
  description: 'Show diff on failure',
83
82
  group: GROUPS.OUTPUT
84
83
  },
85
- exclude: {
86
- defaultDescription: '(none)',
87
- description: 'Ignore file(s) or glob pattern(s)',
88
- group: GROUPS.FILES,
89
- requiresArg: true
90
- },
91
84
  exit: {
92
85
  description: 'Force Mocha to quit after tests complete',
93
86
  group: GROUPS.RULES
@@ -143,6 +136,12 @@ exports.builder = yargs =>
143
136
  description: 'Enable Growl notifications',
144
137
  group: GROUPS.OUTPUT
145
138
  },
139
+ ignore: {
140
+ defaultDescription: '(none)',
141
+ description: 'Ignore file(s) or glob pattern(s)',
142
+ group: GROUPS.FILES,
143
+ requiresArg: true
144
+ },
146
145
  'inline-diffs': {
147
146
  description:
148
147
  'Display actual/expected differences inline within each string',
@@ -290,8 +289,5 @@ exports.builder = yargs =>
290
289
  exports.handler = argv => {
291
290
  debug('post-yargs config', argv);
292
291
  const mocha = new Mocha(argv);
293
- const files = handleFiles(argv);
294
-
295
- debug('running tests with files', files);
296
- runMocha(mocha, argv, files);
292
+ runMocha(mocha, argv);
297
293
  };
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+
3
+ const utils = require('../utils');
4
+ const Context = require('../context');
5
+ const Mocha = require('../mocha');
6
+ const collectFiles = require('./collect-files');
7
+
8
+ /**
9
+ * Exports the `watchRun` function that runs mocha in "watch" mode.
10
+ * @see module:lib/cli/run-helpers
11
+ * @module
12
+ * @private
13
+ */
14
+
15
+ /**
16
+ * Run Mocha in "watch" mode
17
+ * @param {Mocha} mocha - Mocha instance
18
+ * @param {Object} opts - Options
19
+ * @param {string} opts.ui - User interface
20
+ * @param {Object} fileCollectParams - Parameters that control test
21
+ * file collection. See `lib/cli/collect-files.js`.
22
+ * @param {string[]} fileCollectParams.extension - List of extensions to watch
23
+ * @private
24
+ */
25
+ module.exports = (mocha, {ui}, fileCollectParams) => {
26
+ let runner;
27
+ const files = collectFiles(fileCollectParams);
28
+
29
+ console.log();
30
+ hideCursor();
31
+ process.on('SIGINT', () => {
32
+ showCursor();
33
+ console.log('\n');
34
+ // By UNIX/Posix convention this indicates that the process was
35
+ // killed by SIGINT which has portable number 2.
36
+ process.exit(128 + 2);
37
+ });
38
+
39
+ const watchFiles = utils.files(process.cwd(), fileCollectParams.extension);
40
+ let runAgain = false;
41
+
42
+ const loadAndRun = () => {
43
+ try {
44
+ mocha.files = files;
45
+ runAgain = false;
46
+ runner = mocha.run(() => {
47
+ runner = null;
48
+ if (runAgain) {
49
+ rerun();
50
+ }
51
+ });
52
+ } catch (e) {
53
+ console.log(e.stack);
54
+ }
55
+ };
56
+
57
+ const purge = () => {
58
+ watchFiles.forEach(Mocha.unloadFile);
59
+ };
60
+
61
+ loadAndRun();
62
+
63
+ const rerun = () => {
64
+ purge();
65
+ eraseLine();
66
+ mocha.suite = mocha.suite.clone();
67
+ mocha.suite.ctx = new Context();
68
+ mocha.ui(ui);
69
+ loadAndRun();
70
+ };
71
+
72
+ utils.watch(watchFiles, () => {
73
+ runAgain = true;
74
+ if (runner) {
75
+ runner.abort();
76
+ } else {
77
+ rerun();
78
+ }
79
+ });
80
+ };
81
+
82
+ /**
83
+ * Hide the cursor.
84
+ * @ignore
85
+ * @private
86
+ */
87
+ const hideCursor = () => {
88
+ process.stdout.write('\u001b[?25l');
89
+ };
90
+
91
+ /**
92
+ * Show the cursor.
93
+ * @ignore
94
+ * @private
95
+ */
96
+ const showCursor = () => {
97
+ process.stdout.write('\u001b[?25h');
98
+ };
99
+
100
+ /**
101
+ * Erases the line on stdout
102
+ * @private
103
+ */
104
+ const eraseLine = () => {
105
+ process.stdout.write('\u001b[2K');
106
+ };
package/lib/mocha.js CHANGED
@@ -83,7 +83,7 @@ exports.Test = require('./test');
83
83
  * @param {boolean} [options.ignoreLeaks] - Ignore global leaks?
84
84
  * @param {boolean} [options.invert] - Invert test filter matches?
85
85
  * @param {boolean} [options.noHighlighting] - Disable syntax highlighting?
86
- * @param {string} [options.reporter] - Reporter name.
86
+ * @param {string|constructor} [options.reporter] - Reporter name or constructor.
87
87
  * @param {Object} [options.reporterOption] - Reporter settings object.
88
88
  * @param {number} [options.retries] - Number of times to retry failed tests.
89
89
  * @param {number} [options.slow] - Slow threshold value.
@@ -106,6 +106,10 @@ function Mocha(options) {
106
106
  options.color = 'color' in options ? options.color : options.useColors;
107
107
  }
108
108
 
109
+ // Globals are passed in as options.global, with options.globals for backward compatibility.
110
+ options.globals = options.global || options.globals || [];
111
+ delete options.global;
112
+
109
113
  this.grep(options.grep)
110
114
  .fgrep(options.fgrep)
111
115
  .ui(options.ui)
@@ -159,7 +163,7 @@ function Mocha(options) {
159
163
  * Enables or disables bailing on the first failure.
160
164
  *
161
165
  * @public
162
- * @see {@link https://mochajs.org/#-b---bail|CLI option}
166
+ * @see {@link /#-bail-b|CLI option}
163
167
  * @param {boolean} [bail=true] - Whether to bail on first error.
164
168
  * @returns {Mocha} this
165
169
  * @chainable
@@ -180,7 +184,7 @@ Mocha.prototype.bail = function(bail) {
180
184
  * Useful for generic setup code that must be included within test suite.
181
185
  *
182
186
  * @public
183
- * @see {@link https://mochajs.org/#--file-file|CLI option}
187
+ * @see {@link /#-file-filedirectoryglob|CLI option}
184
188
  * @param {string} file - Pathname of file to be loaded.
185
189
  * @returns {Mocha} this
186
190
  * @chainable
@@ -194,8 +198,8 @@ Mocha.prototype.addFile = function(file) {
194
198
  * Sets reporter to `reporter`, defaults to "spec".
195
199
  *
196
200
  * @public
197
- * @see {@link https://mochajs.org/#-r---reporter-name|CLI option}
198
- * @see {@link https://mochajs.org/#reporters|Reporters}
201
+ * @see {@link /#-reporter-name-r-name|CLI option}
202
+ * @see {@link /#reporters|Reporters}
199
203
  * @param {String|Function} reporter - Reporter name or constructor.
200
204
  * @param {Object} [reporterOptions] - Options used to configure the reporter.
201
205
  * @returns {Mocha} this
@@ -261,8 +265,8 @@ Mocha.prototype.reporter = function(reporter, reporterOptions) {
261
265
  * Sets test UI `name`, defaults to "bdd".
262
266
  *
263
267
  * @public
264
- * @see {@link https://mochajs.org/#-u---ui-name|CLI option}
265
- * @see {@link https://mochajs.org/#interfaces|Interface DSLs}
268
+ * @see {@link /#-ui-name-u-name|CLI option}
269
+ * @see {@link /#interfaces|Interface DSLs}
266
270
  * @param {string|Function} [ui=bdd] - Interface name or class.
267
271
  * @returns {Mocha} this
268
272
  * @chainable
@@ -400,7 +404,7 @@ Mocha.prototype.fgrep = function(str) {
400
404
  * <strong>Previous filter value will be overwritten on each call!</strong>
401
405
  *
402
406
  * @public
403
- * @see {@link https://mochajs.org/#-g---grep-pattern|CLI option}
407
+ * @see {@link /#grep-regexp-g-regexp|CLI option}
404
408
  * @see {@link Mocha#fgrep}
405
409
  * @see {@link Mocha#invert}
406
410
  * @param {RegExp|String} re - Regular expression used to select tests.
@@ -470,7 +474,7 @@ Mocha.prototype.ignoreLeaks = function(ignoreLeaks) {
470
474
  * Enables checking for global variables leaked while running tests.
471
475
  *
472
476
  * @public
473
- * @see {@link https://mochajs.org/#--check-leaks|CLI option}
477
+ * @see {@link /#-check-leaks|CLI option}
474
478
  * @see {@link Mocha#ignoreLeaks}
475
479
  * @return {Mocha} this
476
480
  * @chainable
@@ -540,7 +544,7 @@ Mocha.prototype._growl = growl.notify;
540
544
  * Specifies whitelist of variable names to be expected in global scope.
541
545
  *
542
546
  * @public
543
- * @see {@link https://mochajs.org/#--globals-names|CLI option}
547
+ * @see {@link /#-global-variable-name|CLI option}
544
548
  * @see {@link Mocha#checkLeaks}
545
549
  * @param {String[]|String} globals - Accepted global variable name(s).
546
550
  * @return {Mocha} this
@@ -551,9 +555,12 @@ Mocha.prototype._growl = growl.notify;
551
555
  * mocha.globals(['jQuery', 'MyLib']);
552
556
  */
553
557
  Mocha.prototype.globals = function(globals) {
554
- this.options.globals = (this.options.globals || [])
558
+ this.options.globals = this.options.globals
555
559
  .concat(globals)
556
- .filter(Boolean);
560
+ .filter(Boolean)
561
+ .filter(function(elt, idx, arr) {
562
+ return arr.indexOf(elt) === idx;
563
+ });
557
564
  return this;
558
565
  };
559
566
 
@@ -608,9 +615,8 @@ Mocha.prototype.hideDiff = function(hideDiff) {
608
615
  * If the value is `0`, timeouts will be disabled.
609
616
  *
610
617
  * @public
611
- * @see {@link https://mochajs.org/#-t---timeout-ms|CLI option}
612
- * @see {@link https://mochajs.org/#--no-timeouts|CLI option}
613
- * @see {@link https://mochajs.org/#timeouts|Timeouts}
618
+ * @see {@link /#-timeout-ms-t-ms|CLI option}
619
+ * @see {@link /#timeouts|Timeouts}
614
620
  * @see {@link Mocha#enableTimeouts}
615
621
  * @param {number|string} msecs - Timeout threshold value.
616
622
  * @return {Mocha} this
@@ -633,7 +639,7 @@ Mocha.prototype.timeout = function(msecs) {
633
639
  * Sets the number of times to retry failed tests.
634
640
  *
635
641
  * @public
636
- * @see {@link https://mochajs.org/#retry-tests|Retry Tests}
642
+ * @see {@link /#retry-tests|Retry Tests}
637
643
  * @param {number} retry - Number of times to retry failed tests.
638
644
  * @return {Mocha} this
639
645
  * @chainable
@@ -651,7 +657,7 @@ Mocha.prototype.retries = function(n) {
651
657
  * Sets slowness threshold value.
652
658
  *
653
659
  * @public
654
- * @see {@link https://mochajs.org/#-s---slow-ms|CLI option}
660
+ * @see {@link /#-slow-ms-s-ms|CLI option}
655
661
  * @param {number} msecs - Slowness threshold value.
656
662
  * @return {Mocha} this
657
663
  * @chainable
@@ -673,8 +679,7 @@ Mocha.prototype.slow = function(msecs) {
673
679
  * Enables or disables timeouts.
674
680
  *
675
681
  * @public
676
- * @see {@link https://mochajs.org/#-t---timeout-ms|CLI option}
677
- * @see {@link https://mochajs.org/#--no-timeouts|CLI option}
682
+ * @see {@link /#-timeout-ms-t-ms|CLI option}
678
683
  * @param {boolean} enableTimeouts - Whether to enable timeouts.
679
684
  * @return {Mocha} this
680
685
  * @chainable
@@ -730,7 +735,7 @@ Mocha.prototype.allowUncaught = function() {
730
735
  * Used to perform asynch operations before any suites are run.
731
736
  *
732
737
  * @public
733
- * @see {@link https://mochajs.org/#delayed-root-suite|delayed root suite}
738
+ * @see {@link /#delayed-root-suite|delayed root suite}
734
739
  * @returns {Mocha} this
735
740
  * @chainable
736
741
  */