mocha 5.1.0 → 6.0.0-1

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +653 -981
  2. package/README.md +2 -1
  3. package/{images → assets/growl}/error.png +0 -0
  4. package/{images → assets/growl}/ok.png +0 -0
  5. package/bin/_mocha +4 -595
  6. package/bin/mocha +84 -58
  7. package/bin/options.js +6 -39
  8. package/browser-entry.js +21 -17
  9. package/lib/browser/growl.js +164 -2
  10. package/lib/browser/progress.js +11 -11
  11. package/lib/{template.html → browser/template.html} +0 -0
  12. package/lib/browser/tty.js +2 -2
  13. package/lib/cli/cli.js +68 -0
  14. package/lib/cli/commands.js +13 -0
  15. package/lib/cli/config.js +79 -0
  16. package/lib/cli/index.js +9 -0
  17. package/lib/cli/init.js +37 -0
  18. package/lib/cli/node-flags.js +48 -0
  19. package/lib/cli/one-and-dones.js +70 -0
  20. package/lib/cli/options.js +299 -0
  21. package/lib/cli/run-helpers.js +328 -0
  22. package/lib/cli/run-option-metadata.js +72 -0
  23. package/lib/cli/run.js +293 -0
  24. package/lib/context.js +14 -14
  25. package/lib/errors.js +139 -0
  26. package/lib/growl.js +135 -0
  27. package/lib/hook.js +5 -16
  28. package/lib/interfaces/bdd.js +14 -13
  29. package/lib/interfaces/common.js +59 -16
  30. package/lib/interfaces/exports.js +4 -7
  31. package/lib/interfaces/qunit.js +8 -10
  32. package/lib/interfaces/tdd.js +10 -11
  33. package/lib/mocha.js +442 -255
  34. package/lib/mocharc.json +10 -0
  35. package/lib/pending.js +1 -5
  36. package/lib/reporters/base.js +92 -117
  37. package/lib/reporters/doc.js +18 -9
  38. package/lib/reporters/dot.js +13 -13
  39. package/lib/reporters/html.js +76 -47
  40. package/lib/reporters/json-stream.js +38 -23
  41. package/lib/reporters/json.js +26 -23
  42. package/lib/reporters/landing.js +9 -8
  43. package/lib/reporters/list.js +11 -10
  44. package/lib/reporters/markdown.js +13 -12
  45. package/lib/reporters/min.js +4 -3
  46. package/lib/reporters/nyan.js +36 -35
  47. package/lib/reporters/progress.js +8 -7
  48. package/lib/reporters/spec.js +14 -11
  49. package/lib/reporters/tap.js +243 -32
  50. package/lib/reporters/xunit.js +52 -33
  51. package/lib/runnable.js +103 -90
  52. package/lib/runner.js +156 -107
  53. package/lib/stats-collector.js +81 -0
  54. package/lib/suite.js +57 -51
  55. package/lib/test.js +13 -13
  56. package/lib/utils.js +192 -103
  57. package/mocha.js +3836 -2046
  58. package/package.json +122 -38
  59. package/bin/.eslintrc.yml +0 -3
  60. package/lib/browser/.eslintrc.yml +0 -4
  61. package/lib/ms.js +0 -94
  62. package/lib/reporters/base.js.orig +0 -498
  63. package/lib/reporters/json.js.orig +0 -128
