Skip to content

fix: Fail with an informative error message on a file with a broken default import #5413

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/nodejs/esm-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('node:path');
const url = require('node:url');
const debug = require('debug')('mocha:esm-utils');

const forward = x => x;

Expand Down Expand Up @@ -93,11 +94,20 @@ const requireModule = async (file, esmDecorator) => {
try {
return require(file);
} catch (err) {
// Import if require fails.
return dealWithExports(await formattedImport(file, esmDecorator));
debug('requireModule caught err: %O', err.message);
if (
err instanceof SyntaxError &&
err.message.includes("does not provide an export named 'default'")
) {
throw err;
} else {
// Import if require fails.
return dealWithExports(await formattedImport(file, esmDecorator));
}
}
}

debug('assigning requireOrImport, require_module === %O', process.features.require_module);
if (process.features.require_module) {
exports.requireOrImport = requireModule;
} else {
Expand Down
29 changes: 23 additions & 6 deletions test/node-unit/esm-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@ const sinon = require('sinon');
const url = require('node:url');

describe('esm-utils', function () {
beforeEach(function () {
sinon.stub(esmUtils, 'doImport').resolves({});
});

afterEach(function () {
sinon.restore();
describe('requireOrImport', function () {
it('should show an informative error message for a broken default import', async function () {
return expect(
() =>
esmUtils.requireOrImport(
'../../test/node-unit/fixtures/broken-default-import.mjs'
),
'to be rejected with error satisfying',
{
name: 'SyntaxError',
message:
"The requested module './module-without-default-export.mjs' does not provide an export named 'default'"
}
);
});
});

describe('loadFilesAsync()', function () {
beforeEach(function () {
sinon.stub(esmUtils, 'doImport').resolves({});
});

afterEach(function () {
sinon.restore();
});

it('should not decorate imported module if no decorator passed', async function () {
await esmUtils.loadFilesAsync(
['/foo/bar.mjs'],
Expand Down
3 changes: 3 additions & 0 deletions test/node-unit/fixtures/broken-default-import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import moduleWithoutDefaultExport from './module-without-default-export.mjs';

moduleWithoutDefaultExport;
1 change: 1 addition & 0 deletions test/node-unit/fixtures/module-without-default-export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 42;