Skip to content
Closed
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
module: testing on require(esm) error caching
Adds testing to make ensure top level errors thrown during ESM module
import/evaluation get preserved on subsequent attempts to load the same
module again no matter if require(esm) or import(esm)

Refs: #58945
  • Loading branch information
samuelhnrq committed Jul 4, 2025
commit 0d52b6528444cc94de29b1bb1c2012c2e9847abd
26 changes: 26 additions & 0 deletions test/es-module/test-esm-cjs-error-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const { spawnPromisified } = require('../common');
const fixtures = require('../common/fixtures.js');
const assert = require('node:assert');
const { execPath } = require('node:process');
const { describe, it } = require('node:test');


describe('CJS ↔︎ ESM preserve errors', () => {
it('should preserve errors when CJS import first', async () => {
const validEntry = fixtures.path('/es-modules/cjs-esm-runtime-error.js');
const { code, signal, stdout } = await spawnPromisified(execPath, [validEntry]);
assert.strictEqual(stdout, 'CJS_OK\nESM_OK\n');
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should preserve errors when ESM import first', async () => {
const validEntry = fixtures.path('/es-modules/esm-cjs-runtime-error.js');
const { code, signal, stdout } = await spawnPromisified(execPath, [validEntry]);
assert.strictEqual(stdout, 'ESM_OK\nCJS_OK\n');
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
});
2 changes: 1 addition & 1 deletion test/es-module/test-require-module-error-catching.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const assert = require('assert');

// Runtime errors from throw should be caught.
assert.throws(() => {
require('../fixtures/es-modules/runtime-error-esm.js');
require('../fixtures/es-modules/runtime-error-esm.mjs');
}, {
message: 'hello'
});
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/es-modules/cjs-esm-runtime-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ensure we're a CJS
require('path');


function goodError(err) {
return err instanceof Error && err.message === 'hello'
}

try {
require('./runtime-error-esm.mjs')
process.exit(1)
} catch (err) {
if (goodError(err)) {
console.log('CJS_OK')
} else {
process.exit(1)
}
}

import('./runtime-error-esm.mjs').then(() => {
process.exit(1)
}).catch((err) => {
if (goodError(err)) {
console.log('ESM_OK')
} else {
process.exit(1)
}
})
28 changes: 28 additions & 0 deletions test/fixtures/es-modules/esm-cjs-runtime-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

// ensure we're a CJS
require('path');

function goodError(err) {
return err instanceof Error && err.message === 'hello'
}

import('./runtime-error-esm.mjs').then(() => {
process.exit(1)
}).catch((err) => {
if (goodError(err)) {
console.log('ESM_OK')
} else {
process.exit(1)
}
try {
require('./runtime-error-esm.mjs')
process.exit(1)
} catch (err) {
if (goodError(err)) {
console.log("CJS_OK")
} else {
process.exit(1)
}
}
})