GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lib: fix assert shows diff messages in ESM and CJS · nodejs/node@f1cf1c3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • assert.js
  • test/parallel
    • test-assert-esm-cjs-message-verify.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/assert.js‎

Copy file name to clipboardExpand all lines: lib/assert.js
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const CallTracker = require('internal/assert/calltracker');
7373
const {
7474
validateFunction,
7575
} = require('internal/validators');
76+
const { fileURLToPath } = require('internal/url');
7677

7778
let isDeepEqual;
7879
let isDeepStrictEqual;
@@ -296,7 +297,7 @@ function getErrMessage(message, fn) {
296297
overrideStackTrace.set(err, (_, stack) => stack);
297298
const call = err.stack[0];
298299

299-
const filename = call.getFileName();
300+
let filename = call.getFileName();
300301
const line = call.getLineNumber() - 1;
301302
let column = call.getColumnNumber() - 1;
302303
let identifier;
@@ -330,6 +331,14 @@ function getErrMessage(message, fn) {
330331
const { StringDecoder } = require('string_decoder');
331332
decoder = new StringDecoder('utf8');
332333
}
334+
335+
// ESM file prop is a file proto. Convert that to path.
336+
// This ensure opensync will not throw ENOENT for ESM files.
337+
const fileProtoPrefix = 'file://';
338+
if (StringPrototypeStartsWith(filename, fileProtoPrefix)) {
339+
filename = fileURLToPath(filename);
340+
}
341+
333342
fd = openSync(filename, 'r', 0o666);
334343
// Reset column and message.
335344
({ 0: column, 1: message } = getCode(fd, line, column));
Collapse file

‎test/parallel/test-assert-esm-cjs-message-verify.js‎

Copy file name to clipboard
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
const { spawnPromisified } = require('../common');
4+
const tmpdir = require('../common/tmpdir');
5+
const assert = require('assert');
6+
const { writeFileSync, unlink } = require('fs');
7+
const { describe, after, it } = require('node:test');
8+
9+
tmpdir.refresh();
10+
11+
const fileImports = {
12+
cjs: 'const assert = require("assert");',
13+
mjs: 'import assert from "assert";',
14+
};
15+
16+
const fileNames = [];
17+
18+
for (const [ext, header] of Object.entries(fileImports)) {
19+
const fileName = `test-file.${ext}`;
20+
// Store the generated filesnames in an array
21+
fileNames.push(`${tmpdir.path}/${fileName}`);
22+
23+
writeFileSync(tmpdir.resolve(fileName), `${header}\nassert.ok(0 === 2);`);
24+
}
25+
26+
describe('ensure the assert.ok throwing similar error messages for esm and cjs files', () => {
27+
const nodejsPath = `${process.execPath}`;
28+
const errorsMessages = [];
29+
30+
it('should return code 1 for each command', async () => {
31+
for (const fileName of fileNames) {
32+
const { stderr, code } = await spawnPromisified(nodejsPath, [fileName]);
33+
assert.strictEqual(code, 1);
34+
// For each error message, filter the lines which will starts with AssertionError
35+
errorsMessages.push(
36+
stderr.split('\n').find((s) => s.startsWith('AssertionError'))
37+
);
38+
}
39+
});
40+
41+
after(() => {
42+
assert.strictEqual(errorsMessages.length, 2);
43+
assert.deepStrictEqual(errorsMessages[0], errorsMessages[1]);
44+
45+
for (const fileName of fileNames) {
46+
unlink(fileName, () => {});
47+
}
48+
49+
tmpdir.refresh();
50+
});
51+
});

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.