-
-
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
WIP: use dynamic import to load es module tests. #3253
Changes from all commits
d46d557
a7d5262
10736aa
b28d2db
e1ad789
a16f8d0
a494924
666a25f
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 |
---|---|---|
|
@@ -234,6 +234,34 @@ Mocha.prototype.loadFiles = function (fn) { | |
fn && fn(); | ||
}; | ||
|
||
/** | ||
* Load registered files via es6 dynamic imports. | ||
* | ||
* Note: `eval` is used in this function or else mocha will not | ||
* compile on older node versions. | ||
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 don't understand where this is supposed to work. 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. The new 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. right, but I'm unclear on why we're removing old versions of Node.js from the matrix.
if we're going to break backwards compatibility, then it'd have to wait until the oldest version of Node.js that supports modules is no longer maintained. that'll be a few years. so, that's why I'm trying to understand what I'm supposed to do with this... 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. Sorry, I didn't mean to add that change to the pull request, I will revert it. The idea was that mocha could add a new CLI flag which would cause mocha to use the new This new CLI flag could only be used on the most recent versions of node but mocha would work normally if this flag was not set. The tests fail because I added a test for the new flag and travis/appveyor runs this test on old versions of node, I didn't know how to work around that. |
||
* | ||
* @api private | ||
*/ | ||
Mocha.prototype.dynamicallyImportFiles = function (fn) { | ||
var self = this; | ||
var suite = this.suite; | ||
Promise | ||
.all(this.files.map(function (file) { | ||
file = path.resolve(file); | ||
suite.emit('pre-require', global, file, self); | ||
return eval('import(file)') | ||
.then(function (module) { | ||
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. probably shouldn't use |
||
suite.emit('require', module, file, self); | ||
suite.emit('post-require', global, file, self); | ||
return module; | ||
}); | ||
})) | ||
.then(function() { | ||
fn(); | ||
}) | ||
.catch(fn); | ||
}; | ||
|
||
/** | ||
* Enable growl support. | ||
* | ||
|
@@ -334,6 +362,17 @@ Mocha.prototype.fullTrace = function () { | |
return this; | ||
}; | ||
|
||
/** | ||
* Use dynamic imports to load tests as es modules. | ||
* | ||
* @return {Mocha} | ||
* @api public | ||
*/ | ||
Mocha.prototype.esModules = function () { | ||
this.options.esModules = true; | ||
return this; | ||
}; | ||
|
||
/** | ||
* Enable growl support. | ||
* | ||
|
@@ -532,9 +571,6 @@ Mocha.prototype.forbidPending = function () { | |
* @return {Runner} | ||
*/ | ||
Mocha.prototype.run = function (fn) { | ||
if (this.files.length) { | ||
this.loadFiles(); | ||
} | ||
var suite = this.suite; | ||
var options = this.options; | ||
options.files = this.files; | ||
|
@@ -569,5 +605,23 @@ Mocha.prototype.run = function (fn) { | |
} | ||
} | ||
|
||
return runner.run(done); | ||
if (this.files.length) { | ||
if (this.options.esModules) { | ||
this.dynamicallyImportFiles(function (error) { | ||
if (error) { | ||
console.log(error); | ||
done(1); | ||
} else { | ||
runner.run(done); | ||
} | ||
}); | ||
} else { | ||
this.loadFiles(); | ||
runner.run(done); | ||
} | ||
} else { | ||
runner.run(done); | ||
} | ||
|
||
return runner; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
var run = require('./helpers').runMocha; | ||
var assert = require('assert'); | ||
|
||
describe('esModules', function () { | ||
describe('loading of es module tests', function () { | ||
this.timeout(1000); | ||
it('should load tests which use named exports', function (done) { | ||
run('es-modules/named-export.fixture.mjs', ['--es-modules', '--experimental-modules', '--harmony-dynamic-import'], function (err, res) { | ||
if (err) { | ||
done(err); | ||
return; | ||
} | ||
assert.equal(res.pending, 0); | ||
assert.equal(res.passing, 1); | ||
assert.equal(res.failing, 0); | ||
assert.equal(res.code, 0); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should load tests which use default exports', function (done) { | ||
this.timeout(1000); | ||
run('es-modules/default-export.fixture.mjs', ['--es-modules', '--experimental-modules', '--harmony-dynamic-import'], function (err, res) { | ||
if (err) { | ||
done(err); | ||
return; | ||
} | ||
assert.equal(res.pending, 0); | ||
assert.equal(res.passing, 1); | ||
assert.equal(res.failing, 0); | ||
assert.equal(res.code, 0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('loading of cjs tests when running with --experimental-modules and --dynamic-import', function () { | ||
it('should load cjs test', function (done) { | ||
this.timeout(1000); | ||
run('es-modules/cjs-test.fixture.js', ['--es-modules', '--experimental-modules', '--harmony-dynamic-import'], function (err, res) { | ||
if (err) { | ||
done(err); | ||
return; | ||
} | ||
assert.equal(res.pending, 0); | ||
assert.equal(res.passing, 1); | ||
assert.equal(res.failing, 0); | ||
assert.equal(res.code, 0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
exports.cjs = 'cjs'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var cjs = require('./cjs-module').cjs; | ||
|
||
|
||
describe('testing common js require', function () { | ||
it('should be able to require cjs modules', function () { | ||
assert(cjs, 'cjs'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import double from './module.mjs' | ||
|
||
describe('testing imported function', function () { | ||
it('imported value should double its argument', function () { | ||
assert(double(5), 10); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const double = x => 2*x; | ||
|
||
export default double; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { double } from './module.mjs' | ||
|
||
describe('testing imported function', function () { | ||
it('imported value should double its argument', function () { | ||
assert(double(5), 10); | ||
}); | ||
}); |
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.
why is this necessary?