-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixes #150. * Specs and helpers with names ending in .mjs are loaded as ES modules (`import("foo.mjs")`). * All other specs and helpers are loaded as CommonJS modules, as before (`require("foo.js")`). * If using ES modules with Node 10 or 11, run `node --experimental-modules /path/to/jasmine` instead of `jasmine`.
- Loading branch information
Showing
17 changed files
with
246 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module.exports = Loader; | ||
|
||
function Loader(options) { | ||
options = options || {}; | ||
this.require_ = options.requireShim || requireShim; | ||
this.import_ = options.importShim || importShim; | ||
} | ||
|
||
Loader.prototype.load = function(path) { | ||
if (path.endsWith('.mjs')) { | ||
return this.import_(path); | ||
} else { | ||
return new Promise(resolve => { | ||
this.require_(path); | ||
resolve(); | ||
}); | ||
} | ||
}; | ||
|
||
function requireShim(path) { | ||
require(path); | ||
} | ||
|
||
function importShim(path) { | ||
return import(path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const child_process = require('child_process'); | ||
|
||
|
||
describe('ES module support', function() { | ||
it('supports ES modules', function(done) { | ||
const child = child_process.spawn( | ||
'node', | ||
['--experimental-modules', '../../../bin/jasmine.js', '--config=jasmine.json'], | ||
{ | ||
cwd: 'spec/fixtures/esm', | ||
shell: false | ||
} | ||
); | ||
let output = ''; | ||
child.stdout.on('data', function(data) { | ||
output += data; | ||
}); | ||
child.stderr.on('data', function(data) { | ||
output += data; | ||
}); | ||
child.on('close', function(exitCode) { | ||
expect(exitCode).toEqual(0); | ||
// Node < 14 outputs a warning when ES modules are used, e.g.: | ||
// (node:5258) ExperimentalWarning: The ESM module loader is experimental. | ||
// The position of this warning in the output varies. Sometimes it | ||
// occurs before the lines we're interested in but sometimes it's in | ||
// the middle of them. | ||
output = output.replace(/^.*ExperimentalWarning.*$\n/m, ''); | ||
expect(output).toContain( | ||
'name_reporter\n' + | ||
'commonjs_helper\n' + | ||
'esm_helper\n' + | ||
'Started\n' + | ||
'Spec: A spec file ending in .js is required as a commonjs module\n' + | ||
'.Spec: A spec file ending in .mjs is imported as an es module\n' | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
console.log('commonjs_helper'); | ||
require('./commonjs_sentinel'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// An empty module that we can require, to see if require works. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('A spec file ending in .js', function() { | ||
it('is required as a commonjs module', function() { | ||
require('./commonjs_sentinel'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import './esm_sentinel.mjs'; | ||
console.log('esm_helper'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// An empty module that will fail if loaded via require(), due to its extension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
describe('A spec file ending in .mjs', function() { | ||
it('is imported as an es module', function() { | ||
// Node probably threw already if we tried to require this file, | ||
// but check anyway just to be sure. | ||
expect(function() { | ||
require('./commonjs_sentinel'); | ||
}).toThrowError(/require is not defined/); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"spec_dir": ".", | ||
"spec_files": [ | ||
"commonjs_spec.js", | ||
"esm_spec.mjs" | ||
], | ||
"helpers": [ | ||
"name_reporter.js", | ||
"commonjs_helper.js", | ||
"esm_helper.mjs" | ||
], | ||
"stopSpecOnExpectationFailure": false, | ||
"random": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
console.log('name_reporter'); | ||
|
||
beforeAll(function() { | ||
jasmine.getEnv().addReporter({ | ||
specStarted: function (event) { | ||
console.log('Spec:', event.fullName); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
global.require_tester_was_loaded = true; | ||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.