Skip to content

Commit

Permalink
remove mocha.runAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Feb 23, 2020
1 parent 22aad5e commit 6a61d73
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 55 deletions.
13 changes: 4 additions & 9 deletions lib/cli/run-helpers.js
Expand Up @@ -92,7 +92,7 @@ exports.handleRequires = (requires = []) => {
};

/**
* Collect test files and run mocha instance.
* Collect and load test files, then run mocha instance.
* @param {Mocha} mocha - Mocha instance
* @param {Options} [opts] - Command line options
* @param {boolean} [opts.exit] - Whether or not to force-exit after tests are complete
Expand All @@ -101,18 +101,13 @@ exports.handleRequires = (requires = []) => {
* @returns {Promise<Runner>}
* @private
*/
exports.singleRun = (mocha, {exit}, fileCollectParams) => {
exports.singleRun = async (mocha, {exit}, fileCollectParams) => {
const files = collectFiles(fileCollectParams);
debug('running tests with files', files);
mocha.files = files;

return mocha.runAsync().then(exitCode => {
if (exit) {
exitMocha(exitCode);
} else {
exitMochaLater(exitCode);
}
});
await mocha.loadFilesAsync();
return mocha.run(exit ? exitMocha : exitMochaLater);
};

/**
Expand Down
54 changes: 8 additions & 46 deletions lib/mocha.js
Expand Up @@ -325,7 +325,7 @@ Mocha.prototype.loadFiles = function(fn) {
* the test interface functions and will be subject to its cache.
* Supports both CJS and ESM modules.
*
* @private
* @public
* @see {@link Mocha#addFile}
* @see {@link Mocha#run}
* @see {@link Mocha#unloadFiles}
Expand All @@ -335,6 +335,7 @@ Mocha.prototype.loadFiles = function(fn) {
Mocha.prototype.loadFilesAsync = function() {
var self = this;
var suite = this.suite;
this.loadAsync = true;

if (!esmUtils) {
return new Promise(function(resolve) {
Expand Down Expand Up @@ -878,31 +879,22 @@ Object.defineProperty(Mocha.prototype, 'version', {
* already in the `require` cache), make sure to clear them from
* the cache first!
*
* This method supports only CommonJS test files, and not ES modules ones.
* To use ES module (and CommonJS) test files, call Mocha#runAsync instead.
*
* @public
* @see {@link Mocha#runAsync}
* @see {@link Mocha#unloadFiles}
* @see {@link Runner#run}
* @param {DoneCB} [fn] - Callback invoked when test execution completed.
* @return {Runner} runner instance
*/
Mocha.prototype.run = function(fn) {
if (this.files.length) {
if (this.files.length && !this.loadAsync) {
this.loadFiles();
}
return this._startRunning(fn);
};

Mocha.prototype._startRunning = function(fn) {
var self = this;
var suite = self.suite;
var options = self.options;
options.files = self.files;
var suite = this.suite;
var options = this.options;
options.files = this.files;
var runner = new exports.Runner(suite, options.delay);
createStatsCollector(runner);
var reporter = new self._reporter(runner, options);
var reporter = new this._reporter(runner, options);
runner.checkLeaks = options.checkLeaks === true;
runner.fullStackTrace = options.fullTrace;
runner.asyncOnly = options.asyncOnly;
Expand All @@ -916,7 +908,7 @@ Mocha.prototype._startRunning = function(fn) {
runner.globals(options.global);
}
if (options.growl) {
self._growl(runner);
this._growl(runner);
}
if (options.color !== undefined) {
exports.reporters.Base.useColors = options.color;
Expand All @@ -935,33 +927,3 @@ Mocha.prototype._startRunning = function(fn) {

return runner.run(done);
};

/**
* Runs root suite and invokes `fn()` when complete. Supports ES Modules.
*
* @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!
*
* This method supports both ES Modules and CommonJS test files.
*
* @public
* @see {@link Mocha#unloadFiles}
* @see {@link Runner#run}
* @return {Promise<Runner>} runner instance
*/
Mocha.prototype.runAsync = function() {
var loadResult = this.files.length
? this.loadFilesAsync()
: Promise.resolve();

var self = this;
return loadResult.then(function() {
return new Promise(function(resolve) {
self._startRunning(function(result) {
resolve(result);
});
});
});
};

0 comments on commit 6a61d73

Please sign in to comment.