Skip to content

Commit 72346e3

Browse files
committed
assert mocha is bundle-able by webpack; fixes #4422
1 parent 1807e05 commit 72346e3

File tree

8 files changed

+1218
-24
lines changed

8 files changed

+1218
-24
lines changed

lib/mocha.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ var builtinReporters = require('./reporters');
1212
var growl = require('./nodejs/growl');
1313
var utils = require('./utils');
1414
var mocharc = require('./mocharc.json');
15-
var errors = require('./errors');
1615
var Suite = require('./suite');
1716
var esmUtils = utils.supportsEsModules(true)
1817
? require('./esm-utils')
1918
: undefined;
2019
var createStatsCollector = require('./stats-collector');
21-
var createInvalidReporterError = errors.createInvalidReporterError;
22-
var createInvalidInterfaceError = errors.createInvalidInterfaceError;
23-
var createMochaInstanceAlreadyDisposedError =
24-
errors.createMochaInstanceAlreadyDisposedError;
25-
var createMochaInstanceAlreadyRunningError =
26-
errors.createMochaInstanceAlreadyRunningError;
20+
const {
21+
createUnsupportedError,
22+
createInvalidInterfaceError,
23+
createInvalidReporterError,
24+
createMochaInstanceAlreadyDisposedError,
25+
createMochaInstanceAlreadyRunningError
26+
} = require('./errors');
2727
var EVENT_FILE_PRE_REQUIRE = Suite.constants.EVENT_FILE_PRE_REQUIRE;
2828
var EVENT_FILE_POST_REQUIRE = Suite.constants.EVENT_FILE_POST_REQUIRE;
2929
var EVENT_FILE_REQUIRE = Suite.constants.EVENT_FILE_REQUIRE;
@@ -445,7 +445,12 @@ Mocha.prototype.loadFilesAsync = function() {
445445
* @param {string} file - Pathname of file to be unloaded.
446446
*/
447447
Mocha.unloadFile = function(file) {
448-
delete require.cache[require.resolve(file)];
448+
if (utils.isBrowser()) {
449+
throw createUnsupportedError(
450+
'unloadFile() is only suported in a Node.js environment'
451+
);
452+
}
453+
return require('./nodejs/file-unloader').unloadFile(file);
449454
};
450455

451456
/**
@@ -1051,9 +1056,7 @@ Mocha.prototype.rootHooks = function rootHooks(hooks) {
10511056
*/
10521057
Mocha.prototype.parallelMode = function parallelMode(enable) {
10531058
if (utils.isBrowser()) {
1054-
throw errors.createUnsupportedError(
1055-
'parallel mode is only supported in Node.js'
1056-
);
1059+
throw createUnsupportedError('parallel mode is only supported in Node.js');
10571060
}
10581061
var parallel = enable === true;
10591062
if (
@@ -1064,7 +1067,7 @@ Mocha.prototype.parallelMode = function parallelMode(enable) {
10641067
return this;
10651068
}
10661069
if (this._state !== mochaStates.INIT) {
1067-
throw errors.createUnsupportedError(
1070+
throw createUnsupportedError(
10681071
'cannot change parallel mode after having called run()'
10691072
);
10701073
}

lib/nodejs/file-unloader.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
/**
4+
* This module should not be in the browser bundle, so it's here.
5+
* @private
6+
* @module
7+
*/
8+
9+
/**
10+
* Deletes a file from the `require` cache.
11+
* @param {string} file - File
12+
*/
13+
exports.unloadFile = file => {
14+
delete require.cache[require.resolve(file)];
15+
};

0 commit comments

Comments
 (0)