Skip to content

Commit e789901

Browse files
committed
fixup
1 parent 511f8a1 commit e789901

File tree

3 files changed

+54
-45
lines changed

3 files changed

+54
-45
lines changed

lib/internal/assert.js

+41-37
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ let red = '';
1111
let white = '';
1212

1313
const kReadableOperator = {
14-
deepStrictEqual: 'Expected inputs to be strictly deep-equal',
15-
notDeepStrictEqual: 'Expected "actual" not to be strictly deep-equal',
16-
strictEqual: 'Expected inputs to be strictly equal',
17-
notStrictEqual: 'Expected "actual" to be strictly unequal',
14+
deepStrictEqual: 'Expected inputs to be strictly deep-equal:',
15+
notDeepStrictEqual: 'Expected "actual" not to be strictly deep-equal to:',
16+
strictEqual: 'Expected inputs to be strictly equal:',
17+
notStrictEqual: 'Expected "actual" to be strictly unequal to:',
1818
notIdentical: 'Inputs identical but not reference equal:',
1919
};
2020

@@ -62,7 +62,7 @@ function createErrDiff(actual, expected, operator) {
6262
const actualLines = inspectValue(actual);
6363
const expectedLines = inspectValue(expected);
6464
const msg = kReadableOperator[operator] +
65-
`:\n${green}+ actual${white} ${red}- expected${white}`;
65+
`\n${green}+ actual${white} ${red}- expected${white}`;
6666
const skippedMsg = ` ${blue}...${white} Lines skipped`;
6767

6868
let i = 0;
@@ -80,20 +80,25 @@ function createErrDiff(actual, expected, operator) {
8080
if ((typeof actual !== 'object' || actual === null) &&
8181
(typeof expected !== 'object' || expected === null) &&
8282
(actual !== 0 || expected !== 0)) { // -0 === +0
83-
return `${kReadableOperator[operator]}:\n\n` +
83+
return `${kReadableOperator[operator]}\n\n` +
8484
`${actualLines[0]} !== ${expectedLines[0]}\n`;
8585
}
86-
// If the stdout is a tty and the input length is lower than the current
87-
// columns per line, add a mismatch indicator below the output.
88-
} else if (process.stdout.isTTY &&
89-
inputLength < process.stdout.columns) {
90-
while (actualLines[0][i] === expectedLines[0][i]) {
91-
i++;
86+
} else {
87+
// If the stdout is a tty and the input length is lower than the current
88+
// columns per line, add a mismatch indicator below the output. If it is
89+
// not a tty, use a default value of 80 characters.
90+
const maxLength = process.stdout.isTTY ? process.stdout.columns : 80;
91+
if (inputLength < maxLength) {
92+
while (actualLines[0][i] === expectedLines[0][i]) {
93+
i++;
94+
}
95+
if (i !== 0) {
96+
// Add position indicator for the first mismatch in case it is a
97+
// single line and the input length is less than the column length.
98+
indicator = `\n ${' '.repeat(i)}^`;
99+
i = 0;
100+
}
92101
}
93-
// Add position indicator for the first mismatch in case it is a single
94-
// line and the input length is less than the column length.
95-
indicator = `\n ${' '.repeat(i)}^`;
96-
i = 0;
97102
}
98103
}
99104

@@ -114,6 +119,26 @@ function createErrDiff(actual, expected, operator) {
114119
a = actualLines[actualLines.length - 1];
115120
b = expectedLines[expectedLines.length - 1];
116121
}
122+
123+
const maxLines = Math.max(actualLines.length, expectedLines.length);
124+
// Strict equal with identical objects that are not identical by reference.
125+
// E.g., assert.deepStrictEqual({ a: Symbol() }, { a: Symbol() })
126+
if (maxLines === 0) {
127+
// We have to get the result again. The lines were all removed before.
128+
const actualLines = inspectValue(actual);
129+
130+
// Only remove lines in case it makes sense to collapse those.
131+
// TODO: Accept env to always show the full error.
132+
if (actualLines.length > 30) {
133+
actualLines[26] = `${blue}...${white}`;
134+
while (actualLines.length > 27) {
135+
actualLines.pop();
136+
}
137+
}
138+
139+
return `${kReadableOperator.notIdentical}\n\n${actualLines.join('\n')}\n`;
140+
}
141+
117142
if (i > 3) {
118143
end = `\n${blue}...${white}${end}`;
119144
skipped = true;
@@ -123,9 +148,7 @@ function createErrDiff(actual, expected, operator) {
123148
other = '';
124149
}
125150

126-
const maxLines = Math.max(actualLines.length, expectedLines.length);
127151
let printedLines = 0;
128-
let identical = 0;
129152
for (i = 0; i < maxLines; i++) {
130153
// Only extra expected lines exist
131154
const cur = i - lastPos;
@@ -185,7 +208,6 @@ function createErrDiff(actual, expected, operator) {
185208
res += `\n ${actualLines[i]}`;
186209
printedLines++;
187210
}
188-
identical++;
189211
}
190212
// Inspected object to big (Show ~20 rows max)
191213
if (printedLines > 20 && i < maxLines - 2) {
@@ -194,24 +216,6 @@ function createErrDiff(actual, expected, operator) {
194216
}
195217
}
196218

197-
// Strict equal with identical objects that are not identical by reference.
198-
// E.g., assert.deepStrictEqual({ a: Symbol() }, { a: Symbol() })
199-
if (identical === maxLines) {
200-
// We have to get the result again. The lines were all removed before.
201-
const actualLines = inspectValue(actual);
202-
203-
// Only remove lines in case it makes sense to collapse those.
204-
// TODO: Accept env to always show the full error.
205-
if (actualLines.length > 30) {
206-
actualLines[26] = `${blue}...${white}`;
207-
while (actualLines.length > 27) {
208-
actualLines.pop();
209-
}
210-
}
211-
212-
return `${kReadableOperator.notIdentical}\n\n${actualLines.join('\n')}\n`;
213-
}
214-
215219
return `${msg}${skipped ? skippedMsg : ''}\n${res}${other}${end}${indicator}`;
216220
}
217221

test/parallel/test-assert-deep.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ assert.throws(
750750
() => assert.notDeepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)),
751751
{
752752
name: 'AssertionError [ERR_ASSERTION]',
753-
message: 'Expected "actual" not to be strictly deep-equal ' +
753+
message: 'Expected "actual" not to be strictly deep-equal to: ' +
754754
util.inspect(new Date(2000, 3, 14))
755755
}
756756
);
@@ -795,7 +795,7 @@ assert.throws(
795795
{
796796
code: 'ERR_ASSERTION',
797797
name: 'AssertionError [ERR_ASSERTION]',
798-
message: `${defaultMsgStartFull}\n\n+ /a/gim\n- /a/im`
798+
message: `${defaultMsgStartFull}\n\n+ /a/gim\n- /a/im\n ^`
799799
});
800800

