-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: fix assert shows diff messages in ESM and CJS
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.
- Loading branch information
Showing
2 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const assert = require('assert'); | ||
const { writeFileSync, unlink } = require('fs'); | ||
const { execSync } = require('child_process'); | ||
|
||
tmpdir.refresh(); | ||
|
||
// To ensure the assert ok throwing similar error messages for esm and cjs | ||
|
||
const nodejs = `${process.execPath}`; | ||
|
||
// Importing package for each extensions. | ||
const fileImports = { | ||
mjs: 'import assert from "assert";', | ||
js: 'const assert = require("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);`); | ||
} | ||
|
||
const errorsMessages = []; | ||
|
||
for (const fileName of fileNames) { | ||
const command = `${nodejs} ${fileName}`; | ||
assert.throws( | ||
() => { | ||
execSync(command, { stdio: 'pipe' }); | ||
}, | ||
(e) => { | ||
// For each error message, filter the lines which will starts with AssertionError | ||
errorsMessages.push( | ||
e.message.split('\n').find((s) => s.startsWith('AssertionError')) | ||
); | ||
return true; | ||
} | ||
); | ||
} | ||
|
||
assert.deepStrictEqual(errorsMessages[0], errorsMessages[1]); | ||
|
||
for (const fileName of fileNames) { | ||
unlink(fileName, () => {}); | ||
} | ||
|
||
tmpdir.refresh(); | ||