See these examples:
First:
assert.deepStrictEqual([1, 2, 1, 2, 1], [1, 1, 1])
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected
[
1,
+ 2,
+ 1,
+ 2,
- 1,
1
]
It should actually print:
assert.deepStrictEqual([1, 2, 1, 2, 1], [1, 1, 1])
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected
[
1,
+ 2,
1,
+ 2,
1
]
Second example:
> assert.deepStrictEqual([1, { a: true, b: false }, 1], [2, 1, { b: false, a: true }, 2])
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected
[
+ 1,
+ {
+ a: true,
+ b: false
+ },
+ 1
- 2,
- 1,
- {
- a: true,
- b: false
- },
- 2
]
Should be:
> assert.deepStrictEqual([1, { a: true, b: false }, 1], [2, 1, { b: false, a: true }, 2])
AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected
[
- 2,
1,
{
a: true,
b: false
},
+ 1
- 2
]
Already fixed part
$ ./node -e 'assert.deepStrictEqual({a: 1, b: 2}, {a: 1, b: 2, c: 3})'
assert.js:84
throw new AssertionError(obj);
^
AssertionError [ERR_ASSERTION]: Expected inputs to be strictly deep-equal:
+ actual - expected
{
a: 1,
+ b: 2
- b: 2,
- c: 3
}
I think I would expect more something like this:
+ actual - expected
{
a: 1,
b: 2,
- c: 3
}
On a related note, I think it's even more confusing when assert.strictEqual is called. Since it is supposed to compare by reference, it doesn't make sense to me to show a diff, because what the developer has to do to fix the error is not change the properties in the object.
This is already on my ToDo list. I had a look at proper diffing algorithms and I'll add that when I find a bit more time. The output could still be improved a lot. This was a quick and simple implementation that improved the former situation a lot and that's why I went for that first.
I'll have a thought about assert.strictEqual() as well.
@targos looking at your comment about strictEqual again: in case an object has the same properties but it's not reference equal, the error is going to be:
AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
{
a: 1,
b: 2,
c: 3
}
What do you think would be a good output in case of two diverging objects that are not reference equal? Only showing the actual or expected object or not showing any at all removes important information for the user. So the only thing I can think about right now that might improve the situation would be to show both objects at the same time (which the current situation should actually already solve by having a combined view that reduces the overall output length).
Would a different message instead already help? E.g., 'Expected reference equal inputs but got:'
I would only use that message for objects and not when comparing primitives.
Thanks for the fixes @BridgeAR !
@targos you are welcome but for me this is still work in progress. There are still a couple of cases that will not produce a nice diff. It is just not that trivial to solve. This applies especially to more complex nested objects. I am going to work on it either way but we could just this issue open as reference point.
OK, I can reopen. Can you update the OP or post a message with an example that is still problematic?
Done. I just updated your post :)
I'm closing this. The current output is not strictly wrong
Most helpful comment
This is already on my ToDo list. I had a look at proper diffing algorithms and I'll add that when I find a bit more time. The output could still be improved a lot. This was a quick and simple implementation that improved the former situation a lot and that's why I went for that first.
I'll have a thought about
assert.strictEqual()as well.