Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 014a2d1

Browse filesBrowse files
BridgeARMylesBorins
authored andcommitted
assert: fix wrong message indentation
If code is read from a file and that code is indented, it would be misaligned. This makes sure it has a natural indentation that is relative to the starting point of the assertion. PR-URL: #20791 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 3e7741c commit 014a2d1
Copy full SHA for 014a2d1

File tree

Expand file treeCollapse file tree

2 files changed

+60
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+60
-6
lines changed
Open diff view settings
Collapse file

‎lib/assert.js‎

Copy file name to clipboardExpand all lines: lib/assert.js
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,19 @@ function getErrMessage(call) {
192192
if (EOL === '\r\n') {
193193
message = message.replace(/\r\n/g, '\n');
194194
}
195+
// Always normalize indentation, otherwise the message could look weird.
196+
if (message.indexOf('\n') !== -1) {
197+
const tmp = message.split('\n');
198+
message = tmp[0];
199+
for (var i = 1; i < tmp.length; i++) {
200+
let pos = 0;
201+
while (pos < column &&
202+
(tmp[i][pos] === ' ' || tmp[i][pos] === '\t')) {
203+
pos++;
204+
}
205+
message += `\n ${tmp[i].slice(pos)}`;
206+
}
207+
}
195208
message = 'The expression evaluated to a falsy value:' +
196209
`\n\n assert${ok}(${message})\n`;
197210
}
Collapse file

‎test/parallel/test-assert.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-assert.js
+47-6Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -694,14 +694,55 @@ common.expectsError(
694694
{
695695
code: 'ERR_ASSERTION',
696696
type: assert.AssertionError,
697-
message: 'The expression evaluated to a falsy value:\n\n ' +
698-
"assert((() => 'string')()\n" +
699-
' // eslint-disable-next-line\n' +
700-
' ===\n' +
701-
' 123 instanceof\n' +
702-
' Buffer)\n'
697+
message: 'The expression evaluated to a falsy value:\n\n' +
698+
' assert((() => \'string\')()\n' +
699+
' // eslint-disable-next-line\n' +
700+
' ===\n' +
701+
' 123 instanceof\n' +
702+
' Buffer)\n'
703+
}
704+
);
705+
706+
common.expectsError(
707+
() => {
708+
a(
709+
(() => 'string')()
710+
// eslint-disable-next-line
711+
===
712+
123 instanceof
713+
Buffer
714+
);
715+
},
716+
{
717+
code: 'ERR_ASSERTION',
718+
type: assert.AssertionError,
719+
message: 'The expression evaluated to a falsy value:\n\n' +
720+
' assert((() => \'string\')()\n' +
721+
' // eslint-disable-next-line\n' +
722+
' ===\n' +
723+
' 123 instanceof\n' +
724+
' Buffer)\n'
725+
}
726+
);
727+
728+
/* eslint-disable indent */
729+
common.expectsError(() => {
730+
a((
731+
() => 'string')() ===
732+
123 instanceof
733+
Buffer
734+
);
735+
}, {
736+
code: 'ERR_ASSERTION',
737+
type: assert.AssertionError,
738+
message: 'The expression evaluated to a falsy value:\n\n' +
739+
' assert((\n' +
740+
' () => \'string\')() ===\n' +
741+
' 123 instanceof\n' +
742+
' Buffer)\n'
703743
}
704744
);
745+
/* eslint-enable indent */
705746

706747
common.expectsError(
707748
() => assert(null, undefined),

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.