mocha-compat 3.6.4 → 10.8.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.
Files changed (92) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +61 -110
  3. package/bin/_mocha +10 -0
  4. package/bin/mocha.js +142 -0
  5. package/browser-entry.js +61 -22
  6. package/lib/browser/highlight-tags.js +39 -0
  7. package/lib/browser/parse-query.js +24 -0
  8. package/lib/{template.html → browser/template.html} +7 -5
  9. package/lib/cli/cli.js +89 -0
  10. package/lib/cli/collect-files.js +137 -0
  11. package/lib/cli/commands.js +14 -0
  12. package/lib/cli/config.js +100 -0
  13. package/lib/cli/index.js +3 -0
  14. package/lib/cli/init.js +36 -0
  15. package/lib/cli/lookup-files.js +150 -0
  16. package/lib/cli/node-flags.js +85 -0
  17. package/lib/cli/one-and-dones.js +69 -0
  18. package/lib/cli/options.js +284 -0
  19. package/lib/cli/run-helpers.js +304 -0
  20. package/lib/cli/run-option-metadata.js +116 -0
  21. package/lib/cli/run.js +380 -0
  22. package/lib/cli/watch-run.js +377 -0
  23. package/lib/context.js +16 -42
  24. package/lib/errors.js +563 -0
  25. package/lib/hook.js +50 -9
  26. package/lib/interfaces/bdd.js +28 -29
  27. package/lib/interfaces/common.js +56 -21
  28. package/lib/interfaces/exports.js +4 -7
  29. package/lib/interfaces/qunit.js +10 -11
  30. package/lib/interfaces/tdd.js +15 -15
  31. package/lib/mocha.js +1073 -292
  32. package/lib/mocharc.json +10 -0
  33. package/lib/nodejs/buffered-worker-pool.js +188 -0
  34. package/lib/nodejs/esm-utils.js +106 -0
  35. package/lib/nodejs/file-unloader.js +15 -0
  36. package/lib/nodejs/parallel-buffered-runner.js +433 -0
  37. package/lib/nodejs/reporters/parallel-buffered.js +165 -0
  38. package/lib/nodejs/serializer.js +414 -0
  39. package/lib/nodejs/worker.js +151 -0
  40. package/lib/pending.js +3 -3
  41. package/lib/plugin-loader.js +286 -0
  42. package/lib/reporters/base.js +305 -205
  43. package/lib/reporters/doc.js +52 -21
  44. package/lib/reporters/dot.js +31 -18
  45. package/lib/reporters/html.js +153 -81
  46. package/lib/reporters/json-stream.js +53 -24
  47. package/lib/reporters/json.js +88 -18
  48. package/lib/reporters/landing.js +38 -16
  49. package/lib/reporters/list.js +34 -19
  50. package/lib/reporters/markdown.js +28 -15
  51. package/lib/reporters/min.js +22 -8
  52. package/lib/reporters/nyan.js +62 -58
  53. package/lib/reporters/progress.js +32 -19
  54. package/lib/reporters/spec.js +41 -23
  55. package/lib/reporters/tap.js +255 -32
  56. package/lib/reporters/xunit.js +89 -39
  57. package/lib/runnable.js +233 -148
  58. package/lib/runner.js +679 -380
  59. package/lib/stats-collector.js +83 -0
  60. package/lib/suite.js +376 -112
  61. package/lib/test.js +82 -21
  62. package/lib/utils.js +364 -477
  63. package/mocha.css +183 -54
  64. package/mocha.js +19840 -15846
  65. package/mocha.js.map +1 -0
  66. package/package.json +163 -336
  67. package/{lib/to-iso-string → vendor/serialize-javascript}/LICENSE +8 -6
  68. package/vendor/serialize-javascript/README.md +10 -0
  69. package/vendor/serialize-javascript/index.js +349 -0
  70. package/bin/_mocha-compat +0 -572
  71. package/bin/mocha-compat +0 -87
  72. package/bin/options.js +0 -41
  73. package/bower.json +0 -38
  74. package/images/error.png +0 -0
  75. package/images/ok.png +0 -0
  76. package/lib/browser/.eslintrc.yaml +0 -4
  77. package/lib/browser/debug.js +0 -7
  78. package/lib/browser/events.js +0 -195
  79. package/lib/browser/progress.js +0 -119
  80. package/lib/browser/tty.js +0 -13
  81. package/lib/ms.js +0 -130
  82. package/lib/to-iso-string/index.js +0 -37
  83. package/vendor/glob/LICENSE +0 -15
  84. package/vendor/glob/README.md +0 -399
  85. package/vendor/glob/common.js +0 -244
  86. package/vendor/glob/glob.js +0 -788
  87. package/vendor/glob/package.json +0 -55
  88. package/vendor/glob/sync.js +0 -486
  89. package/vendor/inflight/LICENSE +0 -15
  90. package/vendor/inflight/README.md +0 -37
  91. package/vendor/inflight/inflight.js +0 -54
  92. package/vendor/inflight/package.json +0 -29
