mocha 11.7.4 → 11.7.6
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/bin/_mocha +2 -2
- package/bin/mocha.js +46 -44
- package/browser-entry.js +20 -20
- package/index.js +2 -2
- package/lib/browser/highlight-tags.js +6 -6
- package/lib/browser/parse-query.js +5 -5
- package/lib/browser/template.html +2 -2
- package/lib/cli/cli.js +32 -27
- package/lib/cli/collect-files.js +25 -25
- package/lib/cli/commands.js +4 -4
- package/lib/cli/config.js +26 -25
- package/lib/cli/index.js +2 -2
- package/lib/cli/init.js +19 -19
- package/lib/cli/lookup-files.js +20 -20
- package/lib/cli/node-flags.js +12 -12
- package/lib/cli/one-and-dones.js +12 -11
- package/lib/cli/options.js +49 -49
- package/lib/cli/run-helpers.js +52 -54
- package/lib/cli/run-option-metadata.js +75 -75
- package/lib/cli/run.js +164 -159
- package/lib/cli/watch-run.js +75 -75
- package/lib/context.js +1 -1
- package/lib/error-constants.js +17 -17
- package/lib/errors.js +26 -26
- package/lib/hook.js +9 -9
- package/lib/interfaces/bdd.js +8 -8
- package/lib/interfaces/common.js +12 -12
- package/lib/interfaces/exports.js +8 -8
- package/lib/interfaces/index.js +5 -5
- package/lib/interfaces/qunit.js +7 -7
- package/lib/interfaces/tdd.js +7 -7
- package/lib/mocha.js +97 -97
- package/lib/nodejs/buffered-worker-pool.js +30 -30
- package/lib/nodejs/esm-utils.js +24 -21
- package/lib/nodejs/file-unloader.js +2 -2
- package/lib/nodejs/parallel-buffered-runner.js +67 -67
- package/lib/nodejs/reporters/parallel-buffered.js +13 -10
- package/lib/nodejs/serializer.js +47 -47
- package/lib/nodejs/worker.js +38 -38
- package/lib/pending.js +1 -1
- package/lib/plugin-loader.js +48 -48
- package/lib/reporters/base.js +97 -94
- package/lib/reporters/doc.js +17 -17
- package/lib/reporters/dot.js +14 -14
- package/lib/reporters/html.js +73 -67
- package/lib/reporters/index.js +16 -16
- package/lib/reporters/json-stream.js +10 -10
- package/lib/reporters/json.js +16 -16
- package/lib/reporters/landing.js +20 -20
- package/lib/reporters/list.js +10 -10
- package/lib/reporters/markdown.js +21 -21
- package/lib/reporters/min.js +7 -7
- package/lib/reporters/nyan.js +35 -35
- package/lib/reporters/progress.js +14 -14
- package/lib/reporters/spec.js +15 -15
- package/lib/reporters/tap.js +26 -26
- package/lib/reporters/xunit.js +38 -34
- package/lib/runnable.js +41 -41
- package/lib/runner.js +105 -105
- package/lib/stats-collector.js +4 -4
- package/lib/suite.js +56 -46
- package/lib/test.js +10 -10
- package/lib/utils.js +122 -122
- package/mocha.css +68 -50
- package/mocha.js +826 -803
- package/mocha.js.map +1 -1
- package/package.json +8 -13
package/lib/mocha.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
/*!
|
|
4
4
|
* mocha
|
|
@@ -6,24 +6,24 @@
|
|
|
6
6
|
* MIT Licensed
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
var escapeRe = require(
|
|
10
|
-
var path = require(
|
|
11
|
-
var builtinReporters = require(
|
|
12
|
-
var utils = require(
|
|
13
|
-
var mocharc = require(
|
|
14
|
-
var Suite = require(
|
|
15
|
-
var esmUtils = require(
|
|
16
|
-
var createStatsCollector = require(
|
|
9
|
+
var escapeRe = require("escape-string-regexp");
|
|
10
|
+
var path = require("node:path");
|
|
11
|
+
var builtinReporters = require("./reporters");
|
|
12
|
+
var utils = require("./utils");
|
|
13
|
+
var mocharc = require("./mocharc.json");
|
|
14
|
+
var Suite = require("./suite");
|
|
15
|
+
var esmUtils = require("./nodejs/esm-utils");
|
|
16
|
+
var createStatsCollector = require("./stats-collector");
|
|
17
17
|
const {
|
|
18
18
|
createInvalidReporterError,
|
|
19
19
|
createInvalidInterfaceError,
|
|
20
20
|
createMochaInstanceAlreadyDisposedError,
|
|
21
21
|
createMochaInstanceAlreadyRunningError,
|
|
22
|
-
createUnsupportedError
|
|
23
|
-
} = require(
|
|
24
|
-
const {EVENT_FILE_PRE_REQUIRE, EVENT_FILE_POST_REQUIRE, EVENT_FILE_REQUIRE} =
|
|
22
|
+
createUnsupportedError,
|
|
23
|
+
} = require("./errors");
|
|
24
|
+
const { EVENT_FILE_PRE_REQUIRE, EVENT_FILE_POST_REQUIRE, EVENT_FILE_REQUIRE } =
|
|
25
25
|
Suite.constants;
|
|
26
|
-
var debug = require(
|
|
26
|
+
var debug = require("debug")("mocha:mocha");
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* @typedef {import('./types.d.ts').DoneCB} DoneCB
|
|
@@ -45,32 +45,32 @@ var mochaStates = utils.defineConstants({
|
|
|
45
45
|
* Initial state of the mocha instance
|
|
46
46
|
* @private
|
|
47
47
|
*/
|
|
48
|
-
INIT:
|
|
48
|
+
INIT: "init",
|
|
49
49
|
/**
|
|
50
50
|
* Mocha instance is running tests
|
|
51
51
|
* @private
|
|
52
52
|
*/
|
|
53
|
-
RUNNING:
|
|
53
|
+
RUNNING: "running",
|
|
54
54
|
/**
|
|
55
55
|
* Mocha instance is done running tests and references to test functions and hooks are cleaned.
|
|
56
56
|
* You can reset this state by unloading the test files.
|
|
57
57
|
* @private
|
|
58
58
|
*/
|
|
59
|
-
REFERENCES_CLEANED:
|
|
59
|
+
REFERENCES_CLEANED: "referencesCleaned",
|
|
60
60
|
/**
|
|
61
61
|
* Mocha instance is disposed and can no longer be used.
|
|
62
62
|
* @private
|
|
63
63
|
*/
|
|
64
|
-
DISPOSED:
|
|
64
|
+
DISPOSED: "disposed",
|
|
65
65
|
});
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
68
|
* To require local UIs and reporters when running in node.
|
|
69
69
|
*/
|
|
70
70
|
|
|
71
|
-
if (!utils.isBrowser() && typeof module.paths !==
|
|
71
|
+
if (!utils.isBrowser() && typeof module.paths !== "undefined") {
|
|
72
72
|
var cwd = utils.cwd();
|
|
73
|
-
module.paths.push(cwd, path.join(cwd,
|
|
73
|
+
module.paths.push(cwd, path.join(cwd, "node_modules"));
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
/**
|
|
@@ -79,34 +79,34 @@ if (!utils.isBrowser() && typeof module.paths !== 'undefined') {
|
|
|
79
79
|
*/
|
|
80
80
|
|
|
81
81
|
exports.utils = utils;
|
|
82
|
-
exports.interfaces = require(
|
|
82
|
+
exports.interfaces = require("./interfaces");
|
|
83
83
|
/**
|
|
84
84
|
* @public
|
|
85
85
|
* @memberof Mocha
|
|
86
86
|
*/
|
|
87
87
|
exports.reporters = builtinReporters;
|
|
88
|
-
exports.Runnable = require(
|
|
89
|
-
exports.Context = require(
|
|
88
|
+
exports.Runnable = require("./runnable");
|
|
89
|
+
exports.Context = require("./context");
|
|
90
90
|
/**
|
|
91
91
|
*
|
|
92
92
|
* @memberof Mocha
|
|
93
93
|
*/
|
|
94
|
-
exports.Runner = require(
|
|
94
|
+
exports.Runner = require("./runner");
|
|
95
95
|
exports.Suite = Suite;
|
|
96
|
-
exports.Hook = require(
|
|
97
|
-
exports.Test = require(
|
|
96
|
+
exports.Hook = require("./hook");
|
|
97
|
+
exports.Test = require("./test");
|
|
98
98
|
|
|
99
99
|
let currentContext;
|
|
100
100
|
exports.afterEach = function (...args) {
|
|
101
101
|
return (currentContext.afterEach || currentContext.teardown).apply(
|
|
102
102
|
this,
|
|
103
|
-
args
|
|
103
|
+
args,
|
|
104
104
|
);
|
|
105
105
|
};
|
|
106
106
|
exports.after = function (...args) {
|
|
107
107
|
return (currentContext.after || currentContext.suiteTeardown).apply(
|
|
108
108
|
this,
|
|
109
|
-
args
|
|
109
|
+
args,
|
|
110
110
|
);
|
|
111
111
|
};
|
|
112
112
|
exports.beforeEach = function (...args) {
|
|
@@ -121,13 +121,13 @@ exports.describe = function (...args) {
|
|
|
121
121
|
exports.describe.only = function (...args) {
|
|
122
122
|
return (currentContext.describe || currentContext.suite).only.apply(
|
|
123
123
|
this,
|
|
124
|
-
args
|
|
124
|
+
args,
|
|
125
125
|
);
|
|
126
126
|
};
|
|
127
127
|
exports.describe.skip = function (...args) {
|
|
128
128
|
return (currentContext.describe || currentContext.suite).skip.apply(
|
|
129
129
|
this,
|
|
130
|
-
args
|
|
130
|
+
args,
|
|
131
131
|
);
|
|
132
132
|
};
|
|
133
133
|
exports.it = function (...args) {
|
|
@@ -159,11 +159,11 @@ exports.run = function (...args) {
|
|
|
159
159
|
* @param {MochaOptions} [options] - Settings object.
|
|
160
160
|
*/
|
|
161
161
|
function Mocha(options = {}) {
|
|
162
|
-
options = {...mocharc, ...options};
|
|
162
|
+
options = { ...mocharc, ...options };
|
|
163
163
|
this.files = [];
|
|
164
164
|
this.options = options;
|
|
165
165
|
// root suite
|
|
166
|
-
this.suite = new exports.Suite(
|
|
166
|
+
this.suite = new exports.Suite("", new exports.Context(), true);
|
|
167
167
|
this._cleanReferencesAfterRun = true;
|
|
168
168
|
this._state = mochaStates.INIT;
|
|
169
169
|
|
|
@@ -172,38 +172,38 @@ function Mocha(options = {}) {
|
|
|
172
172
|
.ui(options.ui)
|
|
173
173
|
.reporter(
|
|
174
174
|
options.reporter,
|
|
175
|
-
options[
|
|
175
|
+
options["reporter-option"] ||
|
|
176
176
|
options.reporterOption ||
|
|
177
|
-
options.reporterOptions // for backwards compatibility
|
|
177
|
+
options.reporterOptions, // for backwards compatibility
|
|
178
178
|
)
|
|
179
179
|
.slow(options.slow)
|
|
180
180
|
.global(options.global);
|
|
181
181
|
|
|
182
182
|
// this guard exists because Suite#timeout does not consider `undefined` to be valid input
|
|
183
|
-
if (typeof options.timeout !==
|
|
183
|
+
if (typeof options.timeout !== "undefined") {
|
|
184
184
|
this.timeout(options.timeout === false ? 0 : options.timeout);
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
if (
|
|
187
|
+
if ("retries" in options) {
|
|
188
188
|
this.retries(options.retries);
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
[
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
192
|
+
"allowUncaught",
|
|
193
|
+
"asyncOnly",
|
|
194
|
+
"bail",
|
|
195
|
+
"checkLeaks",
|
|
196
|
+
"color",
|
|
197
|
+
"delay",
|
|
198
|
+
"diff",
|
|
199
|
+
"dryRun",
|
|
200
|
+
"passOnFailingTestSuite",
|
|
201
|
+
"failZero",
|
|
202
|
+
"forbidOnly",
|
|
203
|
+
"forbidPending",
|
|
204
|
+
"fullTrace",
|
|
205
|
+
"inlineDiffs",
|
|
206
|
+
"invert",
|
|
207
207
|
].forEach(function (opt) {
|
|
208
208
|
if (options[opt]) {
|
|
209
209
|
this[opt]();
|
|
@@ -246,9 +246,9 @@ function Mocha(options = {}) {
|
|
|
246
246
|
|
|
247
247
|
if (
|
|
248
248
|
options.parallel &&
|
|
249
|
-
(typeof options.jobs ===
|
|
249
|
+
(typeof options.jobs === "undefined" || options.jobs > 1)
|
|
250
250
|
) {
|
|
251
|
-
debug(
|
|
251
|
+
debug("attempting to enable parallel mode");
|
|
252
252
|
this.parallelMode(true);
|
|
253
253
|
}
|
|
254
254
|
}
|
|
@@ -302,10 +302,10 @@ Mocha.prototype.addFile = function (file) {
|
|
|
302
302
|
* mocha.reporter('xunit', { output: '/path/to/testspec.xunit.xml' });
|
|
303
303
|
*/
|
|
304
304
|
Mocha.prototype.reporter = function (reporterName, reporterOptions) {
|
|
305
|
-
if (typeof reporterName ===
|
|
305
|
+
if (typeof reporterName === "function") {
|
|
306
306
|
this._reporter = reporterName;
|
|
307
307
|
} else {
|
|
308
|
-
reporterName = reporterName ||
|
|
308
|
+
reporterName = reporterName || "spec";
|
|
309
309
|
var reporter;
|
|
310
310
|
// Try to load a built-in reporter.
|
|
311
311
|
if (builtinReporters[reporterName]) {
|
|
@@ -350,10 +350,10 @@ Mocha.prototype.reporter = function (reporterName, reporterOptions) {
|
|
|
350
350
|
*/
|
|
351
351
|
Mocha.prototype.ui = function (ui) {
|
|
352
352
|
var bindInterface;
|
|
353
|
-
if (typeof ui ===
|
|
353
|
+
if (typeof ui === "function") {
|
|
354
354
|
bindInterface = ui;
|
|
355
355
|
} else {
|
|
356
|
-
ui = ui ||
|
|
356
|
+
ui = ui || "bdd";
|
|
357
357
|
bindInterface = exports.interfaces[ui];
|
|
358
358
|
if (!bindInterface) {
|
|
359
359
|
try {
|
|
@@ -421,7 +421,7 @@ Mocha.prototype.loadFiles = function (fn) {
|
|
|
421
421
|
* .then(() => mocha.run(failures => process.exitCode = failures ? 1 : 0))
|
|
422
422
|
* .catch(() => process.exitCode = 1);
|
|
423
423
|
*/
|
|
424
|
-
Mocha.prototype.loadFilesAsync = function ({esmDecorator} = {}) {
|
|
424
|
+
Mocha.prototype.loadFilesAsync = function ({ esmDecorator } = {}) {
|
|
425
425
|
var self = this;
|
|
426
426
|
var suite = this.suite;
|
|
427
427
|
this.lazyLoadFiles(true);
|
|
@@ -435,7 +435,7 @@ Mocha.prototype.loadFilesAsync = function ({esmDecorator} = {}) {
|
|
|
435
435
|
suite.emit(EVENT_FILE_REQUIRE, resultModule, file, self);
|
|
436
436
|
suite.emit(EVENT_FILE_POST_REQUIRE, global, file, self);
|
|
437
437
|
},
|
|
438
|
-
esmDecorator
|
|
438
|
+
esmDecorator,
|
|
439
439
|
);
|
|
440
440
|
};
|
|
441
441
|
|
|
@@ -450,10 +450,10 @@ Mocha.prototype.loadFilesAsync = function ({esmDecorator} = {}) {
|
|
|
450
450
|
Mocha.unloadFile = function (file) {
|
|
451
451
|
if (utils.isBrowser()) {
|
|
452
452
|
throw createUnsupportedError(
|
|
453
|
-
|
|
453
|
+
"unloadFile() is only supported in a Node.js environment",
|
|
454
454
|
);
|
|
455
455
|
}
|
|
456
|
-
return require(
|
|
456
|
+
return require("./nodejs/file-unloader").unloadFile(file);
|
|
457
457
|
};
|
|
458
458
|
|
|
459
459
|
/**
|
|
@@ -474,9 +474,9 @@ Mocha.unloadFile = function (file) {
|
|
|
474
474
|
Mocha.prototype.unloadFiles = function () {
|
|
475
475
|
if (this._state === mochaStates.DISPOSED) {
|
|
476
476
|
throw createMochaInstanceAlreadyDisposedError(
|
|
477
|
-
|
|
477
|
+
"Mocha instance is already disposed, it cannot be used again.",
|
|
478
478
|
this._cleanReferencesAfterRun,
|
|
479
|
-
this
|
|
479
|
+
this,
|
|
480
480
|
);
|
|
481
481
|
}
|
|
482
482
|
|
|
@@ -606,7 +606,7 @@ Mocha.prototype.cleanReferencesAfterRun = function (cleanReferencesAfterRun) {
|
|
|
606
606
|
Mocha.prototype.dispose = function () {
|
|
607
607
|
if (this._state === mochaStates.RUNNING) {
|
|
608
608
|
throw createMochaInstanceAlreadyRunningError(
|
|
609
|
-
|
|
609
|
+
"Cannot dispose while the mocha instance is still running tests.",
|
|
610
610
|
);
|
|
611
611
|
}
|
|
612
612
|
this.unloadFiles();
|
|
@@ -861,7 +861,7 @@ Mocha.prototype.failZero = function (failZero) {
|
|
|
861
861
|
* @return {Mocha} this
|
|
862
862
|
* @chainable
|
|
863
863
|
*/
|
|
864
|
-
Mocha.prototype.passOnFailingTestSuite = function(passOnFailingTestSuite) {
|
|
864
|
+
Mocha.prototype.passOnFailingTestSuite = function (passOnFailingTestSuite) {
|
|
865
865
|
this.options.passOnFailingTestSuite = passOnFailingTestSuite === true;
|
|
866
866
|
return this;
|
|
867
867
|
};
|
|
@@ -901,8 +901,8 @@ Mocha.prototype.forbidPending = function (forbidPending) {
|
|
|
901
901
|
Mocha.prototype._guardRunningStateTransition = function () {
|
|
902
902
|
if (this._state === mochaStates.RUNNING) {
|
|
903
903
|
throw createMochaInstanceAlreadyRunningError(
|
|
904
|
-
|
|
905
|
-
this
|
|
904
|
+
"Mocha instance is currently running tests, cannot start a next test run until this one is done",
|
|
905
|
+
this,
|
|
906
906
|
);
|
|
907
907
|
}
|
|
908
908
|
if (
|
|
@@ -910,9 +910,9 @@ Mocha.prototype._guardRunningStateTransition = function () {
|
|
|
910
910
|
this._state === mochaStates.REFERENCES_CLEANED
|
|
911
911
|
) {
|
|
912
912
|
throw createMochaInstanceAlreadyDisposedError(
|
|
913
|
-
|
|
913
|
+
"Mocha instance is already disposed, cannot start a new test run. Please create a new mocha instance. Be sure to set disable `cleanReferencesAfterRun` when you want to reuse the same mocha instance for multiple test runs.",
|
|
914
914
|
this._cleanReferencesAfterRun,
|
|
915
|
-
this
|
|
915
|
+
this,
|
|
916
916
|
);
|
|
917
917
|
}
|
|
918
918
|
};
|
|
@@ -924,11 +924,11 @@ Mocha.prototype._guardRunningStateTransition = function () {
|
|
|
924
924
|
* @type string
|
|
925
925
|
* @readonly
|
|
926
926
|
*/
|
|
927
|
-
Object.defineProperty(Mocha.prototype,
|
|
928
|
-
value: require(
|
|
927
|
+
Object.defineProperty(Mocha.prototype, "version", {
|
|
928
|
+
value: require("../package.json").version,
|
|
929
929
|
configurable: false,
|
|
930
930
|
enumerable: true,
|
|
931
|
-
writable: false
|
|
931
|
+
writable: false,
|
|
932
932
|
});
|
|
933
933
|
|
|
934
934
|
/**
|
|
@@ -966,7 +966,7 @@ Mocha.prototype.run = function (fn) {
|
|
|
966
966
|
cleanReferencesAfterRun: this._cleanReferencesAfterRun,
|
|
967
967
|
delay: options.delay,
|
|
968
968
|
dryRun: options.dryRun,
|
|
969
|
-
failZero: options.failZero
|
|
969
|
+
failZero: options.failZero,
|
|
970
970
|
});
|
|
971
971
|
createStatsCollector(runner);
|
|
972
972
|
var reporter = new this._reporter(runner, options);
|
|
@@ -988,30 +988,30 @@ Mocha.prototype.run = function (fn) {
|
|
|
988
988
|
exports.reporters.Base.inlineDiffs = options.inlineDiffs;
|
|
989
989
|
exports.reporters.Base.hideDiff = !options.diff;
|
|
990
990
|
|
|
991
|
-
const done = failures => {
|
|
991
|
+
const done = (failures) => {
|
|
992
992
|
this._previousRunner = runner;
|
|
993
993
|
this._state = this._cleanReferencesAfterRun
|
|
994
994
|
? mochaStates.REFERENCES_CLEANED
|
|
995
995
|
: mochaStates.INIT;
|
|
996
996
|
fn = fn || utils.noop;
|
|
997
|
-
if (typeof reporter.done ===
|
|
997
|
+
if (typeof reporter.done === "function") {
|
|
998
998
|
reporter.done(failures, fn);
|
|
999
999
|
} else {
|
|
1000
1000
|
fn(failures);
|
|
1001
1001
|
}
|
|
1002
1002
|
};
|
|
1003
1003
|
|
|
1004
|
-
const runAsync = async runner => {
|
|
1004
|
+
const runAsync = async (runner) => {
|
|
1005
1005
|
const context =
|
|
1006
1006
|
this.options.enableGlobalSetup && this.hasGlobalSetupFixtures()
|
|
1007
1007
|
? await this.runGlobalSetup(runner)
|
|
1008
1008
|
: {};
|
|
1009
1009
|
const failureCount = await runner.runAsync({
|
|
1010
1010
|
files: this.files,
|
|
1011
|
-
options
|
|
1011
|
+
options,
|
|
1012
1012
|
});
|
|
1013
1013
|
if (this.options.enableGlobalTeardown && this.hasGlobalTeardownFixtures()) {
|
|
1014
|
-
await this.runGlobalTeardown(runner, {context});
|
|
1014
|
+
await this.runGlobalTeardown(runner, { context });
|
|
1015
1015
|
}
|
|
1016
1016
|
return failureCount;
|
|
1017
1017
|
};
|
|
@@ -1035,22 +1035,22 @@ Mocha.prototype.rootHooks = function rootHooks({
|
|
|
1035
1035
|
beforeAll = [],
|
|
1036
1036
|
beforeEach = [],
|
|
1037
1037
|
afterAll = [],
|
|
1038
|
-
afterEach = []
|
|
1038
|
+
afterEach = [],
|
|
1039
1039
|
} = {}) {
|
|
1040
1040
|
beforeAll = utils.castArray(beforeAll);
|
|
1041
1041
|
beforeEach = utils.castArray(beforeEach);
|
|
1042
1042
|
afterAll = utils.castArray(afterAll);
|
|
1043
1043
|
afterEach = utils.castArray(afterEach);
|
|
1044
|
-
beforeAll.forEach(hook => {
|
|
1044
|
+
beforeAll.forEach((hook) => {
|
|
1045
1045
|
this.suite.beforeAll(hook);
|
|
1046
1046
|
});
|
|
1047
|
-
beforeEach.forEach(hook => {
|
|
1047
|
+
beforeEach.forEach((hook) => {
|
|
1048
1048
|
this.suite.beforeEach(hook);
|
|
1049
1049
|
});
|
|
1050
|
-
afterAll.forEach(hook => {
|
|
1050
|
+
afterAll.forEach((hook) => {
|
|
1051
1051
|
this.suite.afterAll(hook);
|
|
1052
1052
|
});
|
|
1053
|
-
afterEach.forEach(hook => {
|
|
1053
|
+
afterEach.forEach((hook) => {
|
|
1054
1054
|
this.suite.afterEach(hook);
|
|
1055
1055
|
});
|
|
1056
1056
|
return this;
|
|
@@ -1073,7 +1073,7 @@ Mocha.prototype.rootHooks = function rootHooks({
|
|
|
1073
1073
|
*/
|
|
1074
1074
|
Mocha.prototype.parallelMode = function parallelMode(enable = true) {
|
|
1075
1075
|
if (utils.isBrowser()) {
|
|
1076
|
-
throw createUnsupportedError(
|
|
1076
|
+
throw createUnsupportedError("parallel mode is only supported in Node.js");
|
|
1077
1077
|
}
|
|
1078
1078
|
const parallel = Boolean(enable);
|
|
1079
1079
|
if (
|
|
@@ -1085,14 +1085,14 @@ Mocha.prototype.parallelMode = function parallelMode(enable = true) {
|
|
|
1085
1085
|
}
|
|
1086
1086
|
if (this._state !== mochaStates.INIT) {
|
|
1087
1087
|
throw createUnsupportedError(
|
|
1088
|
-
|
|
1088
|
+
"cannot change parallel mode after having called run()",
|
|
1089
1089
|
);
|
|
1090
1090
|
}
|
|
1091
1091
|
this.options.parallel = parallel;
|
|
1092
1092
|
|
|
1093
1093
|
// swap Runner class
|
|
1094
1094
|
this._runnerClass = parallel
|
|
1095
|
-
? require(
|
|
1095
|
+
? require("./nodejs/parallel-buffered-runner")
|
|
1096
1096
|
: exports.Runner;
|
|
1097
1097
|
|
|
1098
1098
|
// lazyLoadFiles may have been set `true` otherwise (for ESM loading),
|
|
@@ -1112,7 +1112,7 @@ Mocha.prototype.parallelMode = function parallelMode(enable = true) {
|
|
|
1112
1112
|
*/
|
|
1113
1113
|
Mocha.prototype.lazyLoadFiles = function lazyLoadFiles(enable) {
|
|
1114
1114
|
this._lazyLoadFiles = enable === true;
|
|
1115
|
-
debug(
|
|
1115
|
+
debug("set lazy load to %s", enable);
|
|
1116
1116
|
return this;
|
|
1117
1117
|
};
|
|
1118
1118
|
|
|
@@ -1128,7 +1128,7 @@ Mocha.prototype.lazyLoadFiles = function lazyLoadFiles(enable) {
|
|
|
1128
1128
|
Mocha.prototype.globalSetup = function globalSetup(setupFns = []) {
|
|
1129
1129
|
setupFns = utils.castArray(setupFns);
|
|
1130
1130
|
this.options.globalSetup = setupFns;
|
|
1131
|
-
debug(
|
|
1131
|
+
debug("configured %d global setup functions", setupFns.length);
|
|
1132
1132
|
return this;
|
|
1133
1133
|
};
|
|
1134
1134
|
|
|
@@ -1144,7 +1144,7 @@ Mocha.prototype.globalSetup = function globalSetup(setupFns = []) {
|
|
|
1144
1144
|
Mocha.prototype.globalTeardown = function globalTeardown(teardownFns = []) {
|
|
1145
1145
|
teardownFns = utils.castArray(teardownFns);
|
|
1146
1146
|
this.options.globalTeardown = teardownFns;
|
|
1147
|
-
debug(
|
|
1147
|
+
debug("configured %d global teardown functions", teardownFns.length);
|
|
1148
1148
|
return this;
|
|
1149
1149
|
};
|
|
1150
1150
|
|
|
@@ -1159,11 +1159,11 @@ Mocha.prototype.globalTeardown = function globalTeardown(teardownFns = []) {
|
|
|
1159
1159
|
* @returns {Promise<object>} Context object
|
|
1160
1160
|
*/
|
|
1161
1161
|
Mocha.prototype.runGlobalSetup = async function runGlobalSetup(context = {}) {
|
|
1162
|
-
const {globalSetup} = this.options;
|
|
1162
|
+
const { globalSetup } = this.options;
|
|
1163
1163
|
if (globalSetup && globalSetup.length) {
|
|
1164
|
-
debug(
|
|
1164
|
+
debug("run(): global setup starting");
|
|
1165
1165
|
await this._runGlobalFixtures(globalSetup, context);
|
|
1166
|
-
debug(
|
|
1166
|
+
debug("run(): global setup complete");
|
|
1167
1167
|
}
|
|
1168
1168
|
return context;
|
|
1169
1169
|
};
|
|
@@ -1179,14 +1179,14 @@ Mocha.prototype.runGlobalSetup = async function runGlobalSetup(context = {}) {
|
|
|
1179
1179
|
* @returns {Promise<object>} Context object
|
|
1180
1180
|
*/
|
|
1181
1181
|
Mocha.prototype.runGlobalTeardown = async function runGlobalTeardown(
|
|
1182
|
-
context = {}
|
|
1182
|
+
context = {},
|
|
1183
1183
|
) {
|
|
1184
|
-
const {globalTeardown} = this.options;
|
|
1184
|
+
const { globalTeardown } = this.options;
|
|
1185
1185
|
if (globalTeardown && globalTeardown.length) {
|
|
1186
|
-
debug(
|
|
1186
|
+
debug("run(): global teardown starting");
|
|
1187
1187
|
await this._runGlobalFixtures(globalTeardown, context);
|
|
1188
1188
|
}
|
|
1189
|
-
debug(
|
|
1189
|
+
debug("run(): global teardown complete");
|
|
1190
1190
|
return context;
|
|
1191
1191
|
};
|
|
1192
1192
|
|
|
@@ -1199,7 +1199,7 @@ Mocha.prototype.runGlobalTeardown = async function runGlobalTeardown(
|
|
|
1199
1199
|
*/
|
|
1200
1200
|
Mocha.prototype._runGlobalFixtures = async function _runGlobalFixtures(
|
|
1201
1201
|
fixtureFns = [],
|
|
1202
|
-
context = {}
|
|
1202
|
+
context = {},
|
|
1203
1203
|
) {
|
|
1204
1204
|
for await (const fixtureFn of fixtureFns) {
|
|
1205
1205
|
await fixtureFn.call(context);
|
|
@@ -1229,7 +1229,7 @@ Mocha.prototype.enableGlobalSetup = function enableGlobalSetup(enabled = true) {
|
|
|
1229
1229
|
* @returns {Mocha}
|
|
1230
1230
|
*/
|
|
1231
1231
|
Mocha.prototype.enableGlobalTeardown = function enableGlobalTeardown(
|
|
1232
|
-
enabled = true
|
|
1232
|
+
enabled = true,
|
|
1233
1233
|
) {
|
|
1234
1234
|
this.options.enableGlobalTeardown = Boolean(enabled);
|
|
1235
1235
|
return this;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @module buffered-worker-pool
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
"use strict";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* @typedef {import('workerpool').WorkerPoolOptions} WorkerPoolOptions
|
|
@@ -13,13 +13,13 @@
|
|
|
13
13
|
* @typedef {import('../types.d.ts').SerializedWorkerResult} SerializedWorkerResult
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
const serializeJavascript = require(
|
|
17
|
-
const workerpool = require(
|
|
18
|
-
const {deserialize} = require(
|
|
19
|
-
const debug = require(
|
|
20
|
-
const {createInvalidArgumentTypeError} = require(
|
|
16
|
+
const serializeJavascript = require("serialize-javascript");
|
|
17
|
+
const workerpool = require("workerpool");
|
|
18
|
+
const { deserialize } = require("./serializer");
|
|
19
|
+
const debug = require("debug")("mocha:parallel:buffered-worker-pool");
|
|
20
|
+
const { createInvalidArgumentTypeError } = require("../errors");
|
|
21
21
|
|
|
22
|
-
const WORKER_PATH = require.resolve(
|
|
22
|
+
const WORKER_PATH = require.resolve("./worker.js");
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* A mapping of Mocha `Options` objects to serialized values.
|
|
@@ -36,11 +36,11 @@ let optionsCache = new WeakMap();
|
|
|
36
36
|
*/
|
|
37
37
|
const WORKER_POOL_DEFAULT_OPTS = {
|
|
38
38
|
// use child processes, not worker threads!
|
|
39
|
-
workerType:
|
|
39
|
+
workerType: "process",
|
|
40
40
|
// ensure the same flags sent to `node` for this `mocha` invocation are passed
|
|
41
41
|
// along to children
|
|
42
|
-
forkOpts: {execArgv: process.execArgv},
|
|
43
|
-
maxWorkers: workerpool.cpus - 1
|
|
42
|
+
forkOpts: { execArgv: process.execArgv },
|
|
43
|
+
maxWorkers: workerpool.cpus - 1,
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
/**
|
|
@@ -55,40 +55,40 @@ class BufferedWorkerPool {
|
|
|
55
55
|
constructor(opts = {}) {
|
|
56
56
|
const maxWorkers = Math.max(
|
|
57
57
|
1,
|
|
58
|
-
typeof opts.maxWorkers ===
|
|
58
|
+
typeof opts.maxWorkers === "undefined"
|
|
59
59
|
? WORKER_POOL_DEFAULT_OPTS.maxWorkers
|
|
60
|
-
: opts.maxWorkers
|
|
60
|
+
: opts.maxWorkers,
|
|
61
61
|
);
|
|
62
62
|
|
|
63
63
|
/* istanbul ignore next */
|
|
64
64
|
if (workerpool.cpus < 2) {
|
|
65
65
|
// TODO: decide whether we should warn
|
|
66
66
|
debug(
|
|
67
|
-
|
|
67
|
+
"not enough CPU cores available to run multiple jobs; avoid --parallel on this machine",
|
|
68
68
|
);
|
|
69
69
|
} else if (maxWorkers >= workerpool.cpus) {
|
|
70
70
|
// TODO: decide whether we should warn
|
|
71
71
|
debug(
|
|
72
|
-
|
|
72
|
+
"%d concurrent job(s) requested, but only %d core(s) available",
|
|
73
73
|
maxWorkers,
|
|
74
|
-
workerpool.cpus
|
|
74
|
+
workerpool.cpus,
|
|
75
75
|
);
|
|
76
76
|
}
|
|
77
77
|
/* istanbul ignore next */
|
|
78
78
|
debug(
|
|
79
|
-
|
|
79
|
+
"run(): starting worker pool of max size %d, using node args: %s",
|
|
80
80
|
maxWorkers,
|
|
81
|
-
process.execArgv.join(
|
|
81
|
+
process.execArgv.join(" "),
|
|
82
82
|
);
|
|
83
83
|
|
|
84
84
|
let counter = 0;
|
|
85
|
-
const onCreateWorker = ({forkOpts}) => {
|
|
85
|
+
const onCreateWorker = ({ forkOpts }) => {
|
|
86
86
|
return {
|
|
87
87
|
forkOpts: {
|
|
88
88
|
...forkOpts,
|
|
89
89
|
// adds an incremental id to all workers, which can be useful to allocate resources for each process
|
|
90
|
-
env: {...process.env, MOCHA_WORKER_ID: counter++}
|
|
91
|
-
}
|
|
90
|
+
env: { ...process.env, MOCHA_WORKER_ID: counter++ },
|
|
91
|
+
},
|
|
92
92
|
};
|
|
93
93
|
};
|
|
94
94
|
|
|
@@ -96,7 +96,7 @@ class BufferedWorkerPool {
|
|
|
96
96
|
...WORKER_POOL_DEFAULT_OPTS,
|
|
97
97
|
...opts,
|
|
98
98
|
maxWorkers,
|
|
99
|
-
onCreateWorker
|
|
99
|
+
onCreateWorker,
|
|
100
100
|
};
|
|
101
101
|
this._pool = workerpool.pool(WORKER_PATH, this.options);
|
|
102
102
|
}
|
|
@@ -109,7 +109,7 @@ class BufferedWorkerPool {
|
|
|
109
109
|
*/
|
|
110
110
|
async terminate(force = false) {
|
|
111
111
|
/* istanbul ignore next */
|
|
112
|
-
debug(
|
|
112
|
+
debug("terminate(): terminating with force = %s", force);
|
|
113
113
|
return this._pool.terminate(force);
|
|
114
114
|
}
|
|
115
115
|
|
|
@@ -124,15 +124,15 @@ class BufferedWorkerPool {
|
|
|
124
124
|
* @returns {Promise<SerializedWorkerResult>}
|
|
125
125
|
*/
|
|
126
126
|
async run(filepath, options = {}) {
|
|
127
|
-
if (!filepath || typeof filepath !==
|
|
127
|
+
if (!filepath || typeof filepath !== "string") {
|
|
128
128
|
throw createInvalidArgumentTypeError(
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
"Expected a non-empty filepath",
|
|
130
|
+
"filepath",
|
|
131
|
+
"string",
|
|
132
132
|
);
|
|
133
133
|
}
|
|
134
134
|
const serializedOptions = BufferedWorkerPool.serializeOptions(options);
|
|
135
|
-
const result = await this._pool.exec(
|
|
135
|
+
const result = await this._pool.exec("run", [filepath, serializedOptions]);
|
|
136
136
|
return deserialize(result);
|
|
137
137
|
}
|
|
138
138
|
|
|
@@ -167,14 +167,14 @@ class BufferedWorkerPool {
|
|
|
167
167
|
if (!optionsCache.has(opts)) {
|
|
168
168
|
const serialized = serializeJavascript(opts, {
|
|
169
169
|
unsafe: true, // this means we don't care about XSS
|
|
170
|
-
ignoreFunction: true // do not serialize functions
|
|
170
|
+
ignoreFunction: true, // do not serialize functions
|
|
171
171
|
});
|
|
172
172
|
optionsCache.set(opts, serialized);
|
|
173
173
|
/* istanbul ignore next */
|
|
174
174
|
debug(
|
|
175
|
-
|
|
175
|
+
"serializeOptions(): serialized options %O to: %s",
|
|
176
176
|
opts,
|
|
177
|
-
serialized
|
|
177
|
+
serialized,
|
|
178
178
|
);
|
|
179
179
|
}
|
|
180
180
|
return optionsCache.get(opts);
|