Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: fix assert throwing different error messages in ESM and CJS #50634

Merged
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
11 changes: 10 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const CallTracker = require('internal/assert/calltracker');
const {
validateFunction,
} = require('internal/validators');
const { fileURLToPath } = require('internal/url');

let isDeepEqual;
let isDeepStrictEqual;
Expand Down Expand Up @@ -296,7 +297,7 @@ function getErrMessage(message, fn) {
overrideStackTrace.set(err, (_, stack) => stack);
const call = err.stack[0];

const filename = call.getFileName();
let filename = call.getFileName();
const line = call.getLineNumber() - 1;
let column = call.getColumnNumber() - 1;
let identifier;
Expand Down Expand Up @@ -330,6 +331,14 @@ function getErrMessage(message, fn) {
const { StringDecoder } = require('string_decoder');
decoder = new StringDecoder('utf8');
}

// ESM file prop is a file proto. Convert that to path.
// This ensure opensync will not throw ENOENT for ESM files.
const fileProtoPrefix = 'file://';
if (StringPrototypeStartsWith(filename, fileProtoPrefix)) {
filename = fileURLToPath(filename);
}

fd = openSync(filename, 'r', 0o666);
// Reset column and message.
({ 0: column, 1: message } = getCode(fd, line, column));
Expand Down
51 changes: 51 additions & 0 deletions test/parallel/test-assert-esm-cjs-message-verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'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();
});
});