801801
{

test/parallel/test-assert.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ assert.throws(() => a.strictEqual(null, undefined),
7171
assert.throws(
7272
() => a.notStrictEqual(2, 2),
7373
{
74-
message: 'Expected "actual" to be strictly unequal 2',
74+
message: 'Expected "actual" to be strictly unequal to: 2',
7575
name: 'AssertionError [ERR_ASSERTION]'
7676
}
7777
);
7878

7979
assert.throws(
8080
() => a.notStrictEqual('a '.repeat(30), 'a '.repeat(30)),
8181
{
82-
message: `Expected "actual" to be strictly unequal '${'a '.repeat(30)}'`,
82+
message: 'Expected "actual" to be strictly unequal to: ' +
83+
`'${'a '.repeat(30)}'`,
8384
name: 'AssertionError [ERR_ASSERTION]'
8485
}
8586
);
@@ -415,7 +416,8 @@ assert.throws(
415416
code: 'ERR_ASSERTION',
416417
name: 'AssertionError [ERR_ASSERTION]',
417418
message: strictEqualMessageStart +
418-
'+ actual - expected\n\n+ [Error: foo]\n- [Error: foobar]'
419+
'+ actual - expected\n\n' +
420+
'+ [Error: foo]\n- [Error: foobar]\n ^'
419421
}
420422
);
421423

@@ -606,12 +608,15 @@ assert.throws(
606608
});
607609

608610
// notDeepEqual tests
609-
message = 'Expected "actual" not to be strictly deep-equal\n\n[\n 1\n]\n';
610611
assert.throws(
611612
() => assert.notDeepEqual([1], [1]),
612-
{ message });
613+
{
614+
message: 'Expected "actual" not to be strictly deep-equal to:\n\n' +
615+
'[\n 1\n]\n'
616+
}
617+
);
613618

614-
message = 'Expected "actual" not to be strictly deep-equal' +
619+
message = 'Expected "actual" not to be strictly deep-equal to:' +
615620
`\n\n[${'\n 1,'.repeat(25)}\n...\n`;
616621
const data = Array(31).fill(1);
617622
assert.throws(

0 commit comments

Comments
 (0)