@@ -13,10 +13,13 @@ let white = '';
13
13
const kReadableOperator = {
14
14
deepStrictEqual : 'Expected inputs to be strictly deep-equal:' ,
15
15
strictEqual : 'Expected inputs to be strictly equal:' ,
16
+ strictEqualObject : 'Expected "actual" to be reference-equal to "expected":' ,
16
17
deepEqual : 'Expected inputs to be loosely deep-equal:' ,
17
18
equal : 'Expected inputs to be loosely equal:' ,
18
19
notDeepStrictEqual : 'Expected "actual" not to be strictly deep-equal to:' ,
19
20
notStrictEqual : 'Expected "actual" to be strictly unequal to:' ,
21
+ // eslint-disable-next-line max-len
22
+ notStrictEqualObject : 'Expected "actual" not to be reference-equal to "expected":' ,
20
23
notDeepEqual : 'Expected "actual" not to be loosely deep-equal to:' ,
21
24
notEqual : 'Expected "actual" to be loosely unequal to:' ,
22
25
notIdentical : 'Inputs identical but not reference equal:' ,
@@ -67,13 +70,20 @@ function createErrDiff(actual, expected, operator) {
67
70
const actualInspected = inspectValue ( actual ) ;
68
71
const actualLines = actualInspected . split ( '\n' ) ;
69
72
const expectedLines = inspectValue ( expected ) . split ( '\n' ) ;
70
- const msg = kReadableOperator [ operator ] +
71
- `\n${ green } + actual${ white } ${ red } - expected${ white } ` ;
72
- const skippedMsg = ` ${ blue } ...${ white } Lines skipped` ;
73
73
74
74
let i = 0 ;
75
75
let indicator = '' ;
76
76
77
+ // In case both inputs are objects explicitly mark them as not reference equal
78
+ // for the `strictEqual` operator.
79
+ if ( operator === 'strictEqual' &&
80
+ typeof actual === 'object' &&
81
+ typeof expected === 'object' &&
82
+ actual !== null &&
83
+ expected !== null ) {
84
+ operator = 'strictEqualObject' ;
85
+ }
86
+
77
87
// If "actual" and "expected" fit on a single line and they are not strictly
78
88
// equal, check further special handling.
79
89
if ( actualLines . length === 1 && expectedLines . length === 1 &&
@@ -89,7 +99,7 @@ function createErrDiff(actual, expected, operator) {
89
99
return `${ kReadableOperator [ operator ] } \n\n` +
90
100
`${ actualLines [ 0 ] } !== ${ expectedLines [ 0 ] } \n` ;
91
101
}
92
- } else {
102
+ } else if ( operator !== 'strictEqualObject' ) {
93
103
// If the stderr is a tty and the input length is lower than the current
94
104
// columns per line, add a mismatch indicator below the output. If it is
95
105
// not a tty, use a default value of 80 characters.
@@ -156,6 +166,10 @@ function createErrDiff(actual, expected, operator) {
156
166
}
157
167
158
168
let printedLines = 0 ;
169
+ const msg = kReadableOperator [ operator ] +
170
+ `\n${ green } + actual${ white } ${ red } - expected${ white } ` ;
171
+ const skippedMsg = ` ${ blue } ...${ white } Lines skipped` ;
172
+
159
173
for ( i = 0 ; i < maxLines ; i ++ ) {
160
174
// Only extra expected lines exist
161
175
const cur = i - lastPos ;
@@ -274,8 +288,15 @@ class AssertionError extends Error {
274
288
operator === 'notStrictEqual' ) {
275
289
// In case the objects are equal but the operator requires unequal, show
276
290
// the first object and say A equals B
291
+ let base = kReadableOperator [ operator ] ;
277
292
const res = inspectValue ( actual ) . split ( '\n' ) ;
278
- const base = kReadableOperator [ operator ] ;
293
+
294
+ // In case "actual" is an object, it should not be reference equal.
295
+ if ( operator === 'notStrictEqual' &&
296
+ typeof actual === 'object' &&
297
+ actual !== null ) {
298
+ base = kReadableOperator . notStrictEqualObject ;
299
+ }
279
300
280
301
// Only remove lines in case it makes sense to collapse those.
281
302
// TODO: Accept env to always show the full error.
0 commit comments