Ava: Improve t.is output when objects are different references

Created on 17 Oct 2017  路  2Comments  路  Source: avajs/ava

Description

The output for a failed t.is() assertion should be clearer when the actual and expected arguments are identical objects but not the same reference.

Object diffing works well when their properties are different, but not when the references are different. Specifically, the issue is that there is nothing useful in the diff and no additional message explaining the actual error.

Since equality in JavaScript is a frequent source of confusion for beginners, I think fixing this will be particularly helpful for those users.

In my case, I changed an API from modifying its input to returning a copy. So I know the solution is t.deepEqual(), but I only know that because I understand what is happening under the covers.

Test Source

test(() => {
    t.is({ hi : 'bye' }, { hi : 'bye' });
});

Output

  Difference:

    {
      hi: 'bye',
    }

Config

Everything default.

Environment

Node 8.6.0
npm 5.4.2
ava 0.22.0
darwin 17.0.0

bug help wanted assertions reporters

All 2 comments

Great observation @sholladay.

Currently we go straight for a diff: https://github.com/avajs/ava/blob/eebf26e6e0bafd3c96f8e20311377a11b9a4f695/lib/assert.js#L90-L97

We could perform a deep equal first and generate the diff when that doesn't pass, and otherwise print an appropriate message. That's a bit more expensive performance wise but IMHO we shouldn't optimize assertion failures as much as we should optimize the happy-everything-passes path.

Please drop a comment if you're reading this and want to take this on 馃槃

@novemberborn I have done a PR for this. Check it out please. - #1568

Was this page helpful?
0 / 5 - 0 ratings