-
-
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 1 commit
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 |
---|---|---|
@@ -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,51 @@ 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' | ||
) | ||
], | ||
{ | ||
env: { | ||
...process.env, | ||
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. This is what I updated the parser options (parserOptions: 2018) in eslintrc for. Considering mocha is documented with:
This seems safe enough for tests at least. If there is an easier way to pass 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 apologize for the state of things (we're working on it; see #4293), but... I wanted to get a subset of the integration tests ( However, stuff in 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. Here, you're running e.g. invokeMocha(['path/to/fixture', '--some-other-flag'].concat(+process.versions.node.split('.')[0] >= 13 ? [] : ['--experimental-modules'], function() {}) I realize this means you can just revert the change to 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. Reverted change to eslintrc Didn't realize I could pass the flag directly though mocha! Since I don't need any lint changes, I don't think this is the place to reorganize what is allowed where. |
||
NODE_OPTIONS: | ||
+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.
node -e "import('./test/integration/fixtures/options/require/root-hook-defs-esm.fixture.mjs').then(mod => console.log(Object.prototype.toString.call(mod)))"
->
[object Module]
Piped through the
type
function: https://github.com/mochajs/mocha/blob/master/lib/utils.js#L216Returns
module
.So added handling for that "module" case (which seems reasonable considering that is what we would expect from ESM... but for the sake of discussing alternatives, could add some handling to
type()
to map module -> objectThere 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.
no, this seems correct. but, we should also add a test in
test/node-unit/utils.spec.js
to cover the behavior. you don't need to do this unless you want to.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.
we can change it to return
object
if there are unforeseen consequences. what doestypeof requiredModule
return?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.
node -e "import('./test/integration/fixtures/options/require/root-hook-defs-esm.fixture.mjs').then(mod => console.log(typeof mod))"
->object
I'll let you interpret that... since there must be some reason not to use
typeof
directly.I can add that as well, so long as we are fine with Modules returning "module"
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.
there's no real great reason to use
type()
here other than trying to maintain some consistency. we should usetypeof
and not worry about it.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.
Ref: #4306