Skip to content

Instantly share code, notes, and snippets.

@tadd
Last active September 29, 2019 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadd/756d21bad38933c179f10e59bddee6b4 to your computer and use it in GitHub Desktop.
Save tadd/756d21bad38933c179f10e59bddee6b4 to your computer and use it in GitHub Desktop.
Short patch to load *.mjs from Mocha; run like `mocha test/*.mjs`
--- a/node_modules/mocha/lib/mocha.js
+++ b/node_modules/mocha/lib/mocha.js
@@ -321,15 +321,15 @@ Mocha.prototype.ui = function(ui) {
* @see {@link Mocha#unloadFiles}
* @param {Function} [fn] - Callback invoked upon completion.
*/
-Mocha.prototype.loadFiles = function(fn) {
+Mocha.prototype.loadFiles = async function(fn) {
var self = this;
var suite = this.suite;
- this.files.forEach(function(file) {
+ for await (let file of this.files) {
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_REQUIRE, await import(file), file, self);
suite.emit(EVENT_FILE_POST_REQUIRE, global, file, self);
- });
+ }
fn && fn();
};
@@ -799,9 +799,9 @@ Object.defineProperty(Mocha.prototype, 'version', {
* @param {DoneCB} [fn] - Callback invoked when test execution completed.
* @return {Runner} runner instance
*/
-Mocha.prototype.run = function(fn) {
+Mocha.prototype.run = async function(fn) {
if (this.files.length) {
- this.loadFiles();
+ await this.loadFiles();
}
var suite = this.suite;
var options = this.options;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment