mocha 8.2.0 → 8.3.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.
package/lib/mocha.js CHANGED
@@ -96,6 +96,61 @@ exports.Suite = Suite;
96
96
  exports.Hook = require('./hook');
97
97
  exports.Test = require('./test');
98
98
 
99
+ let currentContext;
100
+ exports.afterEach = function(...args) {
101
+ return (currentContext.afterEach || currentContext.teardown).apply(
102
+ this,
103
+ args
104
+ );
105
+ };
106
+ exports.after = function(...args) {
107
+ return (currentContext.after || currentContext.suiteTeardown).apply(
108
+ this,
109
+ args
110
+ );
111
+ };
112
+ exports.beforeEach = function(...args) {
113
+ return (currentContext.beforeEach || currentContext.setup).apply(this, args);
114
+ };
115
+ exports.before = function(...args) {
116
+ return (currentContext.before || currentContext.suiteSetup).apply(this, args);
117
+ };
118
+ exports.describe = function(...args) {
119
+ return (currentContext.describe || currentContext.suite).apply(this, args);
120
+ };
121
+ exports.describe.only = function(...args) {
122
+ return (currentContext.describe || currentContext.suite).only.apply(
123
+ this,
124
+ args
125
+ );
126
+ };
127
+ exports.describe.skip = function(...args) {
128
+ return (currentContext.describe || currentContext.suite).skip.apply(
129
+ this,
130
+ args
131
+ );
132
+ };
133
+ exports.it = function(...args) {
134
+ return (currentContext.it || currentContext.test).apply(this, args);
135
+ };
136
+ exports.it.only = function(...args) {
137
+ return (currentContext.it || currentContext.test).only.apply(this, args);
138
+ };
139
+ exports.it.skip = function(...args) {
140
+ return (currentContext.it || currentContext.test).skip.apply(this, args);
141
+ };
142
+ exports.xdescribe = exports.describe.skip;
143
+ exports.xit = exports.it.skip;
144
+ exports.setup = exports.beforeEach;
145
+ exports.suiteSetup = exports.before;
146
+ exports.suiteTeardown = exports.after;
147
+ exports.suite = exports.describe;
148
+ exports.teardown = exports.afterEach;
149
+ exports.test = exports.it;
150
+ exports.run = function(...args) {
151
+ return currentContext.run.apply(this, args);
152
+ };
153
+
99
154
  /**
100
155
  * Constructs a new Mocha instance with `options`.
101
156
  *
@@ -351,20 +406,7 @@ Mocha.prototype.ui = function(ui) {
351
406
  bindInterface(this.suite);
352
407
 
353
408
  this.suite.on(EVENT_FILE_PRE_REQUIRE, function(context) {
354
- exports.afterEach = context.afterEach || context.teardown;
355
- exports.after = context.after || context.suiteTeardown;
356
- exports.beforeEach = context.beforeEach || context.setup;
357
- exports.before = context.before || context.suiteSetup;
358
- exports.describe = context.describe || context.suite;
359
- exports.it = context.it || context.test;
360
- exports.xit = context.xit || (context.test && context.test.skip);
361
- exports.setup = context.setup || context.beforeEach;
362
- exports.suiteSetup = context.suiteSetup || context.before;
363
- exports.suiteTeardown = context.suiteTeardown || context.after;
364
- exports.suite = context.suite || context.describe;
365
- exports.teardown = context.teardown || context.afterEach;
366
- exports.test = context.test || context.it;
367
- exports.run = context.run;
409
+ currentContext = context;
368
410
  });
369
411
 
370
412
  return this;
@@ -1299,7 +1341,7 @@ Mocha.prototype.hasGlobalTeardownFixtures = function hasGlobalTeardownFixtures()
1299
1341
  * A (sync) function to assert a user-supplied plugin implementation is valid.
1300
1342
  *
1301
1343
  * Defined in a {@link PluginDefinition}.
1302
-
1344
+
1303
1345
  * @callback PluginValidator
1304
1346
  * @param {*} value - Value to check
1305
1347
  * @this {PluginDefinition}
package/lib/runnable.js CHANGED
@@ -5,9 +5,11 @@ var Pending = require('./pending');
5
5
  var debug = require('debug')('mocha:runnable');
6
6
  var milliseconds = require('ms');
7
7
  var utils = require('./utils');
8
- var errors = require('./errors');
9
- var createInvalidExceptionError = errors.createInvalidExceptionError;
10
- var createMultipleDoneError = errors.createMultipleDoneError;
8
+ const {
9
+ createInvalidExceptionError,
10
+ createMultipleDoneError,
11
+ createTimeoutError
12
+ } = require('./errors');
11
13
 
12
14
  /**
13
15
  * Save timer references to avoid Sinon interfering (see GH-237).
@@ -422,14 +424,11 @@ Runnable.prototype.run = function(fn) {
422
424
  * @private
423
425
  */
