Skip to content

Commit

Permalink
fixup! module: disallow CJS <-> ESM edges in a cycle from require(esm)
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Apr 6, 2024
1 parent dc62194 commit f414613
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
71 changes: 71 additions & 0 deletions test/es-module/test-require-module-cycle-esm-esm-cjs-esm-esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const fixtures = require('../common/fixtures');
const assert = require('assert');

// a.mjs -> b.mjs -> c.cjs -> z.mjs -> a.mjs
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
fixtures.path('es-modules/esm-esm-cjs-esm-esm-cycle/a.mjs'),
],
{
signal: null,
status: 1,
stderr: /Cannot import Module \.\/a\.mjs in a cycle\. \(from .*z\.mjs\)/,
}
);
}

// b.mjs -> c.cjs -> z.mjs -> a.mjs -> b.mjs
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
fixtures.path('es-modules/esm-esm-cjs-esm-esm-cycle/b.mjs'),
],
{
signal: null,
status: 1,
stderr: /Cannot import Module \.\/b\.mjs in a cycle\. \(from .*a\.mjs\)/,
}
);
}

// c.cjs -> z.mjs -> a.mjs -> b.mjs -> c.cjs
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
fixtures.path('es-modules/esm-esm-cjs-esm-esm-cycle/c.cjs'),
],
{
signal: null,
status: 1,
stderr: /Cannot import CommonJS Module \.\/c\.cjs in a cycle\. \(from .*b\.mjs\)/,
}
);
}


// z.mjs -> a.mjs -> b.mjs -> c.cjs -> z.mjs
{
spawnSyncAndAssert(
process.execPath,
[
'--experimental-require-module',
fixtures.path('es-modules/esm-esm-cjs-esm-esm-cycle/z.mjs'),
],
{
signal: null,
status: 1,
stderr: /Cannot require\(\) ES Module .*z\.mjs in a cycle\. \(from .*c\.cjs\)/,
}
);
}
1 change: 1 addition & 0 deletions test/fixtures/es-modules/esm-esm-cjs-esm-esm-cycle/a.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './b.mjs'
1 change: 1 addition & 0 deletions test/fixtures/es-modules/esm-esm-cjs-esm-esm-cycle/b.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './c.cjs'
1 change: 1 addition & 0 deletions test/fixtures/es-modules/esm-esm-cjs-esm-esm-cycle/c.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./z.mjs')
1 change: 1 addition & 0 deletions test/fixtures/es-modules/esm-esm-cjs-esm-esm-cycle/z.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './a.mjs'

0 comments on commit f414613

Please sign in to comment.