mocha 9.1.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 (76) hide show
  1. package/CHANGELOG.md +1015 -0
  2. package/LICENSE +22 -0
  3. package/README.md +70 -0
  4. package/assets/growl/error.png +0 -0
  5. package/assets/growl/ok.png +0 -0
  6. package/bin/_mocha +10 -0
  7. package/bin/mocha +142 -0
  8. package/browser-entry.js +216 -0
  9. package/index.js +3 -0
  10. package/lib/browser/growl.js +169 -0
  11. package/lib/browser/highlight-tags.js +39 -0
  12. package/lib/browser/parse-query.js +24 -0
  13. package/lib/browser/progress.js +123 -0
  14. package/lib/browser/template.html +20 -0
  15. package/lib/cli/cli.js +89 -0
  16. package/lib/cli/collect-files.js +92 -0
  17. package/lib/cli/commands.js +13 -0
  18. package/lib/cli/config.js +105 -0
  19. package/lib/cli/index.js +3 -0
  20. package/lib/cli/init.js +36 -0
  21. package/lib/cli/lookup-files.js +145 -0
  22. package/lib/cli/node-flags.js +85 -0
  23. package/lib/cli/one-and-dones.js +69 -0
  24. package/lib/cli/options.js +261 -0
  25. package/lib/cli/run-helpers.js +243 -0
  26. package/lib/cli/run-option-metadata.js +117 -0
  27. package/lib/cli/run.js +379 -0
  28. package/lib/cli/watch-run.js +380 -0
  29. package/lib/context.js +86 -0
  30. package/lib/errors.js +563 -0
  31. package/lib/hook.js +89 -0
  32. package/lib/interfaces/bdd.js +111 -0
  33. package/lib/interfaces/common.js +193 -0
  34. package/lib/interfaces/exports.js +60 -0
  35. package/lib/interfaces/index.js +6 -0
  36. package/lib/interfaces/qunit.js +98 -0
  37. package/lib/interfaces/tdd.js +106 -0
  38. package/lib/mocha.js +1374 -0
  39. package/lib/mocharc.json +10 -0
  40. package/lib/nodejs/buffered-worker-pool.js +172 -0
  41. package/lib/nodejs/esm-utils.js +109 -0
  42. package/lib/nodejs/file-unloader.js +15 -0
  43. package/lib/nodejs/growl.js +137 -0
  44. package/lib/nodejs/parallel-buffered-runner.js +433 -0
  45. package/lib/nodejs/reporters/parallel-buffered.js +165 -0
  46. package/lib/nodejs/serializer.js +412 -0
  47. package/lib/nodejs/worker.js +151 -0
  48. package/lib/pending.js +16 -0
  49. package/lib/plugin-loader.js +286 -0
  50. package/lib/reporters/base.js +537 -0
  51. package/lib/reporters/doc.js +95 -0
  52. package/lib/reporters/dot.js +81 -0
  53. package/lib/reporters/html.js +390 -0
  54. package/lib/reporters/index.js +19 -0
  55. package/lib/reporters/json-stream.js +92 -0
  56. package/lib/reporters/json.js +162 -0
  57. package/lib/reporters/landing.js +116 -0
  58. package/lib/reporters/list.js +78 -0
  59. package/lib/reporters/markdown.js +112 -0
  60. package/lib/reporters/min.js +52 -0
  61. package/lib/reporters/nyan.js +276 -0
  62. package/lib/reporters/progress.js +104 -0
  63. package/lib/reporters/spec.js +99 -0
  64. package/lib/reporters/tap.js +293 -0
  65. package/lib/reporters/xunit.js +217 -0
  66. package/lib/runnable.js +476 -0
  67. package/lib/runner.js +1269 -0
  68. package/lib/stats-collector.js +83 -0
  69. package/lib/suite.js +695 -0
  70. package/lib/test.js +113 -0
  71. package/lib/utils.js +641 -0
  72. package/mocha-es2018.js +19816 -0
  73. package/mocha.css +325 -0
  74. package/mocha.js +30844 -0
  75. package/mocha.js.map +1 -0
  76. package/package.json +200 -0
