Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support --require of ESM; closes #4281 #4304

Merged
merged 7 commits into from Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/cli/run-helpers.js
Expand Up @@ -15,6 +15,7 @@ const collectFiles = require('./collect-files');
const {type} = require('../utils');
const {format} = require('util');
const {createInvalidPluginError, createUnsupportedError} = require('../errors');
const {requireOrImport} = require('../esm-utils');

/**
* Exits Mocha when tests + code under test has finished execution (default)
Expand Down Expand Up @@ -81,15 +82,16 @@ exports.list = str =>
* @returns {Promise<MochaRootHookObject|MochaRootHookFunction>} Any root hooks
* @private
*/
exports.handleRequires = async (requires = []) =>
requires.reduce((acc, mod) => {
exports.handleRequires = async (requires = []) => {
const acc = [];
for (const mod of requires) {
let modpath = mod;
// this is relative to cwd
if (fs.existsSync(mod) || fs.existsSync(`${mod}.js`)) {
modpath = path.resolve(mod);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the file in question is a package name, path.resolve will fail [...]

Will this work for packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should, fs.existsSync will return false for a package name, so it is not resolved at all (passed raw) to requireOrImport (e.g. requireOrImport('@babel/register')

This behavior is unchanged in this PR

debug('resolved required file %s to %s', mod, modpath);
}
const requiredModule = require(modpath);
const requiredModule = await requireOrImport(modpath);
if (type(requiredModule) === 'object' && requiredModule.mochaHooks) {
const mochaHooksType = type(requiredModule.mochaHooks);
if (/function$/.test(mochaHooksType) || mochaHooksType === 'object') {
Expand All @@ -102,8 +104,9 @@ exports.handleRequires = async (requires = []) =>
}
}
debug('loaded required module "%s"', mod);
return acc;
}, []);
}
return acc;
};

/**
* Loads root hooks as exported via `mochaHooks` from required files.
Expand Down
17 changes: 11 additions & 6 deletions lib/esm-utils.js
@@ -1,11 +1,16 @@
const url = require('url');
const path = require('path');
const url = require('url');

const requireOrImport = async file => {
file = path.resolve(file);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved resolve() to loadFilesAsync so the file doesn't have to be a fully-resolved name, but can be a package from node_modules as well.

const formattedImport = async file => {
if (path.isAbsolute(file)) {
return import(url.pathToFileURL(file));
}
return import(file);
};

exports.requireOrImport = async file => {
if (path.extname(file) === '.mjs') {
return import(url.pathToFileURL(file));
return formattedImport(file);
}
// This is currently the only known way of figuring out whether a file is CJS or ESM.
// If Node.js or the community establish a better procedure for that, we can fix this code.
Expand All @@ -15,7 +20,7 @@ const requireOrImport = async file => {
return require(file);
} catch (err) {
if (err.code === 'ERR_REQUIRE_ESM') {
return import(url.pathToFileURL(file));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what url.pathToFileURL was achieving?
import() allows a plain string, and this way the required file can still be resolvable via node's logic (e.g. loading from node_modules)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it was required for Windows.
Gosh I hate windows

return formattedImport(file);
} else {
throw err;
}
Expand All @@ -25,7 +30,7 @@ const requireOrImport = async file => {
exports.loadFilesAsync = async (files, preLoadFunc, postLoadFunc) => {
for (const file of files) {
preLoadFunc(file);
const result = await requireOrImport(file);
const result = await exports.requireOrImport(path.resolve(file));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the file in question is a package name, path.resolve will fail (AFAIK with an exception). Why did you add a path.resolve here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. --require-d files can be module names, and so you didn't want requireOrImport, which is now used also in handleRequire, to resolve them to a path. So you moved it to loadFileAsync, where it is needed (for Windows, it seems...)

postLoadFunc(file, result);
}
};