test(ses): improve coverage of module-instance's cjs support
MODIFY
packages/compartment-mapper/test/fixtures-0/node_modules/esmcjs/index.mjs
packages/compartment-mapper/test/fixtures-0/node_modules/esmcjs/index.mjs
import defaultAsName from './module-exports-assigned-object.cjs';
import * as namespaceAsName from './module-exports-assigned-object.cjs';
+
// import { name } from './module-exports-assigned-object.cjs'; // Not expected to work, does not work on Node.js
if (defaultAsName.name !== 42) {
- throw new Error(`Assigning to module exports should be equivalent to exporting a default`);
+ throw new Error(
+ `Assigning to module exports should be equivalent to exporting a default`,
+ );
}
if (namespaceAsName.default.name !== 42) {
- throw new Error(`Assigning to module exports should be equivalent to exporting a default on the namespace object`);
+ throw new Error(
+ `Assigning to module exports should be equivalent to exporting a default on the namespace object`,
+ );
}
if (namespaceAsName.name !== undefined) {
- throw new Error(`Assigning to module exports should not replace the properties of the namespace object`);
+ throw new Error(
+ `Assigning to module exports should not replace the properties of the namespace object`,
+ );
}

MODIFY
packages/compartment-mapper/test/fixtures-0/node_modules/esmcjs/module-exports-assigned-object.cjs
packages/compartment-mapper/test/fixtures-0/node_modules/esmcjs/module-exports-assigned-object.cjs
+const name = 42;
module.exports = {
- name: 42,
+ name,
};
+
+// Beware of lexer issues
+// https://github.com/endojs/endo-e2e-tests/blob/main/test-matrix/table.md
+// https://dev.to/naugtur/additive-changes-to-your-cjs-exports-are-now-breaking-changes-3pnk

MODIFY
packages/ses/test/test-import-cjs.js
packages/ses/test/test-import-cjs.js
@@ -194,7 +194,7 @@ test('ESM imports CommonJS module as default', async t => {
if (specifier === './even') {
return CjsStaticModuleRecord(
`
- exports.default = n => n % 2 === 0;
+ module.exports = n => n % 2 === 0;
`,
'https://example.com/even',
);
@@ -220,6 +220,76 @@ test('ESM imports CommonJS module as default', async t => {
t.is(odd(2), false);
});
+test('ESM imports CommonJS module as star', async t => {
+ t.plan(2);
+
+ const resolveHook = resolveNode;
+ const importHook = async specifier => {
+ if (specifier === './even') {
+ return CjsStaticModuleRecord(
+ `
+ exports.even = n => n % 2 === 0;
+ `,
+ 'https://example.com/even',
+ );
+ }
+ if (specifier === './odd') {
+ return new StaticModuleRecord(
+ `
+ import * as evens from './even';
+ export default n => !evens.even(n);
+ `,
+ 'https://example.com/odd',
+ );
+ }
+ throw new Error(`Cannot load module ${specifier}`);
+ };
+
+ const compartment = new Compartment({}, {}, { resolveHook, importHook });
+ const {
+ namespace: { default: odd },
+ } = await compartment.import('./odd');
+
+ t.is(odd(1), true);
+ t.is(odd(2), false);
+});
+
+test('ESM imports CommonJS module with replaced exports as star', async t => {
+ t.plan(2);
+
+ const resolveHook = resolveNode;
+ const importHook = async specifier => {
+ if (specifier === './even') {
+ return CjsStaticModuleRecord(
+ `
+ module.exports = { even: n => n % 2 === 0 };
+ `,
+ 'https://example.com/even',
+ );
+ }
+ if (specifier === './odd') {
+ // It should be evens.even(n) not evens.default.even(n)
+ // but CjsStaticModuleRecord is not enough to handle that
+ return new StaticModuleRecord(
+ `
+ import * as evens from './even';
+ export default n => !evens.default.even(n);
+ `,
+ 'https://example.com/odd',
+ );
+ }
+ throw new Error(`Cannot load module ${specifier}`);
+ };
+
+ const compartment = new Compartment({}, {}, { resolveHook, importHook });
+ const {
+ namespace: { default: odd },
+ } = await compartment.import('./odd');
+
+ t.is(odd(1), true);
+ t.is(odd(2), false);
+});
+
test('ESM imports CommonJS module by name', async t => {
t.plan(2);

test(ses): improve coverage of module-instance's cjs support