Skip to content

Commit

Permalink
explicitly test the interop patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jul 28, 2024
1 parent 3d7d1eb commit 33165b5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/es-module/test-require-as-esm-interop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Flags: --experimental-require-module
'use strict';
const common = require('../common');
const assert = require('assert');

// This test demonstrates interop between CJS and CJS represented as ESM
// under the `__cjsModule` pattern.
(async () => {
// dep.js and index.js are CJS modules.
// dep exports 'cjs', while index reexports dep:
assert.strictEqual(require('../fixtures/cjsesm/dep.js'), 'cjs');
assert.strictEqual(require('../fixtures/cjsesm/index.js'), 'cjs');

// Now we have ESM conversions of these dependencies, using __cjsModule,

Check failure on line 14 in test/es-module/test-require-as-esm-interop.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Trailing spaces not allowed
// that behave equivalently under require(esm) despite the custom string import:
assert.strictEqual(require('../fixtures/cjsesm/dep.mjs'), 'cjs');
assert.strictEqual(require('../fixtures/cjsesm/index.mjs'), 'cjs');

// Furthermore, if `index.mjs` imports from `dep.mjs` directly, the reexport
// still works:
assert.strictEqual(require('../fixtures/cjsesm/index-importing-depmjs.mjs'), 'cjs');

// Finally, the ESM representations under these conversions all match equivalently:
const esmCjsImport = await import('../fixtures/cjsesm/index.js');

assert.deepStrictEqual(await import('../fixtures/cjsesm/index.mjs'), esmCjsImport);
assert.deepStrictEqual(await import('../fixtures/cjsesm/index-importing-depmjs.mjs'), esmCjsImport);
})().then(common.mustCall());
1 change: 1 addition & 0 deletions test/fixtures/cjsesm/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'cjs';
2 changes: 2 additions & 0 deletions test/fixtures/cjsesm/dep.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default 'cjs';
export const __cjsModule = true;
3 changes: 3 additions & 0 deletions test/fixtures/cjsesm/index-importing-depmjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dep from './dep.mjs';
export default dep;
export const __cjsModule = true;
2 changes: 2 additions & 0 deletions test/fixtures/cjsesm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const dep = require('./dep.js');
module.exports = dep;
3 changes: 3 additions & 0 deletions test/fixtures/cjsesm/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dep from './dep.js';
export default dep;
export const __cjsModule = true;

0 comments on commit 33165b5

Please sign in to comment.