-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from all commits
5ef04be
f28fd67
e9834fd
ebc2a07
c411c30
a6029b9
cc401ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -81,16 +82,21 @@ 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Will this work for packages? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should, This behavior is unchanged in this PR |
||
debug('resolved required file %s to %s', mod, modpath); | ||
} | ||
const requiredModule = require(modpath); | ||
if (type(requiredModule) === 'object' && requiredModule.mochaHooks) { | ||
const requiredModule = await requireOrImport(modpath); | ||
if ( | ||
requiredModule && | ||
typeof requiredModule === 'object' && | ||
requiredModule.mochaHooks | ||
) { | ||
const mochaHooksType = type(requiredModule.mochaHooks); | ||
if (/function$/.test(mochaHooksType) || mochaHooksType === 'object') { | ||
debug('found root hooks in required file %s', mod); | ||
|
@@ -102,8 +108,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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
const url = require('url'); | ||
const path = require('path'); | ||
const url = require('url'); | ||
|
||
const requireOrImport = async file => { | ||
file = path.resolve(file); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved |
||
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. | ||
|
@@ -15,7 +20,7 @@ const requireOrImport = async file => { | |
return require(file); | ||
} catch (err) { | ||
if (err.code === 'ERR_REQUIRE_ESM') { | ||
return import(url.pathToFileURL(file)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it was required for Windows. |
||
return formattedImport(file); | ||
} else { | ||
throw err; | ||
} | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the file in question is a package name, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. |
||
postLoadFunc(file, result); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "type": "module" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const mochaHooks = () => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. awesome |
||
beforeEach() { | ||
console.log('esm beforeEach'); | ||
}, | ||
afterEach() { | ||
console.log('esm afterEach'); | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const mochaHooks = { | ||
beforeAll() { | ||
console.log('mjs beforeAll'); | ||
}, | ||
afterAll() { | ||
console.log('mjs afterAll'); | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
'use strict'; | ||
|
||
var invokeMochaAsync = require('../helpers').invokeMochaAsync; | ||
var utils = require('../../../lib/utils'); | ||
|
||
describe('--require', function() { | ||
describe('when mocha run in serial mode', function() { | ||
|
@@ -45,6 +46,46 @@ describe('--require', function() { | |
/beforeAll[\s\S]+?beforeAll array 1[\s\S]+?beforeAll array 2[\s\S]+?beforeEach[\s\S]+?beforeEach array 1[\s\S]+?beforeEach array 2[\s\S]+?afterEach[\s\S]+?afterEach array 1[\s\S]+?afterEach array 2[\s\S]+?afterAll[\s\S]+?afterAll array 1[\s\S]+?afterAll array 2/ | ||
); | ||
}); | ||
|
||
describe('support ESM when style=module or .mjs extension', function() { | ||
before(function() { | ||
if (!utils.supportsEsModules()) this.skip(); | ||
}); | ||
|
||
it('should run root hooks when provided via mochaHooks', function() { | ||
return expect( | ||
invokeMochaAsync( | ||
[ | ||
'--require=' + | ||
require.resolve( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, see where you commented on None of the tests use it though because the actual CWD isn't very clear in-test. But maybe it is possible to calculate a relative path in test and pass that through as well. Again, that particular functionality is unchanged in this PR |
||
// as object | ||
'../fixtures/options/require/root-hook-defs-esm.fixture.mjs' | ||
), | ||
'--require=' + | ||
require.resolve( | ||
// as function | ||
'../fixtures/options/require/esm/root-hook-defs-esm.fixture.js' | ||
), | ||
'--require=' + | ||
require.resolve( | ||
// mixed with commonjs | ||
'../fixtures/options/require/root-hook-defs-a.fixture.js' | ||
), | ||
require.resolve( | ||
'../fixtures/options/require/root-hook-test.fixture.js' | ||
) | ||
].concat( | ||
+process.versions.node.split('.')[0] >= 13 | ||
? [] | ||
: '--experimental-modules' | ||
) | ||
)[1], | ||
'when fulfilled', | ||
'to contain output', | ||
/mjs beforeAll[\s\S]+?beforeAll[\s\S]+?esm beforeEach[\s\S]+?beforeEach[\s\S]+?esm afterEach[\s\S]+?afterEach[\s\S]+?mjs afterAll[\s\S]+?afterAll/ | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when mocha in parallel mode', function() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that the version here may need changing