424
426
  Runnable.prototype._timeoutError = function(ms) {
425
- var msg =
426
- 'Timeout of ' +
427
- ms +
428
- 'ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.';
427
+ let msg = `Timeout of ${ms}ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.`;
429
428
  if (this.file) {
430
429
  msg += ' (' + this.file + ')';
431
430
  }
432
- return new Error(msg);
431
+ return createTimeoutError(msg, ms, this.file);
433
432
  };
434
433
 
435
434
  var constants = utils.defineConstants(
package/lib/runner.js CHANGED
@@ -24,10 +24,13 @@ var sQuote = utils.sQuote;
24
24
  var stackFilter = utils.stackTraceFilter();
25
25
  var stringify = utils.stringify;
26
26
 
27
- var errors = require('./errors');
28
- var createInvalidExceptionError = errors.createInvalidExceptionError;
29
- var createUnsupportedError = errors.createUnsupportedError;
30
- var createFatalError = errors.createFatalError;
27
+ const {
28
+ createInvalidExceptionError,
29
+ createUnsupportedError,
30
+ createFatalError,
31
+ isMochaError,
32
+ constants: errorConstants
33
+ } = require('./errors');
31
34
 
32
35
  /**
33
36
  * Non-enumerable globals.
@@ -179,6 +182,29 @@ class Runner extends EventEmitter {
179
182
  this.globals(this.globalProps());
180
183
 
181
184
  this.uncaught = this._uncaught.bind(this);
185
+ this.unhandled = (reason, promise) => {
186
+ if (isMochaError(reason)) {
187
+ debug(
188
+ 'trapped unhandled rejection coming out of Mocha; forwarding to uncaught handler:',
189
+ reason
190
+ );
191
+ this.uncaught(reason);
192
+ } else {
193
+ debug(
194
+ 'trapped unhandled rejection from (probably) user code; re-emitting on process'
195
+ );
196
+ this._removeEventListener(
197
+ process,
198
+ 'unhandledRejection',
199
+ this.unhandled
200
+ );
201
+ try {
202
+ process.emit('unhandledRejection', reason, promise);
203
+ } finally {
204
+ this._addEventListener(process, 'unhandledRejection', this.unhandled);
205
+ }
206
+ }
207
+ };
182
208
  }
183
209
  }
184
210
 
@@ -414,7 +440,7 @@ Runner.prototype.fail = function(test, err, force) {
414
440
  return;
415
441
  }
416
442
  if (this.state === constants.STATE_STOPPED) {
417
- if (err.code === errors.constants.MULTIPLE_DONE) {
443
+ if (err.code === errorConstants.MULTIPLE_DONE) {
418
444
  throw err;
419
445
  }
420
446
  throw createFatalError(
@@ -1025,9 +1051,7 @@ Runner.prototype.run = function(fn, opts = {}) {
1025
1051
  this.emit(constants.EVENT_RUN_BEGIN);
1026
1052
  debug('run(): emitted %s', constants.EVENT_RUN_BEGIN);
1027
1053
 
1028
- this.runSuite(rootSuite, async () => {
1029
- end();
1030
- });
1054
+ this.runSuite(rootSuite, end);
1031
1055
  };
1032
1056
 
1033
1057
  const prepare = () => {
@@ -1061,9 +1085,9 @@ Runner.prototype.run = function(fn, opts = {}) {
1061
1085
  });
1062
1086
 
1063
1087
  this._removeEventListener(process, 'uncaughtException', this.uncaught);
1064
- this._removeEventListener(process, 'unhandledRejection', this.uncaught);
1088
+ this._removeEventListener(process, 'unhandledRejection', this.unhandled);
1065
1089
  this._addEventListener(process, 'uncaughtException', this.uncaught);
1066
- this._addEventListener(process, 'unhandledRejection', this.uncaught);
1090
+ this._addEventListener(process, 'unhandledRejection', this.unhandled);
1067
1091
 
1068
1092
  if (this._delay) {
1069
1093
  // for reporters, I guess.
package/lib/utils.js CHANGED
@@ -298,6 +298,9 @@ function jsonStringify(object, spaces, depth) {
298
298
  ? '-0'
299
299
  : val.toString();
300
300
  break;
301
+ case 'bigint':
302
+ val = val.toString() + 'n';
303
+ break;
301
304
  case 'date':
302
305
  var sDate = isNaN(val.getTime()) ? val.toString() : val.toISOString();
303
306
  val = '[Date: ' + sDate + ']';