mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00

This PR addresses an issue which was caused by the design in the ESM loader. The ESM loader was modifying the file path and replacing the 'file' property with the file proto in the stack trace. This, in turn, led to unhandled exceptions when the assert module attempted to open the file to display erroneous code. The changes in this PR resolve this issue by handling the file path correctly, ensuring that the remaining message formatting code can execute as expected. PR-URL: https://github.com/nodejs/node/pull/50634 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const { spawnPromisified } = require('../common');
|
|
const tmpdir = require('../common/tmpdir');
|
|
const assert = require('assert');
|
|
const { writeFileSync, unlink } = require('fs');
|
|
const { describe, after, it } = require('node:test');
|
|
|
|
tmpdir.refresh();
|
|
|
|
const fileImports = {
|
|
cjs: 'const assert = require("assert");',
|
|
mjs: 'import assert from "assert";',
|
|
};
|
|
|
|
const fileNames = [];
|
|
|
|
for (const [ext, header] of Object.entries(fileImports)) {
|
|
const fileName = `test-file.${ext}`;
|
|
// Store the generated filesnames in an array
|
|
fileNames.push(`${tmpdir.path}/${fileName}`);
|
|
|
|
writeFileSync(tmpdir.resolve(fileName), `${header}\nassert.ok(0 === 2);`);
|
|
}
|
|
|
|
describe('ensure the assert.ok throwing similar error messages for esm and cjs files', () => {
|
|
const nodejsPath = `${process.execPath}`;
|
|
const errorsMessages = [];
|
|
|
|
it('should return code 1 for each command', async () => {
|
|
for (const fileName of fileNames) {
|
|
const { stderr, code } = await spawnPromisified(nodejsPath, [fileName]);
|
|
assert.strictEqual(code, 1);
|
|
// For each error message, filter the lines which will starts with AssertionError
|
|
errorsMessages.push(
|
|
stderr.split('\n').find((s) => s.startsWith('AssertionError'))
|
|
);
|
|
}
|
|
});
|
|
|
|
after(() => {
|
|
assert.strictEqual(errorsMessages.length, 2);
|
|
assert.deepStrictEqual(errorsMessages[0], errorsMessages[1]);
|
|
|
|
for (const fileName of fileNames) {
|
|
unlink(fileName, () => {});
|
|
}
|
|
|
|
tmpdir.refresh();
|
|
});
|
|
});
|