Spaces:
Running
Running
File size: 38,799 Bytes
5c2ed06 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 |
'use strict';
/*!
* mocha
* Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
var escapeRe = require('escape-string-regexp');
var path = require('path');
var builtinReporters = require('./reporters');
var growl = require('./nodejs/growl');
var utils = require('./utils');
var mocharc = require('./mocharc.json');
var Suite = require('./suite');
var esmUtils = utils.supportsEsModules(true)
? require('./esm-utils')
: undefined;
var createStatsCollector = require('./stats-collector');
const {
warn,
createInvalidReporterError,
createInvalidInterfaceError,
createMochaInstanceAlreadyDisposedError,
createMochaInstanceAlreadyRunningError,
createUnsupportedError
} = require('./errors');
const {
EVENT_FILE_PRE_REQUIRE,
EVENT_FILE_POST_REQUIRE,
EVENT_FILE_REQUIRE
} = Suite.constants;
var sQuote = utils.sQuote;
var debug = require('debug')('mocha:mocha');
exports = module.exports = Mocha;
/**
* A Mocha instance is a finite state machine.
* These are the states it can be in.
* @private
*/
var mochaStates = utils.defineConstants({
/**
* Initial state of the mocha instance
* @private
*/
INIT: 'init',
/**
* Mocha instance is running tests
* @private
*/
RUNNING: 'running',
/**
* Mocha instance is done running tests and references to test functions and hooks are cleaned.
* You can reset this state by unloading the test files.
* @private
*/
REFERENCES_CLEANED: 'referencesCleaned',
/**
* Mocha instance is disposed and can no longer be used.
* @private
*/
DISPOSED: 'disposed'
});
/**
* To require local UIs and reporters when running in node.
*/
if (!utils.isBrowser() && typeof module.paths !== 'undefined') {
var cwd = utils.cwd();
module.paths.push(cwd, path.join(cwd, 'node_modules'));
}
/**
* Expose internals.
* @private
*/
exports.utils = utils;
exports.interfaces = require('./interfaces');
/**
* @public
* @memberof Mocha
*/
exports.reporters = builtinReporters;
exports.Runnable = require('./runnable');
exports.Context = require('./context');
/**
*
* @memberof Mocha
*/
exports.Runner = require('./runner');
exports.Suite = Suite;
exports.Hook = require('./hook');
exports.Test = require('./test');
let currentContext;
exports.afterEach = function(...args) {
return (currentContext.afterEach || currentContext.teardown).apply(
this,
args
);
};
exports.after = function(...args) {
return (currentContext.after || currentContext.suiteTeardown).apply(
this,
args
);
};
exports.beforeEach = function(...args) {
return (currentContext.beforeEach || currentContext.setup).apply(this, args);
};
exports.before = function(...args) {
return (currentContext.before || currentContext.suiteSetup).apply(this, args);
};
exports.describe = function(...args) {
return (currentContext.describe || currentContext.suite).apply(this, args);
};
exports.describe.only = function(...args) {
return (currentContext.describe || currentContext.suite).only.apply(
this,
args
);
};
exports.describe.skip = function(...args) {
return (currentContext.describe || currentContext.suite).skip.apply(
this,
args
);
};
exports.it = function(...args) {
return (currentContext.it || currentContext.test).apply(this, args);
};
exports.it.only = function(...args) {
return (currentContext.it || currentContext.test).only.apply(this, args);
};
exports.it.skip = function(...args) {
return (currentContext.it || currentContext.test).skip.apply(this, args);
};
exports.xdescribe = exports.describe.skip;
exports.xit = exports.it.skip;
exports.setup = exports.beforeEach;
exports.suiteSetup = exports.before;
exports.suiteTeardown = exports.after;
exports.suite = exports.describe;
exports.teardown = exports.afterEach;
exports.test = exports.it;
exports.run = function(...args) {
return currentContext.run.apply(this, args);
};
/**
* Constructs a new Mocha instance with `options`.
*
* @public
* @class Mocha
* @param {Object} [options] - Settings object.
* @param {boolean} [options.allowUncaught] - Propagate uncaught errors?
* @param {boolean} [options.asyncOnly] - Force `done` callback or promise?
* @param {boolean} [options.bail] - Bail after first test failure?
* @param {boolean} [options.checkLeaks] - Check for global variable leaks?
* @param {boolean} [options.color] - Color TTY output from reporter?
* @param {boolean} [options.delay] - Delay root suite execution?
* @param {boolean} [options.diff] - Show diff on failure?
* @param {string} [options.fgrep] - Test filter given string.
* @param {boolean} [options.forbidOnly] - Tests marked `only` fail the suite?
* @param {boolean} [options.forbidPending] - Pending tests fail the suite?
* @param {boolean} [options.fullTrace] - Full stacktrace upon failure?
* @param {string[]} [options.global] - Variables expected in global scope.
* @param {RegExp|string} [options.grep] - Test filter given regular expression.
* @param {boolean} [options.growl] - Enable desktop notifications?
* @param {boolean} [options.inlineDiffs] - Display inline diffs?
* @param {boolean} [options.invert] - Invert test filter matches?
* @param {boolean} [options.noHighlighting] - Disable syntax highlighting?
* @param {string|constructor} [options.reporter] - Reporter name or constructor.
* @param {Object} [options.reporterOption] - Reporter settings object.
* @param {number} [options.retries] - Number of times to retry failed tests.
* @param {number} [options.slow] - Slow threshold value.
* @param {number|string} [options.timeout] - Timeout threshold value.
* @param {string} [options.ui] - Interface name.
* @param {boolean} [options.parallel] - Run jobs in parallel.
* @param {number} [options.jobs] - Max number of worker processes for parallel runs.
* @param {MochaRootHookObject} [options.rootHooks] - Hooks to bootstrap the root suite with.
* @param {string[]} [options.require] - Pathname of `rootHooks` plugin for parallel runs.
* @param {boolean} [options.isWorker] - Should be `true` if `Mocha` process is running in a worker process.
*/
function Mocha(options = {}) {
options = {...mocharc, ...options};
this.files = [];
this.options = options;
// root suite
this.suite = new exports.Suite('', new exports.Context(), true);
this._cleanReferencesAfterRun = true;
this._state = mochaStates.INIT;
this.grep(options.grep)
.fgrep(options.fgrep)
.ui(options.ui)
.reporter(
options.reporter,
options.reporterOption || options.reporterOptions // reporterOptions was previously the only way to specify options to reporter
)
.slow(options.slow)
.global(options.global);
// this guard exists because Suite#timeout does not consider `undefined` to be valid input
if (typeof options.timeout !== 'undefined') {
this.timeout(options.timeout === false ? 0 : options.timeout);
}
if ('retries' in options) {
this.retries(options.retries);
}
[
'allowUncaught',
'asyncOnly',
'bail',
'checkLeaks',
'color',
'delay',
'diff',
'forbidOnly',
'forbidPending',
'fullTrace',
'growl',
'inlineDiffs',
'invert'
].forEach(function(opt) {
if (options[opt]) {
this[opt]();
}
}, this);
if (options.rootHooks) {
this.rootHooks(options.rootHooks);
}
/**
* The class which we'll instantiate in {@link Mocha#run}. Defaults to
* {@link Runner} in serial mode; changes in parallel mode.
* @memberof Mocha
* @private
*/
this._runnerClass = exports.Runner;
/**
* Whether or not to call {@link Mocha#loadFiles} implicitly when calling
* {@link Mocha#run}. If this is `true`, then it's up to the consumer to call
* {@link Mocha#loadFiles} _or_ {@link Mocha#loadFilesAsync}.
* @private
* @memberof Mocha
*/
this._lazyLoadFiles = false;
/**
* It's useful for a Mocha instance to know if it's running in a worker process.
* We could derive this via other means, but it's helpful to have a flag to refer to.
* @memberof Mocha
* @private
*/
this.isWorker = Boolean(options.isWorker);
this.globalSetup(options.globalSetup)
.globalTeardown(options.globalTeardown)
.enableGlobalSetup(options.enableGlobalSetup)
.enableGlobalTeardown(options.enableGlobalTeardown);
if (
options.parallel &&
(typeof options.jobs === 'undefined' || options.jobs > 1)
) {
debug('attempting to enable parallel mode');
this.parallelMode(true);
}
}
/**
* Enables or disables bailing on the first failure.
*
* @public
* @see [CLI option](../#-bail-b)
* @param {boolean} [bail=true] - Whether to bail on first error.
* @returns {Mocha} this
* @chainable
*/
Mocha.prototype.bail = function(bail) {
this.suite.bail(bail !== false);
return this;
};
/**
* @summary
* Adds `file` to be loaded for execution.
*
* @description
* Useful for generic setup code that must be included within test suite.
*
* @public
* @see [CLI option](../#-file-filedirectoryglob)
* @param {string} file - Pathname of file to be loaded.
* @returns {Mocha} this
* @chainable
*/
Mocha.prototype.addFile = function(file) {
this.files.push(file);
return this;
};
/**
* Sets reporter to `reporter`, defaults to "spec".
*
* @public
* @see [CLI option](../#-reporter-name-r-name)
* @see [Reporters](../#reporters)
* @param {String|Function} reporterName - Reporter name or constructor.
* @param {Object} [reporterOptions] - Options used to configure the reporter.
* @returns {Mocha} this
* @chainable
* @throws {Error} if requested reporter cannot be loaded
* @example
*
* // Use XUnit reporter and direct its output to file
* mocha.reporter('xunit', { output: '/path/to/testspec.xunit.xml' });
*/
Mocha.prototype.reporter = function(reporterName, reporterOptions) {
if (typeof reporterName === 'function') {
this._reporter = reporterName;
} else {
reporterName = reporterName || 'spec';
var reporter;
// Try to load a built-in reporter.
if (builtinReporters[reporterName]) {
reporter = builtinReporters[reporterName];
}
// Try to load reporters from process.cwd() and node_modules
if (!reporter) {
try {
reporter = require(reporterName);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
// Try to load reporters from a path (absolute or relative)
try {
reporter = require(path.resolve(utils.cwd(), reporterName));
} catch (_err) {
_err.code === 'MODULE_NOT_FOUND'
? warn(sQuote(reporterName) + ' reporter not found')
: warn(
sQuote(reporterName) +
' reporter blew up with error:\n' +
err.stack
);
}
} else {
warn(
sQuote(reporterName) + ' reporter blew up with error:\n' + err.stack
);
}
}
}
if (!reporter) {
throw createInvalidReporterError(
'invalid reporter ' + sQuote(reporterName),
reporterName
);
}
this._reporter = reporter;
}
this.options.reporterOption = reporterOptions;
// alias option name is used in public reporters xunit/tap/progress
this.options.reporterOptions = reporterOptions;
return this;
};
/**
* Sets test UI `name`, defaults to "bdd".
*
* @public
* @see [CLI option](../#-ui-name-u-name)
* @see [Interface DSLs](../#interfaces)
* @param {string|Function} [ui=bdd] - Interface name or class.
* @returns {Mocha} this
* @chainable
* @throws {Error} if requested interface cannot be loaded
*/
Mocha.prototype.ui = function(ui) {
var bindInterface;
if (typeof ui === 'function') {
bindInterface = ui;
} else {
ui = ui || 'bdd';
bindInterface = exports.interfaces[ui];
if (!bindInterface) {
try {
bindInterface = require(ui);
} catch (err) {
throw createInvalidInterfaceError(
'invalid interface ' + sQuote(ui),
ui
);
}
}
}
bindInterface(this.suite);
this.suite.on(EVENT_FILE_PRE_REQUIRE, function(context) {
currentContext = context;
});
return this;
};
/**
* Loads `files` prior to execution. Does not support ES Modules.
*
* @description
* The implementation relies on Node's `require` to execute
* the test interface functions and will be subject to its cache.
* Supports only CommonJS modules. To load ES modules, use Mocha#loadFilesAsync.
*
* @private
* @see {@link Mocha#addFile}
* @see {@link Mocha#run}
* @see {@link Mocha#unloadFiles}
* @see {@link Mocha#loadFilesAsync}
* @param {Function} [fn] - Callback invoked upon completion.
*/
Mocha.prototype.loadFiles = function(fn) {
var self = this;
var suite = this.suite;
this.files.forEach(function(file) {
file = path.resolve(file);
suite.emit(EVENT_FILE_PRE_REQUIRE, global, file, self);
suite.emit(EVENT_FILE_REQUIRE, require(file), file, self);
suite.emit(EVENT_FILE_POST_REQUIRE, global, file, self);
});
fn && fn();
};
/**
* Loads `files` prior to execution. Supports Node ES Modules.
*
* @description
* The implementation relies on Node's `require` and `import` to execute
* the test interface functions and will be subject to its cache.
* Supports both CJS and ESM modules.
*
* @public
* @see {@link Mocha#addFile}
* @see {@link Mocha#run}
* @see {@link Mocha#unloadFiles}
* @returns {Promise}
* @example
*
* // loads ESM (and CJS) test files asynchronously, then runs root suite
* mocha.loadFilesAsync()
* .then(() => mocha.run(failures => process.exitCode = failures ? 1 : 0))
* .catch(() => process.exitCode = 1);
*/
Mocha.prototype.loadFilesAsync = function() {
var self = this;
var suite = this.suite;
this.lazyLoadFiles(true);
if (!esmUtils) {
return new Promise(function(resolve) {
self.loadFiles(resolve);
});
}
return esmUtils.loadFilesAsync(
this.files,
function(file) {
suite.emit(EVENT_FILE_PRE_REQUIRE, global, file, self);
},
function(file, resultModule) {
suite.emit(EVENT_FILE_REQUIRE, resultModule, file, self);
suite.emit(EVENT_FILE_POST_REQUIRE, global, file, self);
}
);
};
/**
* Removes a previously loaded file from Node's `require` cache.
*
* @private
* @static
* @see {@link Mocha#unloadFiles}
* @param {string} file - Pathname of file to be unloaded.
*/
Mocha.unloadFile = function(file) {
if (utils.isBrowser()) {
throw createUnsupportedError(
'unloadFile() is only suported in a Node.js environment'
);
}
return require('./nodejs/file-unloader').unloadFile(file);
};
/**
* Unloads `files` from Node's `require` cache.
*
* @description
* This allows required files to be "freshly" reloaded, providing the ability
* to reuse a Mocha instance programmatically.
* Note: does not clear ESM module files from the cache
*
* <strong>Intended for consumers — not used internally</strong>
*
* @public
* @see {@link Mocha#run}
* @returns {Mocha} this
* @chainable
*/
Mocha.prototype.unloadFiles = function() {
if (this._state === mochaStates.DISPOSED) {
throw createMochaInstanceAlreadyDisposedError(
'Mocha instance is already disposed, it cannot be used again.',
this._cleanReferencesAfterRun,
this
);
}
this.files.forEach(function(file) {
Mocha.unloadFile(file);
});
this._state = mochaStates.INIT;
return this;
};
/**
* Sets `grep` filter after escaping RegExp special characters.
*
* @public
* @see {@link Mocha#grep}
* @param {string} str - Value to be converted to a regexp.
* @returns {Mocha} this
* @chainable
* @example
*
* // Select tests whose full title begins with `"foo"` followed by a period
* mocha.fgrep('foo.');
*/
Mocha.prototype.fgrep = function(str) {
if (!str) {
return this;
}
return this.grep(new RegExp(escapeRe(str)));
};
/**
* @summary
* Sets `grep` filter used to select specific tests for execution.
*
* @description
* If `re` is a regexp-like string, it will be converted to regexp.
* The regexp is tested against the full title of each test (i.e., the
* name of the test preceded by titles of each its ancestral suites).
* As such, using an <em>exact-match</em> fixed pattern against the
* test name itself will not yield any matches.
* <br>
* <strong>Previous filter value will be overwritten on each call!</strong>
*
* @public
* @see [CLI option](../#-grep-regexp-g-regexp)
* @see {@link Mocha#fgrep}
* @see {@link Mocha#invert}
* @param {RegExp|String} re - Regular expression used to select tests.
* @return {Mocha} this
* @chainable
* @example
*
* // Select tests whose full title contains `"match"`, ignoring case
* mocha.grep(/match/i);
* @example
*
* // Same as above but with regexp-like string argument
* mocha.grep('/match/i');
* @example
*
* // ## Anti-example
* // Given embedded test `it('only-this-test')`...
* mocha.grep('/^only-this-test$/'); // NO! Use `.only()` to do this!
*/
Mocha.prototype.grep = function(re) {
if (utils.isString(re)) {
// extract args if it's regex-like, i.e: [string, pattern, flag]
var arg = re.match(/^\/(.*)\/(g|i|)$|.*/);
this.options.grep = new RegExp(arg[1] || arg[0], arg[2]);
} else {
this.options.grep = re;
}
return this;
};
/**
* Inverts `grep` matches.
*
* @public
* @see {@link Mocha#grep}
* @return {Mocha} this
* @chainable
* @example
*
* // Select tests whose full title does *not* contain `"match"`, ignoring case
* mocha.grep(/match/i).invert();
*/
Mocha.prototype.invert = function() {
this.options.invert = true;
return this;
};
/**
* Enables or disables checking for global variables leaked while running tests.
*
* @public
* @see [CLI option](../#-check-leaks)
* @param {boolean} [checkLeaks=true] - Whether to check for global variable leaks.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.checkLeaks = function(checkLeaks) {
this.options.checkLeaks = checkLeaks !== false;
return this;
};
/**
* Enables or disables whether or not to dispose after each test run.
* Disable this to ensure you can run the test suite multiple times.
* If disabled, be sure to dispose mocha when you're done to prevent memory leaks.
* @public
* @see {@link Mocha#dispose}
* @param {boolean} cleanReferencesAfterRun
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.cleanReferencesAfterRun = function(cleanReferencesAfterRun) {
this._cleanReferencesAfterRun = cleanReferencesAfterRun !== false;
return this;
};
/**
* Manually dispose this mocha instance. Mark this instance as `disposed` and unable to run more tests.
* It also removes function references to tests functions and hooks, so variables trapped in closures can be cleaned by the garbage collector.
* @public
*/
Mocha.prototype.dispose = function() {
if (this._state === mochaStates.RUNNING) {
throw createMochaInstanceAlreadyRunningError(
'Cannot dispose while the mocha instance is still running tests.'
);
}
this.unloadFiles();
this._previousRunner && this._previousRunner.dispose();
this.suite.dispose();
this._state = mochaStates.DISPOSED;
};
/**
* Displays full stack trace upon test failure.
*
* @public
* @see [CLI option](../#-full-trace)
* @param {boolean} [fullTrace=true] - Whether to print full stacktrace upon failure.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.fullTrace = function(fullTrace) {
this.options.fullTrace = fullTrace !== false;
return this;
};
/**
* Enables desktop notification support if prerequisite software installed.
*
* @public
* @see [CLI option](../#-growl-g)
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.growl = function() {
this.options.growl = this.isGrowlCapable();
if (!this.options.growl) {
var detail = utils.isBrowser()
? 'notification support not available in this browser...'
: 'notification support prerequisites not installed...';
console.error(detail + ' cannot enable!');
}
return this;
};
/**
* @summary
* Determines if Growl support seems likely.
*
* @description
* <strong>Not available when run in browser.</strong>
*
* @private
* @see {@link Growl#isCapable}
* @see {@link Mocha#growl}
* @return {boolean} whether Growl support can be expected
*/
Mocha.prototype.isGrowlCapable = growl.isCapable;
/**
* Implements desktop notifications using a pseudo-reporter.
*
* @private
* @see {@link Mocha#growl}
* @see {@link Growl#notify}
* @param {Runner} runner - Runner instance.
*/
Mocha.prototype._growl = growl.notify;
/**
* Specifies whitelist of variable names to be expected in global scope.
*
* @public
* @see [CLI option](../#-global-variable-name)
* @see {@link Mocha#checkLeaks}
* @param {String[]|String} global - Accepted global variable name(s).
* @return {Mocha} this
* @chainable
* @example
*
* // Specify variables to be expected in global scope
* mocha.global(['jQuery', 'MyLib']);
*/
Mocha.prototype.global = function(global) {
this.options.global = (this.options.global || [])
.concat(global)
.filter(Boolean)
.filter(function(elt, idx, arr) {
return arr.indexOf(elt) === idx;
});
return this;
};
// for backwards compability, 'globals' is an alias of 'global'
Mocha.prototype.globals = Mocha.prototype.global;
/**
* Enables or disables TTY color output by screen-oriented reporters.
*
* @public
* @see [CLI option](../#-color-c-colors)
* @param {boolean} [color=true] - Whether to enable color output.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.color = function(color) {
this.options.color = color !== false;
return this;
};
/**
* Enables or disables reporter to use inline diffs (rather than +/-)
* in test failure output.
*
* @public
* @see [CLI option](../#-inline-diffs)
* @param {boolean} [inlineDiffs=true] - Whether to use inline diffs.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.inlineDiffs = function(inlineDiffs) {
this.options.inlineDiffs = inlineDiffs !== false;
return this;
};
/**
* Enables or disables reporter to include diff in test failure output.
*
* @public
* @see [CLI option](../#-diff)
* @param {boolean} [diff=true] - Whether to show diff on failure.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.diff = function(diff) {
this.options.diff = diff !== false;
return this;
};
/**
* @summary
* Sets timeout threshold value.
*
* @description
* A string argument can use shorthand (such as "2s") and will be converted.
* If the value is `0`, timeouts will be disabled.
*
* @public
* @see [CLI option](../#-timeout-ms-t-ms)
* @see [Timeouts](../#timeouts)
* @param {number|string} msecs - Timeout threshold value.
* @return {Mocha} this
* @chainable
* @example
*
* // Sets timeout to one second
* mocha.timeout(1000);
* @example
*
* // Same as above but using string argument
* mocha.timeout('1s');
*/
Mocha.prototype.timeout = function(msecs) {
this.suite.timeout(msecs);
return this;
};
/**
* Sets the number of times to retry failed tests.
*
* @public
* @see [CLI option](../#-retries-n)
* @see [Retry Tests](../#retry-tests)
* @param {number} retry - Number of times to retry failed tests.
* @return {Mocha} this
* @chainable
* @example
*
* // Allow any failed test to retry one more time
* mocha.retries(1);
*/
Mocha.prototype.retries = function(retry) {
this.suite.retries(retry);
return this;
};
/**
* Sets slowness threshold value.
*
* @public
* @see [CLI option](../#-slow-ms-s-ms)
* @param {number} msecs - Slowness threshold value.
* @return {Mocha} this
* @chainable
* @example
*
* // Sets "slow" threshold to half a second
* mocha.slow(500);
* @example
*
* // Same as above but using string argument
* mocha.slow('0.5s');
*/
Mocha.prototype.slow = function(msecs) {
this.suite.slow(msecs);
return this;
};
/**
* Forces all tests to either accept a `done` callback or return a promise.
*
* @public
* @see [CLI option](../#-async-only-a)
* @param {boolean} [asyncOnly=true] - Whether to force `done` callback or promise.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.asyncOnly = function(asyncOnly) {
this.options.asyncOnly = asyncOnly !== false;
return this;
};
/**
* Disables syntax highlighting (in browser).
*
* @public
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.noHighlighting = function() {
this.options.noHighlighting = true;
return this;
};
/**
* Enables or disables uncaught errors to propagate.
*
* @public
* @see [CLI option](../#-allow-uncaught)
* @param {boolean} [allowUncaught=true] - Whether to propagate uncaught errors.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.allowUncaught = function(allowUncaught) {
this.options.allowUncaught = allowUncaught !== false;
return this;
};
/**
* @summary
* Delays root suite execution.
*
* @description
* Used to perform async operations before any suites are run.
*
* @public
* @see [delayed root suite](../#delayed-root-suite)
* @returns {Mocha} this
* @chainable
*/
Mocha.prototype.delay = function delay() {
this.options.delay = true;
return this;
};
/**
* Causes tests marked `only` to fail the suite.
*
* @public
* @see [CLI option](../#-forbid-only)
* @param {boolean} [forbidOnly=true] - Whether tests marked `only` fail the suite.
* @returns {Mocha} this
* @chainable
*/
Mocha.prototype.forbidOnly = function(forbidOnly) {
this.options.forbidOnly = forbidOnly !== false;
return this;
};
/**
* Causes pending tests and tests marked `skip` to fail the suite.
*
* @public
* @see [CLI option](../#-forbid-pending)
* @param {boolean} [forbidPending=true] - Whether pending tests fail the suite.
* @returns {Mocha} this
* @chainable
*/
Mocha.prototype.forbidPending = function(forbidPending) {
this.options.forbidPending = forbidPending !== false;
return this;
};
/**
* Throws an error if mocha is in the wrong state to be able to transition to a "running" state.
* @private
*/
Mocha.prototype._guardRunningStateTransition = function() {
if (this._state === mochaStates.RUNNING) {
throw createMochaInstanceAlreadyRunningError(
'Mocha instance is currently running tests, cannot start a next test run until this one is done',
this
);
}
if (
this._state === mochaStates.DISPOSED ||
this._state === mochaStates.REFERENCES_CLEANED
) {
throw createMochaInstanceAlreadyDisposedError(
'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.',
this._cleanReferencesAfterRun,
this
);
}
};
/**
* Mocha version as specified by "package.json".
*
* @name Mocha#version
* @type string
* @readonly
*/
Object.defineProperty(Mocha.prototype, 'version', {
value: require('../package.json').version,
configurable: false,
enumerable: true,
writable: false
});
/**
* Callback to be invoked when test execution is complete.
*
* @private
* @callback DoneCB
* @param {number} failures - Number of failures that occurred.
*/
/**
* Runs root suite and invokes `fn()` when complete.
*
* @description
* To run tests multiple times (or to run tests in files that are
* already in the `require` cache), make sure to clear them from
* the cache first!
*
* @public
* @see {@link Mocha#unloadFiles}
* @see {@link Runner#run}
* @param {DoneCB} [fn] - Callback invoked when test execution completed.
* @returns {Runner} runner instance
* @example
*
* // exit with non-zero status if there were test failures
* mocha.run(failures => process.exitCode = failures ? 1 : 0);
*/
Mocha.prototype.run = function(fn) {
this._guardRunningStateTransition();
this._state = mochaStates.RUNNING;
if (this._previousRunner) {
this._previousRunner.dispose();
this.suite.reset();
}
if (this.files.length && !this._lazyLoadFiles) {
this.loadFiles();
}
var suite = this.suite;
var options = this.options;
options.files = this.files;
const runner = new this._runnerClass(suite, {
delay: options.delay,
cleanReferencesAfterRun: this._cleanReferencesAfterRun
});
createStatsCollector(runner);
var reporter = new this._reporter(runner, options);
runner.checkLeaks = options.checkLeaks === true;
runner.fullStackTrace = options.fullTrace;
runner.asyncOnly = options.asyncOnly;
runner.allowUncaught = options.allowUncaught;
runner.forbidOnly = options.forbidOnly;
runner.forbidPending = options.forbidPending;
if (options.grep) {
runner.grep(options.grep, options.invert);
}
if (options.global) {
runner.globals(options.global);
}
if (options.growl) {
this._growl(runner);
}
if (options.color !== undefined) {
exports.reporters.Base.useColors = options.color;
}
exports.reporters.Base.inlineDiffs = options.inlineDiffs;
exports.reporters.Base.hideDiff = !options.diff;
const done = failures => {
this._previousRunner = runner;
this._state = this._cleanReferencesAfterRun
? mochaStates.REFERENCES_CLEANED
: mochaStates.INIT;
fn = fn || utils.noop;
if (typeof reporter.done === 'function') {
reporter.done(failures, fn);
} else {
fn(failures);
}
};
const runAsync = async runner => {
const context =
this.options.enableGlobalSetup && this.hasGlobalSetupFixtures()
? await this.runGlobalSetup(runner)
: {};
const failureCount = await runner.runAsync({
files: this.files,
options
});
if (this.options.enableGlobalTeardown && this.hasGlobalTeardownFixtures()) {
await this.runGlobalTeardown(runner, {context});
}
return failureCount;
};
// no "catch" here is intentional. errors coming out of
// Runner#run are considered uncaught/unhandled and caught
// by the `process` event listeners.
// also: returning anything other than `runner` would be a breaking
// change
runAsync(runner).then(done);
return runner;
};
/**
* Assigns hooks to the root suite
* @param {MochaRootHookObject} [hooks] - Hooks to assign to root suite
* @chainable
*/
Mocha.prototype.rootHooks = function rootHooks({
beforeAll = [],
beforeEach = [],
afterAll = [],
afterEach = []
} = {}) {
beforeAll = utils.castArray(beforeAll);
beforeEach = utils.castArray(beforeEach);
afterAll = utils.castArray(afterAll);
afterEach = utils.castArray(afterEach);
beforeAll.forEach(hook => {
this.suite.beforeAll(hook);
});
beforeEach.forEach(hook => {
this.suite.beforeEach(hook);
});
afterAll.forEach(hook => {
this.suite.afterAll(hook);
});
afterEach.forEach(hook => {
this.suite.afterEach(hook);
});
return this;
};
/**
* Toggles parallel mode.
*
* Must be run before calling {@link Mocha#run}. Changes the `Runner` class to
* use; also enables lazy file loading if not already done so.
*
* Warning: when passed `false` and lazy loading has been enabled _via any means_ (including calling `parallelMode(true)`), this method will _not_ disable lazy loading. Lazy loading is a prerequisite for parallel
* mode, but parallel mode is _not_ a prerequisite for lazy loading!
* @param {boolean} [enable] - If `true`, enable; otherwise disable.
* @throws If run in browser
* @throws If Mocha not in `INIT` state
* @returns {Mocha}
* @chainable
* @public
*/
Mocha.prototype.parallelMode = function parallelMode(enable = true) {
if (utils.isBrowser()) {
throw createUnsupportedError('parallel mode is only supported in Node.js');
}
const parallel = Boolean(enable);
if (
parallel === this.options.parallel &&
this._lazyLoadFiles &&
this._runnerClass !== exports.Runner
) {
return this;
}
if (this._state !== mochaStates.INIT) {
throw createUnsupportedError(
'cannot change parallel mode after having called run()'
);
}
this.options.parallel = parallel;
// swap Runner class
this._runnerClass = parallel
? require('./nodejs/parallel-buffered-runner')
: exports.Runner;
// lazyLoadFiles may have been set `true` otherwise (for ESM loading),
// so keep `true` if so.
return this.lazyLoadFiles(this._lazyLoadFiles || parallel);
};
/**
* Disables implicit call to {@link Mocha#loadFiles} in {@link Mocha#run}. This
* setting is used by watch mode, parallel mode, and for loading ESM files.
* @todo This should throw if we've already loaded files; such behavior
* necessitates adding a new state.
* @param {boolean} [enable] - If `true`, disable eager loading of files in
* {@link Mocha#run}
* @chainable
* @public
*/
Mocha.prototype.lazyLoadFiles = function lazyLoadFiles(enable) {
this._lazyLoadFiles = enable === true;
debug('set lazy load to %s', enable);
return this;
};
/**
* Configures one or more global setup fixtures.
*
* If given no parameters, _unsets_ any previously-set fixtures.
* @chainable
* @public
* @param {MochaGlobalFixture|MochaGlobalFixture[]} [setupFns] - Global setup fixture(s)
* @returns {Mocha}
*/
Mocha.prototype.globalSetup = function globalSetup(setupFns = []) {
setupFns = utils.castArray(setupFns);
this.options.globalSetup = setupFns;
debug('configured %d global setup functions', setupFns.length);
return this;
};
/**
* Configures one or more global teardown fixtures.
*
* If given no parameters, _unsets_ any previously-set fixtures.
* @chainable
* @public
* @param {MochaGlobalFixture|MochaGlobalFixture[]} [teardownFns] - Global teardown fixture(s)
* @returns {Mocha}
*/
Mocha.prototype.globalTeardown = function globalTeardown(teardownFns = []) {
teardownFns = utils.castArray(teardownFns);
this.options.globalTeardown = teardownFns;
debug('configured %d global teardown functions', teardownFns.length);
return this;
};
/**
* Run any global setup fixtures sequentially, if any.
*
* This is _automatically called_ by {@link Mocha#run} _unless_ the `runGlobalSetup` option is `false`; see {@link Mocha#enableGlobalSetup}.
*
* The context object this function resolves with should be consumed by {@link Mocha#runGlobalTeardown}.
* @param {object} [context] - Context object if already have one
* @public
* @returns {Promise<object>} Context object
*/
Mocha.prototype.runGlobalSetup = async function runGlobalSetup(context = {}) {
const {globalSetup} = this.options;
if (globalSetup && globalSetup.length) {
debug('run(): global setup starting');
await this._runGlobalFixtures(globalSetup, context);
debug('run(): global setup complete');
}
return context;
};
/**
* Run any global teardown fixtures sequentially, if any.
*
* This is _automatically called_ by {@link Mocha#run} _unless_ the `runGlobalTeardown` option is `false`; see {@link Mocha#enableGlobalTeardown}.
*
* Should be called with context object returned by {@link Mocha#runGlobalSetup}, if applicable.
* @param {object} [context] - Context object if already have one
* @public
* @returns {Promise<object>} Context object
*/
Mocha.prototype.runGlobalTeardown = async function runGlobalTeardown(
context = {}
) {
const {globalTeardown} = this.options;
if (globalTeardown && globalTeardown.length) {
debug('run(): global teardown starting');
await this._runGlobalFixtures(globalTeardown, context);
}
debug('run(): global teardown complete');
return context;
};
/**
* Run global fixtures sequentially with context `context`
* @private
* @param {MochaGlobalFixture[]} [fixtureFns] - Fixtures to run
* @param {object} [context] - context object
* @returns {Promise<object>} context object
*/
Mocha.prototype._runGlobalFixtures = async function _runGlobalFixtures(
fixtureFns = [],
context = {}
) {
for await (const fixtureFn of fixtureFns) {
await fixtureFn.call(context);
}
return context;
};
/**
* Toggle execution of any global setup fixture(s)
*
* @chainable
* @public
* @param {boolean } [enabled=true] - If `false`, do not run global setup fixture
* @returns {Mocha}
*/
Mocha.prototype.enableGlobalSetup = function enableGlobalSetup(enabled = true) {
this.options.enableGlobalSetup = Boolean(enabled);
return this;
};
/**
* Toggle execution of any global teardown fixture(s)
*
* @chainable
* @public
* @param {boolean } [enabled=true] - If `false`, do not run global teardown fixture
* @returns {Mocha}
*/
Mocha.prototype.enableGlobalTeardown = function enableGlobalTeardown(
enabled = true
) {
this.options.enableGlobalTeardown = Boolean(enabled);
return this;
};
/**
* Returns `true` if one or more global setup fixtures have been supplied.
* @public
* @returns {boolean}
*/
Mocha.prototype.hasGlobalSetupFixtures = function hasGlobalSetupFixtures() {
return Boolean(this.options.globalSetup.length);
};
/**
* Returns `true` if one or more global teardown fixtures have been supplied.
* @public
* @returns {boolean}
*/
Mocha.prototype.hasGlobalTeardownFixtures = function hasGlobalTeardownFixtures() {
return Boolean(this.options.globalTeardown.length);
};
/**
* An alternative way to define root hooks that works with parallel runs.
* @typedef {Object} MochaRootHookObject
* @property {Function|Function[]} [beforeAll] - "Before all" hook(s)
* @property {Function|Function[]} [beforeEach] - "Before each" hook(s)
* @property {Function|Function[]} [afterAll] - "After all" hook(s)
* @property {Function|Function[]} [afterEach] - "After each" hook(s)
*/
/**
* An function that returns a {@link MochaRootHookObject}, either sync or async.
@callback MochaRootHookFunction
* @returns {MochaRootHookObject|Promise<MochaRootHookObject>}
*/
/**
* A function that's invoked _once_ which is either sync or async.
* Can be a "teardown" or "setup". These will all share the same context.
* @callback MochaGlobalFixture
* @returns {void|Promise<void>}
*/
/**
* An object making up all necessary parts of a plugin loader and aggregator
* @typedef {Object} PluginDefinition
* @property {string} exportName - Named export to use
* @property {string} [optionName] - Option name for Mocha constructor (use `exportName` if omitted)
* @property {PluginValidator} [validate] - Validator function
* @property {PluginFinalizer} [finalize] - Finalizer/aggregator function
*/
/**
* A (sync) function to assert a user-supplied plugin implementation is valid.
*
* Defined in a {@link PluginDefinition}.
* @callback PluginValidator
* @param {*} value - Value to check
* @this {PluginDefinition}
* @returns {void}
*/
/**
* A function to finalize plugins impls of a particular ilk
* @callback PluginFinalizer
* @param {Array<*>} impls - User-supplied implementations
* @returns {Promise<*>|*}
*/
|