package/lib/errors.js ADDED
@@ -0,0 +1,563 @@
1
+ 'use strict';
2
+
3
+ const {format} = require('util');
4
+
5
+ /**
6
+ * Contains error codes, factory functions to create throwable error objects,
7
+ * and warning/deprecation functions.
8
+ * @module
9
+ */
10
+
11
+ /**
12
+ * process.emitWarning or a polyfill
13
+ * @see https://nodejs.org/api/process.html#process_process_emitwarning_warning_options
14
+ * @ignore
15
+ */
16
+ const emitWarning = (msg, type) => {
17
+ if (process.emitWarning) {
18
+ process.emitWarning(msg, type);
19
+ } else {
20
+ /* istanbul ignore next */
21
+ process.nextTick(function() {
22
+ console.warn(type + ': ' + msg);
23
+ });
24
+ }
25
+ };
26
+
27
+ /**
28
+ * Show a deprecation warning. Each distinct message is only displayed once.
29
+ * Ignores empty messages.
30
+ *
31
+ * @param {string} [msg] - Warning to print
32
+ * @private
33
+ */
34
+ const deprecate = msg => {
35
+ msg = String(msg);
36
+ if (msg && !deprecate.cache[msg]) {
37
+ deprecate.cache[msg] = true;
38
+ emitWarning(msg, 'DeprecationWarning');
39
+ }
40
+ };
41
+ deprecate.cache = {};
42
+
43
+ /**
44
+ * Show a generic warning.
45
+ * Ignores empty messages.
46
+ *
47
+ * @param {string} [msg] - Warning to print
48
+ * @private
49
+ */
50
+ const warn = msg => {
51
+ if (msg) {
52
+ emitWarning(msg);
53
+ }
54
+ };
55
+
56
+ /**
57
+ * When Mocha throws exceptions (or rejects `Promise`s), it attempts to assign a `code` property to the `Error` object, for easier handling. These are the potential values of `code`.
58
+ * @public
59
+ * @namespace
60
+ * @memberof module:lib/errors
61
+ */
62
+ var constants = {
63
+ /**
64
+ * An unrecoverable error.
65
+ * @constant
66
+ * @default
67
+ */
68
+ FATAL: 'ERR_MOCHA_FATAL',
69
+
70
+ /**
71
+ * The type of an argument to a function call is invalid
72
+ * @constant
73
+ * @default
74
+ */
75
+ INVALID_ARG_TYPE: 'ERR_MOCHA_INVALID_ARG_TYPE',
76
+
77
+ /**
78
+ * The value of an argument to a function call is invalid
79
+ * @constant
80
+ * @default
81
+ */
82
+ INVALID_ARG_VALUE: 'ERR_MOCHA_INVALID_ARG_VALUE',
83
+
84
+ /**
85
+ * Something was thrown, but it wasn't an `Error`
86
+ * @constant
87
+ * @default
88
+ */
89
+ INVALID_EXCEPTION: 'ERR_MOCHA_INVALID_EXCEPTION',
90
+
91
+ /**
92
+ * An interface (e.g., `Mocha.interfaces`) is unknown or invalid
93
+ * @constant
94
+ * @default
95
+ */
96
+ INVALID_INTERFACE: 'ERR_MOCHA_INVALID_INTERFACE',
97
+
98
+ /**
99
+ * A reporter (.e.g, `Mocha.reporters`) is unknown or invalid
100
+ * @constant
101
+ * @default
102
+ */
103
+ INVALID_REPORTER: 'ERR_MOCHA_INVALID_REPORTER',
104
+
105
+ /**
106
+ * `done()` was called twice in a `Test` or `Hook` callback
107
+ * @constant
108
+ * @default
109
+ */
110
+ MULTIPLE_DONE: 'ERR_MOCHA_MULTIPLE_DONE',
111
+
112
+ /**
113
+ * No files matched the pattern provided by the user
114
+ * @constant
115
+ * @default
116
+ */
117
+ NO_FILES_MATCH_PATTERN: 'ERR_MOCHA_NO_FILES_MATCH_PATTERN',
118
+
119
+ /**
120
+ * Known, but unsupported behavior of some kind
121
+ * @constant
122
+ * @default
123
+ */
124
+ UNSUPPORTED: 'ERR_MOCHA_UNSUPPORTED',
125
+
126
+ /**
127
+ * Invalid state transition occurring in `Mocha` instance
128
+ * @constant
129
+ * @default
130
+ */
131
+ INSTANCE_ALREADY_RUNNING: 'ERR_MOCHA_INSTANCE_ALREADY_RUNNING',
132
+
133
+ /**
134
+ * Invalid state transition occurring in `Mocha` instance
135
+ * @constant
136
+ * @default
137
+ */
138
+ INSTANCE_ALREADY_DISPOSED: 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED',
139
+
140
+ /**
141
+ * Use of `only()` w/ `--forbid-only` results in this error.
142
+ * @constant
143
+ * @default
144
+ */
145
+ FORBIDDEN_EXCLUSIVITY: 'ERR_MOCHA_FORBIDDEN_EXCLUSIVITY',
146
+
147
+ /**
148
+ * To be thrown when a user-defined plugin implementation (e.g., `mochaHooks`) is invalid
149
+ * @constant
150
+ * @default
151
+ */
152
+ INVALID_PLUGIN_IMPLEMENTATION: 'ERR_MOCHA_INVALID_PLUGIN_IMPLEMENTATION',
153
+
154
+ /**
155
+ * To be thrown when a builtin or third-party plugin definition (the _definition_ of `mochaHooks`) is invalid
156
+ * @constant
157
+ * @default
158
+ */
159
+ INVALID_PLUGIN_DEFINITION: 'ERR_MOCHA_INVALID_PLUGIN_DEFINITION',
160
+
161
+ /**
162
+ * When a runnable exceeds its allowed run time.
163
+ * @constant
164
+ * @default
165
+ */
166
+ TIMEOUT: 'ERR_MOCHA_TIMEOUT',
167
+
168
+ /**
169
+ * Input file is not able to be parsed
170
+ * @constant
171
+ * @default
172
+ */
173
+ UNPARSABLE_FILE: 'ERR_MOCHA_UNPARSABLE_FILE'
174
+ };
175
+
176
+ /**
177
+ * A set containing all string values of all Mocha error constants, for use by {@link isMochaError}.
178
+ * @private
179
+ */
180
+ const MOCHA_ERRORS = new Set(Object.values(constants));
181
+
182
+ /**
183
+ * Creates an error object to be thrown when no files to be tested could be found using specified pattern.
184
+ *
185
+ * @public
186
+ * @static
187
+ * @param {string} message - Error message to be displayed.
188
+ * @param {string} pattern - User-specified argument value.
189
+ * @returns {Error} instance detailing the error condition
190
+ */
191
+ function createNoFilesMatchPatternError(message, pattern) {
192
+ var err = new Error(message);
193
+ err.code = constants.NO_FILES_MATCH_PATTERN;
194
+ err.pattern = pattern;
195
+ return err;
196
+ }
197
+
198
+ /**
199
+ * Creates an error object to be thrown when the reporter specified in the options was not found.
200
+ *
201
+ * @public
202
+ * @param {string} message - Error message to be displayed.
203
+ * @param {string} reporter - User-specified reporter value.
204
+ * @returns {Error} instance detailing the error condition
205
+ */
206
+ function createInvalidReporterError(message, reporter) {
207
+ var err = new TypeError(message);
208
+ err.code = constants.INVALID_REPORTER;
209
+ err.reporter = reporter;
210
+ return err;
211
+ }
212
+
213
+ /**
214
+ * Creates an error object to be thrown when the interface specified in the options was not found.
215
+ *
216
+ * @public
217
+ * @static
218
+ * @param {string} message - Error message to be displayed.
219
+ * @param {string} ui - User-specified interface value.
220
+ * @returns {Error} instance detailing the error condition
221
+ */
222
+ function createInvalidInterfaceError(message, ui) {
223
+ var err = new Error(message);
224
+ err.code = constants.INVALID_INTERFACE;
225
+ err.interface = ui;
226
+ return err;
227
+ }
228
+
229
+ /**
230
+ * Creates an error object to be thrown when a behavior, option, or parameter is unsupported.
231
+ *
232
+ * @public
233
+ * @static
234
+ * @param {string} message - Error message to be displayed.
235
+ * @returns {Error} instance detailing the error condition
236
+ */
237
+ function createUnsupportedError(message) {
238
+ var err = new Error(message);
239
+ err.code = constants.UNSUPPORTED;
240
+ return err;
241
+ }
242
+
243
+ /**
244
+ * Creates an error object to be thrown when an argument is missing.
245
+ *
246
+ * @public
247
+ * @static
248
+ * @param {string} message - Error message to be displayed.
249
+ * @param {string} argument - Argument name.
250
+ * @param {string} expected - Expected argument datatype.
251
+ * @returns {Error} instance detailing the error condition
252
+ */
253
+ function createMissingArgumentError(message, argument, expected) {
254
+ return createInvalidArgumentTypeError(message, argument, expected);
255
+ }
256
+
257
+ /**
258
+ * Creates an error object to be thrown when an argument did not use the supported type
259
+ *
260
+ * @public
261
+ * @static
262
+ * @param {string} message - Error message to be displayed.
263
+ * @param {string} argument - Argument name.
264
+ * @param {string} expected - Expected argument datatype.
265
+ * @returns {Error} instance detailing the error condition
266
+ */
267
+ function createInvalidArgumentTypeError(message, argument, expected) {
268
+ var err = new TypeError(message);
269
+ err.code = constants.INVALID_ARG_TYPE;
270
+ err.argument = argument;
271
+ err.expected = expected;
272
+ err.actual = typeof argument;
273
+ return err;
274
+ }
275
+
276
+ /**
277
+ * Creates an error object to be thrown when an argument did not use the supported value
278
+ *
279
+ * @public
280
+ * @static
281
+ * @param {string} message - Error message to be displayed.
282
+ * @param {string} argument - Argument name.
283
+ * @param {string} value - Argument value.
284
+ * @param {string} [reason] - Why value is invalid.
285
+ * @returns {Error} instance detailing the error condition
286
+ */
287
+ function createInvalidArgumentValueError(message, argument, value, reason) {
288
+ var err = new TypeError(message);
289
+ err.code = constants.INVALID_ARG_VALUE;
290
+ err.argument = argument;
291
+ err.value = value;
292
+ err.reason = typeof reason !== 'undefined' ? reason : 'is invalid';
293
+ return err;
294
+ }
295
+
296
+ /**
297
+ * Creates an error object to be thrown when an exception was caught, but the `Error` is falsy or undefined.
298
+ *
299
+ * @public
300
+ * @static
301
+ * @param {string} message - Error message to be displayed.
302
+ * @returns {Error} instance detailing the error condition
303
+ */
304
+ function createInvalidExceptionError(message, value) {
305
+ var err = new Error(message);
306
+ err.code = constants.INVALID_EXCEPTION;
307
+ err.valueType = typeof value;
308
+ err.value = value;
309
+ return err;
310
+ }
311
+
312
+ /**
313
+ * Creates an error object to be thrown when an unrecoverable error occurs.
314
+ *
315
+ * @public
316
+ * @static
317
+ * @param {string} message - Error message to be displayed.
318
+ * @returns {Error} instance detailing the error condition
319
+ */
320
+ function createFatalError(message, value) {
321
+ var err = new Error(message);
322
+ err.code = constants.FATAL;
323
+ err.valueType = typeof value;
324
+ err.value = value;
325
+ return err;
326
+ }
327
+
328
+ /**
329
+ * Dynamically creates a plugin-type-specific error based on plugin type
330
+ * @param {string} message - Error message
331
+ * @param {"reporter"|"ui"} pluginType - Plugin type. Future: expand as needed
332
+ * @param {string} [pluginId] - Name/path of plugin, if any
333
+ * @throws When `pluginType` is not known
334
+ * @public
335
+ * @static
336
+ * @returns {Error}
337
+ */
338
+ function createInvalidLegacyPluginError(message, pluginType, pluginId) {
339
+ switch (pluginType) {
340
+ case 'reporter':
341
+ return createInvalidReporterError(message, pluginId);
342
+ case 'ui':
343
+ return createInvalidInterfaceError(message, pluginId);
344
+ default:
345
+ throw new Error('unknown pluginType "' + pluginType + '"');
346
+ }
347
+ }
348
+
349
+ /**
350
+ * **DEPRECATED**. Use {@link createInvalidLegacyPluginError} instead Dynamically creates a plugin-type-specific error based on plugin type
351
+ * @deprecated
352
+ * @param {string} message - Error message
353
+ * @param {"reporter"|"interface"} pluginType - Plugin type. Future: expand as needed
354
+ * @param {string} [pluginId] - Name/path of plugin, if any
355
+ * @throws When `pluginType` is not known
356
+ * @public
357
+ * @static
358
+ * @returns {Error}
359
+ */
360
+ function createInvalidPluginError(...args) {
361
+ deprecate('Use createInvalidLegacyPluginError() instead');
362
+ return createInvalidLegacyPluginError(...args);
363
+ }
364
+
365
+ /**
366
+ * Creates an error object to be thrown when a mocha object's `run` method is executed while it is already disposed.
367
+ * @param {string} message The error message to be displayed.
368
+ * @param {boolean} cleanReferencesAfterRun the value of `cleanReferencesAfterRun`
369
+ * @param {Mocha} instance the mocha instance that throw this error
370
+ * @static
371
+ */
372
+ function createMochaInstanceAlreadyDisposedError(
373
+ message,
374
+ cleanReferencesAfterRun,
375
+ instance
376
+ ) {
377
+ var err = new Error(message);
378
+ err.code = constants.INSTANCE_ALREADY_DISPOSED;
379
+ err.cleanReferencesAfterRun = cleanReferencesAfterRun;
380
+ err.instance = instance;
381
+ return err;
382
+ }
383
+
384
+ /**
385
+ * Creates an error object to be thrown when a mocha object's `run` method is called while a test run is in progress.
386
+ * @param {string} message The error message to be displayed.
387
+ * @static
388
+ * @public
389
+ */
390
+ function createMochaInstanceAlreadyRunningError(message, instance) {
391
+ var err = new Error(message);
392
+ err.code = constants.INSTANCE_ALREADY_RUNNING;
393
+ err.instance = instance;
394
+ return err;
395
+ }
396
+
397
+ /**
398
+ * Creates an error object to be thrown when done() is called multiple times in a test
399
+ *
400
+ * @public
401
+ * @param {Runnable} runnable - Original runnable
402
+ * @param {Error} [originalErr] - Original error, if any
403
+ * @returns {Error} instance detailing the error condition
404
+ * @static
405
+ */
406
+ function createMultipleDoneError(runnable, originalErr) {
407
+ var title;
408
+ try {
409
+ title = format('<%s>', runnable.fullTitle());
410
+ if (runnable.parent.root) {
411
+ title += ' (of root suite)';
412
+ }
413
+ } catch (ignored) {
414
+ title = format('<%s> (of unknown suite)', runnable.title);
415
+ }
416
+ var message = format(
417
+ 'done() called multiple times in %s %s',
418
+ runnable.type ? runnable.type : 'unknown runnable',
419
+ title
420
+ );
421
+ if (runnable.file) {
422
+ message += format(' of file %s', runnable.file);
423
+ }
424
+ if (originalErr) {
425
+ message += format('; in addition, done() received error: %s', originalErr);
426
+ }
427
+
428
+ var err = new Error(message);
429
+ err.code = constants.MULTIPLE_DONE;
430
+ err.valueType = typeof originalErr;
431
+ err.value = originalErr;
432
+ return err;
433
+ }
434
+
435
+ /**
436
+ * Creates an error object to be thrown when `.only()` is used with
437
+ * `--forbid-only`.
438
+ * @static
439
+ * @public
440
+ * @param {Mocha} mocha - Mocha instance
441
+ * @returns {Error} Error with code {@link constants.FORBIDDEN_EXCLUSIVITY}
442
+ */
443
+ function createForbiddenExclusivityError(mocha) {
444
+ var err = new Error(
445
+ mocha.isWorker
446
+ ? '`.only` is not supported in parallel mode'
447
+ : '`.only` forbidden by --forbid-only'
448
+ );
449
+ err.code = constants.FORBIDDEN_EXCLUSIVITY;
450
+ return err;
451
+ }
452
+
453
+ /**
454
+ * Creates an error object to be thrown when a plugin definition is invalid
455
+ * @static
456
+ * @param {string} msg - Error message
457
+ * @param {PluginDefinition} [pluginDef] - Problematic plugin definition
458
+ * @public
459
+ * @returns {Error} Error with code {@link constants.INVALID_PLUGIN_DEFINITION}
460
+ */
461
+ function createInvalidPluginDefinitionError(msg, pluginDef) {
462
+ const err = new Error(msg);
463
+ err.code = constants.INVALID_PLUGIN_DEFINITION;
464
+ err.pluginDef = pluginDef;
465
+ return err;
466
+ }
467
+
468
+ /**
469
+ * Creates an error object to be thrown when a plugin implementation (user code) is invalid
470
+ * @static
471
+ * @param {string} msg - Error message
472
+ * @param {Object} [opts] - Plugin definition and user-supplied implementation
473
+ * @param {PluginDefinition} [opts.pluginDef] - Plugin Definition
474
+ * @param {*} [opts.pluginImpl] - Plugin Implementation (user-supplied)
475
+ * @public
476
+ * @returns {Error} Error with code {@link constants.INVALID_PLUGIN_DEFINITION}
477
+ */
478
+ function createInvalidPluginImplementationError(
479
+ msg,
480
+ {pluginDef, pluginImpl} = {}
481
+ ) {
482
+ const err = new Error(msg);
483
+ err.code = constants.INVALID_PLUGIN_IMPLEMENTATION;
484
+ err.pluginDef = pluginDef;
485
+ err.pluginImpl = pluginImpl;
486
+ return err;
487
+ }
488
+
489
+ /**
490
+ * Creates an error object to be thrown when a runnable exceeds its allowed run time.
491
+ * @static
492
+ * @param {string} msg - Error message
493
+ * @param {number} [timeout] - Timeout in ms
494
+ * @param {string} [file] - File, if given
495
+ * @returns {MochaTimeoutError}
496
+ */
497
+ function createTimeoutError(msg, timeout, file) {
498
+ const err = new Error(msg);
499
+ err.code = constants.TIMEOUT;
500
+ err.timeout = timeout;
501
+ err.file = file;
502
+ return err;
503
+ }
504
+
505
+ /**
506
+ * Creates an error object to be thrown when file is unparsable
507
+ * @public
508
+ * @static
509
+ * @param {string} message - Error message to be displayed.
510
+ * @param {string} filename - File name
511
+ * @returns {Error} Error with code {@link constants.UNPARSABLE_FILE}
512
+ */
513
+ function createUnparsableFileError(message, filename) {
514
+ var err = new Error(message);
515
+ err.code = constants.UNPARSABLE_FILE;
516
+ return err;
517
+ }
518
+
519
+ /**
520
+ * Returns `true` if an error came out of Mocha.
521
+ * _Can suffer from false negatives, but not false positives._
522
+ * @static
523
+ * @public
524
+ * @param {*} err - Error, or anything
525
+ * @returns {boolean}
526
+ */
527
+ const isMochaError = err =>
528
+ Boolean(err && typeof err === 'object' && MOCHA_ERRORS.has(err.code));
529
+
530
+ module.exports = {
531
+ constants,
532
+ createFatalError,
533
+ createForbiddenExclusivityError,
534
+ createInvalidArgumentTypeError,
535
+ createInvalidArgumentValueError,
536
+ createInvalidExceptionError,
537
+ createInvalidInterfaceError,
538
+ createInvalidLegacyPluginError,
539
+ createInvalidPluginDefinitionError,
540
+ createInvalidPluginError,
541
+ createInvalidPluginImplementationError,
542
+ createInvalidReporterError,
543
+ createMissingArgumentError,
544
+ createMochaInstanceAlreadyDisposedError,
545
+ createMochaInstanceAlreadyRunningError,
546
+ createMultipleDoneError,
547
+ createNoFilesMatchPatternError,
548
+ createTimeoutError,
549
+ createUnparsableFileError,
550
+ createUnsupportedError,
551
+ deprecate,
552
+ isMochaError,
553
+ warn
554
+ };
555
+
556
+ /**
557
+ * The error thrown when a Runnable times out
558
+ * @memberof module:lib/errors
559
+ * @typedef {Error} MochaTimeoutError
560
+ * @property {constants.TIMEOUT} code - Error code
561
+ * @property {number?} timeout Timeout in ms
562
+ * @property {string?} file Filepath, if given
563
+ */
package/lib/hook.js ADDED
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ var Runnable = require('./runnable');
4
+ const {inherits, constants} = require('./utils');
5
+ const {MOCHA_ID_PROP_NAME} = constants;
6
+
7
+ /**
8
+ * Expose `Hook`.
9
+ */
10
+
11
+ module.exports = Hook;
12
+
13
+ /**
14
+ * Initialize a new `Hook` with the given `title` and callback `fn`
15
+ *
16
+ * @class
17
+ * @extends Runnable
18
+ * @param {String} title
19
+ * @param {Function} fn
20
+ */
21
+ function Hook(title, fn) {
22
+ Runnable.call(this, title, fn);
23
+ this.type = 'hook';
24
+ }
25
+
26
+ /**
27
+ * Inherit from `Runnable.prototype`.
28
+ */
29
+ inherits(Hook, Runnable);
30
+
31
+ /**
32
+ * Resets the state for a next run.
33
+ */
34
+ Hook.prototype.reset = function() {
35
+ Runnable.prototype.reset.call(this);
36
+ delete this._error;
37
+ };
38
+
39
+ /**
40
+ * Get or set the test `err`.
41
+ *
42
+ * @memberof Hook
43
+ * @public
44
+ * @param {Error} err
45
+ * @return {Error}
46
+ */
47
+ Hook.prototype.error = function(err) {
48
+ if (!arguments.length) {
49
+ err = this._error;
50
+ this._error = null;
51
+ return err;
52
+ }
53
+
54
+ this._error = err;
55
+ };
56
+
57
+ /**
58
+ * Returns an object suitable for IPC.
59
+ * Functions are represented by keys beginning with `$$`.
60
+ * @private
61
+ * @returns {Object}
62
+ */
63
+ Hook.prototype.serialize = function serialize() {
64
+ return {
65
+ $$currentRetry: this.currentRetry(),
66
+ $$fullTitle: this.fullTitle(),
67
+ $$isPending: Boolean(this.isPending()),
68
+ $$titlePath: this.titlePath(),
69
+ ctx:
70
+ this.ctx && this.ctx.currentTest
71
+ ? {
72
+ currentTest: {
73
+ title: this.ctx.currentTest.title,
74
+ [MOCHA_ID_PROP_NAME]: this.ctx.currentTest.id
75
+ }
76
+ }
77
+ : {},
78
+ duration: this.duration,
79
+ file: this.file,
80
+ parent: {
81
+ $$fullTitle: this.parent.fullTitle(),
82
+ [MOCHA_ID_PROP_NAME]: this.parent.id
83
+ },
84
+ state: this.state,
85
+ title: this.title,
86
+ type: this.type,
87
+ [MOCHA_ID_PROP_NAME]: this.id
88
+ };
89
+ };