Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
assert: add partial support for evaluated code in simple assert
This makes sure using `assert.ok()` in `new Function()` statements
visualizes the actual call site in the error message.
  • Loading branch information
BridgeAR committed Jun 5, 2019
commit e36fd00046d8b893a7f3b66b9145d8129820b40e
55 changes: 34 additions & 21 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,24 +269,31 @@ function getErrMessage(message, fn) {
Error.prepareStackTrace = tmpPrepare;

const filename = call.getFileName();

if (!filename) {
return message;
}

const line = call.getLineNumber() - 1;
let column = call.getColumnNumber() - 1;
let identifier;
let code;

const identifier = `${filename}${line}${column}`;
if (filename) {
identifier = `${filename}${line}${column}`;

if (errorCache.has(identifier)) {
return errorCache.get(identifier);
// Skip Node.js modules!
if (filename.endsWith('.js') &&
NativeModule.exists(filename.slice(0, -3))) {
errorCache.set(identifier, undefined);
return;
}
} else {
const fn = call.getFunction();
if (!fn) {
return message;
}
code = String(fn);
identifier = `${code}${line}${column}`;
}

// Skip Node.js modules!
if (filename.endsWith('.js') && NativeModule.exists(filename.slice(0, -3))) {
errorCache.set(identifier, undefined);
return;
if (errorCache.has(identifier)) {
return errorCache.get(identifier);
}

let fd;
Expand All @@ -295,16 +302,22 @@ function getErrMessage(message, fn) {
// errors are handled faster.
Error.stackTraceLimit = 0;

if (decoder === undefined) {
const { StringDecoder } = require('string_decoder');
decoder = new StringDecoder('utf8');
if (filename) {
if (decoder === undefined) {
const { StringDecoder } = require('string_decoder');
decoder = new StringDecoder('utf8');
}
fd = openSync(filename, 'r', 0o666);
// Reset column and message.
[column, message] = getCode(fd, line, column);
// Flush unfinished multi byte characters.
decoder.end();
} else {
for (let i = 0; i < line; i++) {
code = code.slice(code.indexOf('\n') + 1);
}
[column, message] = parseCode(code, column);
}

fd = openSync(filename, 'r', 0o666);
// Reset column and message.
[column, message] = getCode(fd, line, column);
// Flush unfinished multi byte characters.
decoder.end();
// Always normalize indentation, otherwise the message could look weird.
if (message.includes('\n')) {
if (EOL === '\r\n') {
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,13 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'The expression evaluated to a falsy value:\n\n assert(1 === 2)\n'
}
);
assert.throws(
() => eval('console.log("FOO");\nassert.ok(1 === 2);'),
{
code: 'ERR_ASSERTION',
message: 'false == true'
}
);
Expand Down