@@ -0,0 +1,377 @@
1
+ 'use strict';
2
+
3
+ const logSymbols = require('log-symbols');
4
+ const debug = require('debug')('mocha:cli:watch');
5
+ const path = require('path');
6
+ const chokidar = require('chokidar');
7
+ const Context = require('../context');
8
+ const collectFiles = require('./collect-files');
9
+
10
+ /**
11
+ * Exports the `watchRun` function that runs mocha in "watch" mode.
12
+ * @see module:lib/cli/run-helpers
13
+ * @module
14
+ * @private
15
+ */
16
+
17
+ /**
18
+ * Run Mocha in parallel "watch" mode
19
+ * @param {Mocha} mocha - Mocha instance
20
+ * @param {Object} opts - Options
21
+ * @param {string[]} [opts.watchFiles] - List of paths and patterns to
22
+ * watch. If not provided all files with an extension included in
23
+ * `fileCollectionParams.extension` are watched. See first argument of
24
+ * `chokidar.watch`.
25
+ * @param {string[]} opts.watchIgnore - List of paths and patterns to
26
+ * exclude from watching. See `ignored` option of `chokidar`.
27
+ * @param {FileCollectionOptions} fileCollectParams - Parameters that control test
28
+ * @private
29
+ */
30
+ exports.watchParallelRun = (
31
+ mocha,
32
+ {watchFiles, watchIgnore},
33
+ fileCollectParams
34
+ ) => {
35
+ debug('creating parallel watcher');
36
+
37
+ return createWatcher(mocha, {
38
+ watchFiles,
39
+ watchIgnore,
40
+ beforeRun({mocha}) {
41
+ // I don't know why we're cloning the root suite.
42
+ const rootSuite = mocha.suite.clone();
43
+
44
+ // ensure we aren't leaking event listeners
45
+ mocha.dispose();
46
+
47
+ // this `require` is needed because the require cache has been cleared. the dynamic
48
+ // exports set via the below call to `mocha.ui()` won't work properly if a
49
+ // test depends on this module.
50
+ const Mocha = require('../mocha');
51
+
52
+ // ... and now that we've gotten a new module, we need to use it again due
53
+ // to `mocha.ui()` call
54
+ const newMocha = new Mocha(mocha.options);
55
+ // don't know why this is needed
56
+ newMocha.suite = rootSuite;
57
+ // nor this
58
+ newMocha.suite.ctx = new Context();
59
+
60
+ // reset the list of files
61
+ newMocha.files = collectFiles(fileCollectParams).files;
62
+
63
+ // because we've swapped out the root suite (see the `run` inner function
64
+ // in `createRerunner`), we need to call `mocha.ui()` again to set up the context/globals.
65
+ newMocha.ui(newMocha.options.ui);
66
+
67
+ // we need to call `newMocha.rootHooks` to set up rootHooks for the new
68
+ // suite
69
+ newMocha.rootHooks(newMocha.options.rootHooks);
70
+
71
+ // in parallel mode, the main Mocha process doesn't actually load the
72
+ // files. this flag prevents `mocha.run()` from autoloading.
73
+ newMocha.lazyLoadFiles(true);
74
+ return newMocha;
75
+ },
76
+ fileCollectParams
77
+ });
78
+ };
79
+
80
+ /**
81
+ * Run Mocha in "watch" mode
82
+ * @param {Mocha} mocha - Mocha instance
83
+ * @param {Object} opts - Options
84
+ * @param {string[]} [opts.watchFiles] - List of paths and patterns to
85
+ * watch. If not provided all files with an extension included in
86
+ * `fileCollectionParams.extension` are watched. See first argument of
87
+ * `chokidar.watch`.
88
+ * @param {string[]} opts.watchIgnore - List of paths and patterns to
89
+ * exclude from watching. See `ignored` option of `chokidar`.
90
+ * @param {FileCollectionOptions} fileCollectParams - Parameters that control test
91
+ * file collection. See `lib/cli/collect-files.js`.
92
+ * @private
93
+ */
94
+ exports.watchRun = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => {
95
+ debug('creating serial watcher');
96
+
97
+ return createWatcher(mocha, {
98
+ watchFiles,
99
+ watchIgnore,
100
+ beforeRun({mocha}) {
101
+ mocha.unloadFiles();
102
+
103
+ // I don't know why we're cloning the root suite.
104
+ const rootSuite = mocha.suite.clone();
105
+
106
+ // ensure we aren't leaking event listeners
107
+ mocha.dispose();
108
+
109
+ // this `require` is needed because the require cache has been cleared. the dynamic
110
+ // exports set via the below call to `mocha.ui()` won't work properly if a
111
+ // test depends on this module.
112
+ const Mocha = require('../mocha');
113
+
114
+ // ... and now that we've gotten a new module, we need to use it again due
115
+ // to `mocha.ui()` call
116
+ const newMocha = new Mocha(mocha.options);
117
+ // don't know why this is needed
118
+ newMocha.suite = rootSuite;
119
+ // nor this
120
+ newMocha.suite.ctx = new Context();
121
+
122
+ // reset the list of files
123
+ newMocha.files = collectFiles(fileCollectParams).files;
124
+
125
+ // because we've swapped out the root suite (see the `run` inner function
126
+ // in `createRerunner`), we need to call `mocha.ui()` again to set up the context/globals.
127
+ newMocha.ui(newMocha.options.ui);
128
+
129
+ // we need to call `newMocha.rootHooks` to set up rootHooks for the new
130
+ // suite
131
+ newMocha.rootHooks(newMocha.options.rootHooks);
132
+
133
+ return newMocha;
134
+ },
135
+ fileCollectParams
136
+ });
137
+ };
138
+
139
+ /**
140
+ * Bootstraps a chokidar watcher. Handles keyboard input & signals
141
+ * @param {Mocha} mocha - Mocha instance
142
+ * @param {Object} opts
143
+ * @param {BeforeWatchRun} [opts.beforeRun] - Function to call before
144
+ * `mocha.run()`
145
+ * @param {string[]} [opts.watchFiles] - List of paths and patterns to watch. If
146
+ * not provided all files with an extension included in
147
+ * `fileCollectionParams.extension` are watched. See first argument of
148
+ * `chokidar.watch`.
149
+ * @param {string[]} [opts.watchIgnore] - List of paths and patterns to exclude
150
+ * from watching. See `ignored` option of `chokidar`.
151
+ * @param {FileCollectionOptions} opts.fileCollectParams - List of extensions to watch if `opts.watchFiles` is not given.
152
+ * @returns {FSWatcher}
153
+ * @ignore
154
+ * @private
155
+ */
156
+ const createWatcher = (
157
+ mocha,
158
+ {watchFiles, watchIgnore, beforeRun, fileCollectParams}
159
+ ) => {
160
+ if (!watchFiles) {
161
+ watchFiles = fileCollectParams.extension.map(ext => `**/*.${ext}`);
162
+ }
163
+
164
+ debug('ignoring files matching: %s', watchIgnore);
165
+ let globalFixtureContext;
166
+
167
+ // we handle global fixtures manually
168
+ mocha.enableGlobalSetup(false).enableGlobalTeardown(false);
169
+
170
+ const watcher = chokidar.watch(watchFiles, {
171
+ ignored: watchIgnore,
172
+ ignoreInitial: true
173
+ });
174
+
175
+ const rerunner = createRerunner(mocha, watcher, {
176
+ beforeRun
177
+ });
178
+
179
+ watcher.on('ready', async () => {
180
+ if (!globalFixtureContext) {
181
+ debug('triggering global setup');
182
+ globalFixtureContext = await mocha.runGlobalSetup();
183
+ }
184
+ rerunner.run();
185
+ });
186
+
187
+ watcher.on('all', () => {
188
+ rerunner.scheduleRun();
189
+ });
190
+
191
+ hideCursor();
192
+ process.on('exit', () => {
193
+ showCursor();
194
+ });
195
+
196
+ // this is for testing.
197
+ // win32 cannot gracefully shutdown via a signal from a parent
198
+ // process; a `SIGINT` from a parent will cause the process
199
+ // to immediately exit. during normal course of operation, a user
200
+ // will type Ctrl-C and the listener will be invoked, but this
201
+ // is not possible in automated testing.
202
+ // there may be another way to solve this, but it too will be a hack.
203
+ // for our watch tests on win32 we must _fork_ mocha with an IPC channel
204
+ if (process.connected) {
205
+ process.on('message', msg => {
206
+ if (msg === 'SIGINT') {
207
+ process.emit('SIGINT');
208
+ }
209
+ });
210
+ }
211
+
212
+ let exiting = false;
213
+ process.on('SIGINT', async () => {
214
+ showCursor();
215
+ console.error(`${logSymbols.warning} [mocha] cleaning up, please wait...`);
216
+ if (!exiting) {
217
+ exiting = true;
218
+ if (mocha.hasGlobalTeardownFixtures()) {
219
+ debug('running global teardown');
220
+ try {
221
+ await mocha.runGlobalTeardown(globalFixtureContext);
222
+ } catch (err) {
223
+ console.error(err);
224
+ }
225
+ }
226
+ process.exit(130);
227
+ }
228
+ });
229
+
230
+ // Keyboard shortcut for restarting when "rs\n" is typed (ala Nodemon)
231
+ process.stdin.resume();
232
+ process.stdin.setEncoding('utf8');
233
+ process.stdin.on('data', data => {
234
+ const str = data.toString().trim().toLowerCase();
235
+ if (str === 'rs') rerunner.scheduleRun();
236
+ });
237
+
238
+ return watcher;
239
+ };
240
+
241
+ /**
242
+ * Create an object that allows you to rerun tests on the mocha instance.
243
+ *
244
+ * @param {Mocha} mocha - Mocha instance
245
+ * @param {FSWatcher} watcher - chokidar `FSWatcher` instance
246
+ * @param {Object} [opts] - Options!
247
+ * @param {BeforeWatchRun} [opts.beforeRun] - Function to call before `mocha.run()`
248
+ * @returns {Rerunner}
249
+ * @ignore
250
+ * @private
251
+ */
252
+ const createRerunner = (mocha, watcher, {beforeRun} = {}) => {
253
+ // Set to a `Runner` when mocha is running. Set to `null` when mocha is not
254
+ // running.
255
+ let runner = null;
256
+
257
+ // true if a file has changed during a test run
258
+ let rerunScheduled = false;
259
+
260
+ const run = () => {
261
+ try {
262
+ mocha = beforeRun ? beforeRun({mocha, watcher}) || mocha : mocha;
263
+ runner = mocha.run(() => {
264
+ debug('finished watch run');
265
+ runner = null;
266
+ blastCache(watcher);
267
+ if (rerunScheduled) {
268
+ rerun();
269
+ } else {
270
+ console.error(`${logSymbols.info} [mocha] waiting for changes...`);
271
+ }
272
+ });
273
+ } catch (e) {
274
+ console.error(e.stack);
275
+ }
276
+ };
277
+
278
+ const scheduleRun = () => {
279
+ if (rerunScheduled) {
280
+ return;
281
+ }
282
+
283
+ rerunScheduled = true;
284
+ if (runner) {
285
+ runner.abort();
286
+ } else {
287
+ rerun();
288
+ }
289
+ };
290
+
291
+ const rerun = () => {
292
+ rerunScheduled = false;
293
+ eraseLine();
294
+ run();
295
+ };
296
+
297
+ return {
298
+ scheduleRun,
299
+ run
300
+ };
301
+ };
302
+
303
+ /**
304
+ * Return the list of absolute paths watched by a chokidar watcher.
305
+ *
306
+ * @param watcher - Instance of a chokidar watcher
307
+ * @return {string[]} - List of absolute paths
308
+ * @ignore
309
+ * @private
310
+ */
311
+ const getWatchedFiles = watcher => {
312
+ const watchedDirs = watcher.getWatched();
313
+ return Object.keys(watchedDirs).reduce(
314
+ (acc, dir) => [
315
+ ...acc,
316
+ ...watchedDirs[dir].map(file => path.join(dir, file))
317
+ ],
318
+ []
319
+ );
320
+ };
321
+
322
+ /**
323
+ * Hide the cursor.
324
+ * @ignore
325
+ * @private
326
+ */
327
+ const hideCursor = () => {
328
+ process.stdout.write('\u001b[?25l');
329
+ };
330
+
331
+ /**
332
+ * Show the cursor.
333
+ * @ignore
334
+ * @private
335
+ */
336
+ const showCursor = () => {
337
+ process.stdout.write('\u001b[?25h');
338
+ };
339
+
340
+ /**
341
+ * Erases the line on stdout
342
+ * @private
343
+ */
344
+ const eraseLine = () => {
345
+ process.stdout.write('\u001b[2K');
346
+ };
347
+
348
+ /**
349
+ * Blast all of the watched files out of `require.cache`
350
+ * @param {FSWatcher} watcher - chokidar FSWatcher
351
+ * @ignore
352
+ * @private
353
+ */
354
+ const blastCache = watcher => {
355
+ const files = getWatchedFiles(watcher);
356
+ files.forEach(file => {
357
+ delete require.cache[file];
358
+ });
359
+ debug('deleted %d file(s) from the require cache', files.length);
360
+ };
361
+
362
+ /**
363
+ * Callback to be run before `mocha.run()` is called.
364
+ * Optionally, it can return a new `Mocha` instance.
365
+ * @callback BeforeWatchRun
366
+ * @private
367
+ * @param {{mocha: Mocha, watcher: FSWatcher}} options
368
+ * @returns {Mocha}
369
+ */
370
+
371
+ /**
372
+ * Object containing run control methods
373
+ * @typedef {Object} Rerunner
374
+ * @private
375
+ * @property {Function} run - Calls `mocha.run()`
376
+ * @property {Function} scheduleRun - Schedules another call to `run`
377
+ */
package/lib/context.js CHANGED
@@ -1,11 +1,7 @@
1
1
  'use strict';
