Skip to content

Commit

Permalink
Fixed loading of external reporters in the default configuration
Browse files Browse the repository at this point in the history
* Fixes #189
  • Loading branch information
sgravrock committed Jan 9, 2022
1 parent 80b6c4c commit e2a9380
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ function Loader(options) {

Loader.prototype.load = function(modulePath) {
if ((this.alwaysImport && !modulePath.endsWith('.json')) || modulePath.endsWith('.mjs')) {
// The ES module spec requires import paths to be valid URLs. As of v14,
// Node enforces this on Windows but not on other OSes. On OS X, import
// paths that are URLs must not contain parent directory references.
const url = `file://${this.resolvePath_(modulePath)}`;
return this.import_(url)
let importSpecifier;

if (modulePath.indexOf('/') === -1) {
importSpecifier = modulePath;
} else {
// The ES module spec requires import paths to be valid URLs. As of v14,
// Node enforces this on Windows but not on other OSes. On OS X, import
// paths that are URLs must not contain parent directory references.
importSpecifier = `file://${this.resolvePath_(modulePath)}`;
}

return this.import_(importSpecifier)
.then(
mod => mod.default,
e => {
Expand Down
15 changes: 15 additions & 0 deletions spec/loader_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ describe('loader', function() {
});
});

it('imports non-local modules', async function() {
const payload = {default: {}};
const requireShim = jasmine.createSpy('requireShim');
const importShim = jasmine.createSpy('importShim')
.and.returnValue(Promise.resolve(payload));
const loader = new Loader({requireShim, importShim});
loader.alwaysImport = true;

const result = await loader.load('some-module');

expect(result).toBe(payload.default);
expect(requireShim).not.toHaveBeenCalled();
expect(importShim).toHaveBeenCalledWith('some-module');
});

it('uses require to load JSON files', async function() {
const requireShim = jasmine.createSpy('requireShim')
.and.returnValue(Promise.resolve());
Expand Down

0 comments on commit e2a9380

Please sign in to comment.