Skip to content

Commit

Permalink
assert: handle undefined filename in getErrMessage
Browse files Browse the repository at this point in the history
When generating an assertion error message,
`filename` might be undefined,
e.g. if `assert` is called in `eval`.

Handle this case gracefully instead of failing with
`Cannot read property 'endsWith' of undefined`.

Fixes: #20847
  • Loading branch information
jeysal committed May 21, 2018
1 parent 0271b0f commit 0118fcd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ function getBuffer(fd, assertLine) {

function getErrMessage(call) {
const filename = call.getFileName();
if (!filename) {
return;
}

const line = call.getLineNumber() - 1;
const column = call.getColumnNumber() - 1;
const identifier = `${filename}${line}${column}`;
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,16 @@ common.expectsError(
}
);

// works in eval
common.expectsError(
() => new Function('assert', 'assert(1 === 2);')(assert),
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'false == true'
}
);

// Do not try to check Node.js modules.
{
const e = new EventEmitter();
Expand Down

0 comments on commit 0118fcd

Please sign in to comment.