@@ -0,0 +1,79 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Responsible for loading / finding Mocha's "rc" files.
5
+ * This doesn't have anything to do with `mocha.opts`.
6
+ *
7
+ * @private
8
+ * @module
9
+ */
10
+
11
+ const fs = require('fs');
12
+ const findUp = require('findup-sync');
13
+ const path = require('path');
14
+ const debug = require('debug')('mocha:cli:config');
15
+
16
+ /**
17
+ * These are the valid config files, in order of precedence;
18
+ * e.g., if `.mocharc.js` is present, then `.mocharc.yaml` and the rest
19
+ * will be ignored.
20
+ * The user should still be able to explicitly specify a file.
21
+ * @private
22
+ */
23
+ exports.CONFIG_FILES = [
24
+ '.mocharc.js',
25
+ '.mocharc.yaml',
26
+ '.mocharc.yml',
27
+ '.mocharc.json'
28
+ ];
29
+
30
+ /**
31
+ * Parsers for various config filetypes. Each accepts a filepath and
32
+ * returns an object (but could throw)
33
+ */
34
+ const parsers = (exports.parsers = {
35
+ yaml: filepath =>
36
+ require('js-yaml').safeLoad(fs.readFileSync(filepath, 'utf8')),
37
+ js: filepath => require(filepath),
38
+ json: filepath =>
39
+ JSON.parse(
40
+ require('strip-json-comments')(fs.readFileSync(filepath, 'utf8'))
41
+ )
42
+ });
43
+
44
+ /**
45
+ * Loads and parses, based on file extension, a config file.
46
+ * "JSON" files may have comments.
47
+ * @param {string} filepath - Config file path to load
48
+ * @returns {Object} Parsed config object
49
+ * @private
50
+ */
51
+ exports.loadConfig = filepath => {
52
+ let config = {};
53
+ const ext = path.extname(filepath);
54
+ try {
55
+ if (/\.ya?ml/.test(ext)) {
56
+ config = parsers.yaml(filepath);
57
+ } else if (ext === '.js') {
58
+ config = parsers.js(filepath);
59
+ } else {
60
+ config = parsers.json(filepath);
61
+ }
62
+ } catch (err) {
63
+ throw new Error(`failed to parse ${filepath}: ${err}`);
64
+ }
65
+ return config;
66
+ };
67
+
68
+ /**
69
+ * Find ("find up") config file starting at `cwd`
70
+ * @param {string} [cwd] - Current working directory
71
+ * @returns {string|null} Filepath to config, if found
72
+ */
73
+ exports.findConfig = (cwd = process.cwd()) => {
74
+ const filepath = findUp(exports.CONFIG_FILES, {cwd});
75
+ if (filepath) {
76
+ debug(`found config at ${filepath}`);
77
+ }
78
+ return filepath;
79
+ };
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Just exports {@link module:lib/cli/cli} for convenience.
5
+ * @private
6
+ * @module lib/cli
7
+ * @exports module:lib/cli/cli
8
+ */
9
+ module.exports = require('./cli');
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Command module for "init" command
5
+ *
6
+ * @private
7
+ * @module
8
+ */
9
+
10
+ const fs = require('fs');
11
+ const path = require('path');
12
+ const mkdirp = require('mkdirp');
13
+
14
+ exports.command = 'init <path>';
15
+
16
+ exports.description = 'create a client-side Mocha setup at <path>';
17
+
18
+ exports.builder = yargs =>
19
+ yargs.positional('path', {
20
+ type: 'string',
21
+ normalize: true
22
+ });
23
+
24
+ exports.handler = argv => {
25
+ const destdir = argv.path;
26
+ const srcdir = path.join(__dirname, '..', '..');
27
+ mkdirp.sync(destdir);
28
+ const css = fs.readFileSync(path.join(srcdir, 'mocha.css'));
29
+ const js = fs.readFileSync(path.join(srcdir, 'mocha.js'));
30
+ const tmpl = fs.readFileSync(
31
+ path.join(srcdir, 'lib', 'browser', 'template.html')
32
+ );
33
+ fs.writeFileSync(path.join(destdir, 'mocha.css'), css);
34
+ fs.writeFileSync(path.join(destdir, 'mocha.js'), js);
35
+ fs.writeFileSync(path.join(destdir, 'tests.spec.js'), '');
36
+ fs.writeFileSync(path.join(destdir, 'index.html'), tmpl);
37
+ };
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Some settings and code related to Mocha's handling of Node.js/V8 flags.
5
+ * @private
6
+ * @module
7
+ */
8
+
9
+ const nodeFlags = require('node-environment-flags');
10
+
11
+ /**
12
+ * These flags are considered "debug" flags.
13
+ * @see {@link impliesNoTimeouts}
14
+ * @private
15
+ */
16
+ const debugFlags = new Set(['debug', 'debug-brk', 'inspect', 'inspect-brk']);
17
+
18
+ /**
19
+ * Mocha has historical support for various `node` and V8 flags which might not
20
+ * appear in `process.allowedNodeEnvironmentFlags`.
21
+ * These include:
22
+ * - `--preserve-symlinks`
23
+ * - `--harmony-*`
24
+ * - `--gc-global`
25
+ * - `--trace-*`
26
+ * - `--es-staging`
27
+ * - `--use-strict`
28
+ * - `--v8-*` (but *not* `--v8-options`)
29
+ * @summary Whether or not to pass a flag along to the `node` executable.
30
+ * @param {string} flag - Flag to test
31
+ * @returns {boolean}
32
+ * @private
33
+ */
34
+ exports.isNodeFlag = flag =>
35
+ !/^(?:require|r)$/.test(flag) &&
36
+ (nodeFlags.has(flag) ||
37
+ /(?:preserve-symlinks(?:-main)?|harmony(?:[_-]|$)|(?:trace[_-].+$)|gc(?:[_-]global)?$|es[_-]staging$|use[_-]strict$|v8[_-](?!options).+?$)/.test(
38
+ flag
39
+ ));
40
+
41
+ /**
42
+ * Returns `true` if the flag is a "debug-like" flag. These require timeouts
43
+ * to be suppressed, or pausing the debugger on breakpoints will cause test failures.
44
+ * @param {string} flag - Flag to test
45
+ * @returns {boolean}
46
+ * @private
47
+ */
48
+ exports.impliesNoTimeouts = flag => debugFlags.has(flag);
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Contains "command" code for "one-and-dones"--options passed
5
+ * to Mocha which cause it to just dump some info and exit.
6
+ * See {@link module:lib/cli/one-and-dones.ONE_AND_DONE_ARGS ONE_AND_DONE_ARGS} for more info.
7
+ * @module
8
+ * @private
9
+ */
10
+
11
+ const align = require('wide-align');
12
+ const Mocha = require('../mocha');
13
+
14
+ /**
15
+ * Dumps a sorted list of the enumerable, lower-case keys of some object
16
+ * to `STDOUT`.
17
+ * @param {Object} obj - Object, ostensibly having some enumerable keys
18
+ * @ignore
19
+ * @private
20
+ */
21
+ const showKeys = obj => {
22
+ console.log();
23
+ const keys = Object.keys(obj);
24
+ const maxKeyLength = keys.reduce((max, key) => Math.max(max, key.length), 0);
25
+ keys
26
+ .filter(
27
+ key => /^[a-z]/.test(key) && !obj[key].browserOnly && !obj[key].abstract
28
+ )
29
+ .sort()
30
+ .forEach(key => {
31
+ const description = obj[key].description;
32
+ console.log(
33
+ ` ${align.left(key, maxKeyLength + 1)}${
34
+ description ? `- ${description}` : ''
35
+ }`
36
+ );
37
+ });
38
+ console.log();
39
+ };
40
+
41
+ /**
42
+ * Handlers for one-and-done options
43
+ * @namespace
44
+ * @private
45
+ */
46
+ exports.ONE_AND_DONES = {
47
+ /**
48
+ * Dump list of built-in interfaces
49
+ * @private
50
+ */
51
+ interfaces: () => {
52
+ showKeys(Mocha.interfaces);
53
+ },
54
+ /**
55
+ * Dump list of built-in reporters
56
+ * @private
57
+ */
58
+ reporters: () => {
59
+ showKeys(Mocha.reporters);
60
+ }
61
+ };
62
+
63
+ /**
64
+ * A Set of all one-and-done options
65
+ * @type Set<string>
66
+ * @private
67
+ */
68
+ exports.ONE_AND_DONE_ARGS = new Set(
69
+ ['help', 'h', 'version', 'V'].concat(Object.keys(exports.ONE_AND_DONES))
70
+ );
@@ -0,0 +1,299 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Main entry point for handling filesystem-based configuration,
5
+ * whether that's `mocha.opts` or a config file or `package.json` or whatever.
6
+ * @module
7
+ */
8
+
9
+ const fs = require('fs');
10
+ const yargsParser = require('yargs-parser');
11
+ const {types, aliases} = require('./run-option-metadata');
12
+ const {ONE_AND_DONE_ARGS} = require('./one-and-dones');
13
+ const mocharc = require('../mocharc.json');
14
+ const yargsParserConfig = require('../../package.json').yargs;
15
+ const {list} = require('./run-helpers');
16
+ const {loadConfig, findConfig} = require('./config');
17
+ const findup = require('findup-sync');
18
+ const {deprecate} = require('../utils');
19
+ const debug = require('debug')('mocha:cli:options');
20
+
21
+ /**
22
+ * The `yargs-parser` namespace
23
+ * @external yargsParser
24
+ * @see {@link https://npm.im/yargs-parser}
25
+ */
26
+
27
+ /**
28
+ * An object returned by a configured `yargs-parser` representing arguments
29
+ * @memberof external:yargsParser
30
+ * @interface Arguments
31
+ */
32
+
33
+ /**
34
+ * This is the config pulled from the `yargs` property of Mocha's
35
+ * `package.json`, but it also disables camel case expansion as to
36
+ * avoid outputting non-canonical keynames, as we need to do some
37
+ * lookups.
38
+ * @private
39
+ * @ignore
40
+ */
41
+ const configuration = Object.assign({}, yargsParserConfig, {
42
+ 'camel-case-expansion': false
43
+ });
44
+
45
+ /**
46
+ * This is a really fancy way to ensure unique values for `array`-type
47
+ * options.
48
+ * This is passed as the `coerce` option to `yargs-parser`
49
+ * @private
50
+ * @ignore
51
+ */
52
+ const coerceOpts = types.array.reduce(
53
+ (acc, arg) => Object.assign(acc, {[arg]: v => Array.from(new Set(list(v)))}),
54
+ {}
55
+ );
56
+
57
+ /**
58
+ * We do not have a case when multiple arguments are ever allowed after a flag
59
+ * (e.g., `--foo bar baz quux`), so we fix the number of arguments to 1 across
60
+ * the board of non-boolean options.
61
+ * This is passed as the `narg` option to `yargs-parser`
62
+ * @private
63
+ * @ignore
64
+ */
65
+ const nargOpts = types.array
66
+ .concat(types.string, types.number)
67
+ .reduce((acc, arg) => Object.assign(acc, {[arg]: 1}), {});
68
+
69
+ /**
70
+ * Wrapper around `yargs-parser` which applies our settings
71
+ * @param {string|string[]} args - Arguments to parse
72
+ * @param {...Object} configObjects - `configObjects` for yargs-parser
73
+ * @private
74
+ * @ignore
75
+ */
76
+ const parse = (args = [], ...configObjects) =>
77
+ yargsParser(
78
+ args,
79
+ Object.assign(
80
+ {
81
+ configuration,
82
+ configObjects,
83
+ coerce: coerceOpts,
84
+ narg: nargOpts,
85
+ alias: aliases
86
+ },
87
+ types
88
+ )
89
+ );
90
+
91
+ /**
92
+ * - Replaces comments with empty strings
93
+ * - Replaces escaped spaces (e.g., 'xxx\ yyy') with HTML space
94
+ * - Splits on whitespace, creating array of substrings
95
+ * - Filters empty string elements from array
96
+ * - Replaces any HTML space with space
97
+ * @summary Parses options read from run-control file.
98
+ * @private
99
+ * @param {string} content - Content read from run-control file.
100
+ * @returns {string[]} cmdline options (and associated arguments)
101
+ * @ignore
102
+ */
103
+ const parseMochaOpts = content =>
104
+ content
105
+ .replace(/^#.*$/gm, '')
106
+ .replace(/\\\s/g, '%20')
107
+ .split(/\s/)
108
+ .filter(Boolean)
109
+ .map(value => value.replace(/%20/g, ' '));
110
+
111
+ /**
112
+ * Prepends options from run-control file to the command line arguments.
113
+ *
114
+ * @deprecated Deprecated in v6.0.0; This function is no longer used internally and will be removed in a future version.
115
+ * @public
116
+ * @alias module:lib/cli/options
117
+ * @see {@link https://mochajs.org/#mochaopts|mocha.opts}
118
+ */
119
+ module.exports = function getOptions() {
120
+ deprecate(
121
+ 'getOptions() is DEPRECATED and will be removed from a future version of Mocha. Use loadOptions() instead'
122
+ );
123
+ if (process.argv.length === 3 && ONE_AND_DONE_ARGS.has(process.argv[2])) {
124
+ return;
125
+ }
126
+
127
+ const optsPath =
128
+ process.argv.indexOf('--opts') === -1
129
+ ? mocharc.opts
130
+ : process.argv[process.argv.indexOf('--opts') + 1];
131
+
132
+ try {
133
+ const options = parseMochaOpts(fs.readFileSync(optsPath, 'utf8'));
134
+
135
+ process.argv = process.argv
136
+ .slice(0, 2)
137
+ .concat(options.concat(process.argv.slice(2)));
138
+ } catch (ignore) {
139
+ // NOTE: should console.error() and throw the error
140
+ }
141
+
142
+ process.env.LOADED_MOCHA_OPTS = true;
143
+ };
144
+
145
+ /**
146
+ * Given filepath in `args.opts`, attempt to load and parse a `mocha.opts` file.
147
+ * @param {Object} [args] - Arguments object
148
+ * @param {string|boolean} [args.opts] - Filepath to mocha.opts; defaults to whatever's in `mocharc.opts`, or `false` to skip
149
+ * @returns {external:yargsParser.Arguments|void} If read, object containing parsed arguments
150
+ * @memberof module:lib/cli/options
151
+ * @public
152
+ */
153
+ const loadMochaOpts = (args = {}) => {
154
+ let result;
155
+ let filepath = args.opts;
156
+ // /dev/null is backwards compat
157
+ if (filepath === false || filepath === '/dev/null') {
158
+ return result;
159
+ }
160
+ filepath = filepath || mocharc.opts;
161
+ result = {};
162
+ let mochaOpts;
163
+ try {
164
+ mochaOpts = fs.readFileSync(filepath, 'utf8');
165
+ debug(`read ${filepath}`);
166
+ } catch (err) {
167
+ if (args.opts) {
168
+ throw new Error(`Unable to read ${filepath}: ${err}`);
169
+ }
170
+ // ignore otherwise. we tried
171
+ debug(`No mocha.opts found at ${filepath}`);
172
+ }
173
+
174
+ // real args should override `mocha.opts` which should override defaults.
175
+ // if there's an exception to catch here, I'm not sure what it is.
176
+ // by attaching the `no-opts` arg, we avoid re-parsing of `mocha.opts`.
177
+ if (mochaOpts) {
178
+ result = parse(parseMochaOpts(mochaOpts));
179
+ debug(`${filepath} parsed succesfully`);
180
+ }
181
+ return result;
182
+ };
183
+
184
+ module.exports.loadMochaOpts = loadMochaOpts;
185
+
186
+ /**
187
+ * Given path to config file in `args.config`, attempt to load & parse config file.
188
+ * @param {Object} [args] - Arguments object
189
+ * @param {string|boolean} [args.config] - Path to config file or `false` to skip
190
+ * @public
191
+ * @memberof module:lib/cli/options
192
+ * @returns {external:yargsParser.Arguments|void} Parsed config, or nothing if `args.config` is `false`
193
+ */
194
+ const loadRc = (args = {}) => {
195
+ if (args.config !== false) {
196
+ const config = args.config || findConfig();
197
+ return config ? loadConfig(config) : {};
198
+ }
199
+ };
200
+
201
+ module.exports.loadRc = loadRc;
202
+
203
+ /**
204
+ * Given path to `package.json` in `args.package`, attempt to load config from `mocha` prop.
205
+ * @param {Object} [args] - Arguments object
206
+ * @param {string|boolean} [args.config] - Path to `package.json` or `false` to skip
207
+ * @public
208
+ * @memberof module:lib/cli/options
209
+ * @returns {external:yargsParser.Arguments|void} Parsed config, or nothing if `args.package` is `false`
210
+ */
211
+ const loadPkgRc = (args = {}) => {
212
+ let result;
213
+ if (args.package === false) {
214
+ return result;
215
+ }
216
+ result = {};
217
+ const filepath = args.package || findup(mocharc.package);
218
+ if (filepath) {
219
+ try {
220
+ const pkg = JSON.parse(fs.readFileSync(filepath, 'utf8'));
221
+ if (pkg.mocha) {
222
+ debug(`'mocha' prop of package.json parsed:`, pkg.mocha);
223
+ result = pkg.mocha;
224
+ } else {
225
+ debug(`no config found in ${filepath}`);
226
+ }
227
+ } catch (err) {
228
+ if (args.package) {
229
+ throw new Error(`Unable to read/parse ${filepath}: ${err}`);
230
+ }
231
+ debug(`failed to read default package.json at ${filepath}; ignoring`);
232
+ }
233
+ }
234
+ return result;
235
+ };
236
+
237
+ module.exports.loadPkgRc = loadPkgRc;
238
+
239
+ /**
240
+ * Priority list:
241
+ *
242
+ * 1. Command-line args
243
+ * 2. RC file (`.mocharc.js`, `.mocharc.ya?ml`, `mocharc.json`)
244
+ * 3. `mocha` prop of `package.json`
245
+ * 4. `mocha.opts`
246
+ * 5. default configuration (`lib/mocharc.json`)
247
+ *
248
+ * If a {@link module:lib/cli/one-and-dones.ONE_AND_DONE_ARGS "one-and-done" option} is present in the `argv` array, no external config files will be read.
249
+ * @summary Parses options read from `mocha.opts`, `.mocharc.*` and `package.json`.
250
+ * @param {string|string[]} [argv] - Arguments to parse
251
+ * @public
252
+ * @memberof module:lib/cli/options
253
+ * @returns {external:yargsParser.Arguments} Parsed args from everything
254
+ */
255
+ const loadOptions = (argv = []) => {
256
+ let args = parse(argv);
257
+ // short-circuit: look for a flag that would abort loading of mocha.opts
258
+ if (
259
+ Array.from(ONE_AND_DONE_ARGS).reduce(
260
+ (acc, arg) => acc || arg in args,
261
+ false
262
+ )
263
+ ) {
264
+ return args;
265
+ }
266
+
267
+ const rcConfig = loadRc(args);
268
+ const pkgConfig = loadPkgRc(args);
269
+ const optsConfig = loadMochaOpts(args);
270
+
271
+ if (rcConfig) {
272
+ args.config = false;
273
+ }
274
+ if (pkgConfig) {
275
+ args.package = false;
276
+ }
277
+ if (optsConfig) {
278
+ args.opts = false;
279
+ }
280
+
281
+ args = parse(
282
+ args._,
283
+ args,
284
+ rcConfig || {},
285
+ pkgConfig || {},
286
+ optsConfig || {},
287
+ mocharc
288
+ );
289
+
290
+ // recombine positional arguments and "spec"
291
+ if (args.spec) {
292
+ args._ = args._.concat(args.spec);
293
+ delete args.spec;
294
+ }
295
+
296
+ return args;
297
+ };
298
+
299
+ module.exports.loadOptions = loadOptions;