Skip to content

Commit

Permalink
Make "-r <dir>" work with new import-first loading (#4668)
Browse files Browse the repository at this point in the history
  • Loading branch information
giltayar authored Jun 28, 2021
1 parent f033ff1 commit 21cda3e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/esm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ exports.requireOrImport = hasStableEsmImplementation
} catch (err) {
if (
err.code === 'ERR_MODULE_NOT_FOUND' ||
err.code === 'ERR_UNKNOWN_FILE_EXTENSION'
err.code === 'ERR_UNKNOWN_FILE_EXTENSION' ||
err.code === 'ERR_UNSUPPORTED_DIR_IMPORT'
) {
return require(file);
} else {
Expand Down
16 changes: 16 additions & 0 deletions test/integration/esm.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var path = require('path');
var helpers = require('./helpers');
var run = helpers.runMochaJSON;
var runMochaAsync = helpers.runMochaAsync;
Expand Down Expand Up @@ -65,4 +66,19 @@ describe('esm', function() {
done();
});
});

it('should enable requiring/loading a cjs module with "dir" as filename', async function() {
var fixture = 'esm/test-that-uses-dir-cjs-require.fixture.js';
const result = await runMochaAsync(
fixture,
[
...args,
'--require',
path.resolve(__dirname, './fixtures/esm/dir-cjs-require')
],
{stdio: 'pipe'}
);

expect(result, 'to have passed test count', 1);
});
});
1 change: 1 addition & 0 deletions test/integration/fixtures/esm/dir-cjs-require/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.testPassesIfThisVariableIsDefined = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// See https://github.com/mochajs/mocha/issues/4665 for an explanation of this test
it('should require a dir import', () => {
expect(global.testPassesIfThisVariableIsDefined, 'to be', true)
})

0 comments on commit 21cda3e

Please sign in to comment.