Skip to content

Commit

Permalink
assert mocha is bundle-able by webpack; fixes mochajs#4422
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Aug 28, 2020
1 parent 52f07f0 commit 051f9f6
Show file tree
Hide file tree
Showing 8 changed files with 1,218 additions and 24 deletions.
27 changes: 15 additions & 12 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ var builtinReporters = require('./reporters');
var growl = require('./nodejs/growl');
var utils = require('./utils');
var mocharc = require('./mocharc.json');
var errors = require('./errors');
var Suite = require('./suite');
var esmUtils = utils.supportsEsModules(true)
? require('./esm-utils')
: undefined;
var createStatsCollector = require('./stats-collector');
var createInvalidReporterError = errors.createInvalidReporterError;
var createInvalidInterfaceError = errors.createInvalidInterfaceError;
var createMochaInstanceAlreadyDisposedError =
errors.createMochaInstanceAlreadyDisposedError;
var createMochaInstanceAlreadyRunningError =
errors.createMochaInstanceAlreadyRunningError;
const {
createUnsupportedError,
createInvalidInterfaceError,
createInvalidReporterError,
createMochaInstanceAlreadyDisposedError,
createMochaInstanceAlreadyRunningError
} = require('./errors');
var EVENT_FILE_PRE_REQUIRE = Suite.constants.EVENT_FILE_PRE_REQUIRE;
var EVENT_FILE_POST_REQUIRE = Suite.constants.EVENT_FILE_POST_REQUIRE;
var EVENT_FILE_REQUIRE = Suite.constants.EVENT_FILE_REQUIRE;
Expand Down Expand Up @@ -445,7 +445,12 @@ Mocha.prototype.loadFilesAsync = function() {
* @param {string} file - Pathname of file to be unloaded.
*/
Mocha.unloadFile = function(file) {
delete require.cache[require.resolve(file)];
if (utils.isBrowser()) {
throw createUnsupportedError(
'unloadFile() is only suported in a Node.js environment'
);
}
return require('./nodejs/file-unloader').unloadFile(file);
};

/**
Expand Down Expand Up @@ -1051,9 +1056,7 @@ Mocha.prototype.rootHooks = function rootHooks(hooks) {
*/
Mocha.prototype.parallelMode = function parallelMode(enable) {
if (utils.isBrowser()) {
throw errors.createUnsupportedError(
'parallel mode is only supported in Node.js'
);
throw createUnsupportedError('parallel mode is only supported in Node.js');
}
var parallel = enable === true;
if (
Expand All @@ -1064,7 +1067,7 @@ Mocha.prototype.parallelMode = function parallelMode(enable) {
return this;
}
if (this._state !== mochaStates.INIT) {
throw errors.createUnsupportedError(
throw createUnsupportedError(
'cannot change parallel mode after having called run()'
);
}
Expand Down
15 changes: 15 additions & 0 deletions lib/nodejs/file-unloader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

/**
* This module should not be in the browser bundle, so it's here.
* @private
* @module
*/

/**
* Deletes a file from the `require` cache.
* @param {string} file - File
*/
exports.unloadFile = file => {
delete require.cache[require.resolve(file)];
};
Loading

0 comments on commit 051f9f6

Please sign in to comment.