2
-
3
2
  /**
4
- * Module dependencies.
3
+ * @module Context
5
4
  */
6
-
7
- var JSON = require('json3');
8
-
9
5
  /**
10
6
  * Expose `Context`.
11
7
  */
@@ -15,16 +11,16 @@ module.exports = Context;
15
11
  /**
16
12
  * Initialize a new `Context`.
17
13
  *
18
- * @api private
14
+ * @private
19
15
  */
20
- function Context () {}
16
+ function Context() {}
21
17
 
22
18
  /**
23
19
  * Set or get the context `Runnable` to `runnable`.
24
20
  *
25
- * @api private
21
+ * @private
26
22
  * @param {Runnable} runnable
27
- * @return {Context}
23
+ * @return {Context} context
28
24
  */
29
25
  Context.prototype.runnable = function (runnable) {
30
26
  if (!arguments.length) {
@@ -35,9 +31,9 @@ Context.prototype.runnable = function (runnable) {
35
31
  };
36
32
 
37
33
  /**
38
- * Set test timeout `ms`.
34
+ * Set or get test timeout `ms`.
39
35
  *
40
- * @api private
36
+ * @private
41
37
  * @param {number} ms
42
38
  * @return {Context} self
43
39
  */
@@ -50,25 +46,16 @@ Context.prototype.timeout = function (ms) {
50
46
  };
51
47
 
52
48
  /**
53
- * Set test timeout `enabled`.
54
- *
55
- * @api private
56
- * @param {boolean} enabled
57
- * @return {Context} self
58
- */
59
- Context.prototype.enableTimeouts = function (enabled) {
60
- this.runnable().enableTimeouts(enabled);
61
- return this;
62
- };
63
-
64
- /**
65
- * Set test slowness threshold `ms`.
49
+ * Set or get test slowness threshold `ms`.
66
50
  *
67
- * @api private
51
+ * @private
68
52
  * @param {number} ms
69
53
  * @return {Context} self
70
54
  */
71
55
  Context.prototype.slow = function (ms) {
56
+ if (!arguments.length) {
57
+ return this.runnable().slow();
58
+ }
72
59
  this.runnable().slow(ms);
73
60
  return this;
74
61
  };
@@ -76,18 +63,17 @@ Context.prototype.slow = function (ms) {
76
63
  /**
77
64
  * Mark a test as skipped.
78
65
  *
79
- * @api private
80
- * @return {Context} self
66
+ * @private
67
+ * @throws Pending
81
68
  */
82
69
  Context.prototype.skip = function () {
83
70
  this.runnable().skip();
84
- return this;
85
71
  };
86
72
 
87
73
  /**
88
- * Allow a number of retries on failed tests
74
+ * Set or get a number of allowed retries on failed tests
89
75
  *
90
- * @api private
76
+ * @private
91
77
  * @param {number} n
92
78
  * @return {Context} self
93
79
  */
@@ -98,15 +84,3 @@ Context.prototype.retries = function (n) {
98
84
  this.runnable().retries(n);
99
85
  return this;
100
86
  };
101
-
102
- /**
103
- * Inspect the context void of `._runnable`.
104
- *
105
- * @api private
106
- * @return {string}
107
- */
108
- Context.prototype.inspect = function () {
109
- return JSON.stringify(this, function (key, val) {
110
- return key === 'runnable' || key === 'test' ? undefined : val;
111
- }, 2);
112
- };