chaijs/sinon-chai

calledWith should throw a nicer error when dealing with large objects

leggsimon opened this issue · 3 comments

So using calledWith where an argument is an object (also possibly an array although I haven't tried) it would be nice if sinon-chai could throw the same assertion error as the deep equal error.

The reason I say this is that I would love to use the calledWith syntax as it reads nicely but when dealing with a large object it throws an error that is almost impossible to easily find what the difference is. I have provided a little example below if you copy that, run mocha and look at the error it 's not clear what is actually different.

const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const { expect } = require('chai').use(sinonChai);;

const stub = sinon.stub()

it('should throw a deep equal assertion error', () => {
  const largeObject = { "user": { "_id": 422985, "labels": ["User", "Flagged"], "properties": { "nodeRank": 7, "uuid": "28875745-3186-4df0-8372-12e9556881b7" } }, "total": 9, "items": [{ "nodeRank": 902, "name": "The College of the Holy and Undivided Trinity in the University of Oxford", "taxonomy": "organisations", "uuid": "TnN0ZWluX09OX1VLX0NvbGxlZ2VfMzc=-T04=", "_rel": { "created": 1490778694957, "count": 1, "updated": 1490778694357 } }, { "name": "Theo Leanse", "taxonomy": "authors", "uuid": "ZDRiY2JlMjgtYWE4Yi00YjdmLTkyNWYtNDk0NTAxNmNjM2Vk-QXV0aG9ycw==", "_rel": { "created": 1489665098854, "count": 1, "updated": 1489665098854 } }, { "nodeRank": 66986, "name": "World", "taxonomy": "sections", "type": "Concept", "uuid": "MQ==-U2VjdGlvbnM=", "_rel": { "created": 1489079442237, "count": 1, "updated": 1489079442237 } }, { "nodeRank": 39967, "name": "UK Politics & Policy", "taxonomy": "sections", "type": "Concept", "uuid": "OA==-U2VjdGlvbnM=", "_rel": { "created": 1489079426607, "count": 1, "updated": 1489079426607 } }, { "nodeRank": 3735, "name": "Driverless Cars", "taxonomy": "topics", "uuid": "NjMzNzEzODEtZjAzNC00ZTQ0LWIzNTUtYzQyMDcyOTE5ZGMw-VG9waWNz", "_rel": { "created": 1488834945890, "count": 1, "updated": 1488834945890 } }, { "nodeRank": 6210, "name": "Tesla Motors", "taxonomy": "organisations", "uuid": "NmI1MjkxNGQtYjAyYi00NTFkLWIxYmItOWFiMmY4NjcxYTRl-T04=", "_rel": { "created": 1488834936239, "count": 1, "updated": 1488834936239 } }, { "nodeRank": 2365, "name": "Tim Harford", "taxonomy": "authors", "type": "Concept", "uuid": "Q0ItMDAwMDk4OQ==-QXV0aG9ycw==", "_rel": { "created": 1484559153611, "count": 2, "updated": 1488835709195, "instant": true } }, { "nodeRank": 2180, "name": "Artificial Intelligence and Robotics", "attributes": [], "taxonomy": "topics", "type": "Concept", "uuid": "ZTBiZDJjNjktZmM5OS00ZjMyLWE3MDgtNmMyZTRlOGQ3NjFj-VG9waWNz", "_rel": { "created": 1464793309223, "count": 1, "updated": 1464793309223 } }, { "nodeRank": 5359, "name": "Elon Musk", "taxonomy": "people", "uuid": "TnN0ZWluX1BOX0VudGVydGFpbm1lbnRfMjAwOV8xMF85XzQ0MTk2-UE4=", "_rel": { "created": 1462963787560, "count": 1, "updated": 1462963787560 } }], "count": 9 }
  stub(largeObject)

  const expected = { "user": { "_id": 422985, "labels": ["User", "Flagged"], "properties": { "nodeRank": 7, "uuid": "28875745-3186-4df0-8372-12e9556881b7" } }, "total": 9, "items": [{ "nodeRank": 902, "name": "The College of the Holy and Undivided Trinity in the University of Oxford", "taxonomy": "organisations", "uuid": "TnN0ZWluX09OX1VLX0NvbGxlZ2VfMzc=-T04=", "_rel": { "created": 1490778694957, "count": 1, "updated": 1490778694957 } }, { "name": "Theo Leanse", "taxonomy": "authors", "uuid": "ZDRiY2JlMjgtYWE4Yi00YjdmLTkyNWYtNDk0NTAxNmNjM2Vk-QXV0aG9ycw==", "_rel": { "created": 1489665098854, "count": 1, "updated": 1489665098854 } }, { "nodeRank": 66986, "name": "World", "taxonomy": "sections", "type": "Concept", "uuid": "MQ==-U2VjdGlvbnM=", "_rel": { "created": 1489079442237, "count": 1, "updated": 1489079442237 } }, { "nodeRank": 39967, "name": "UK Politics & Policy", "taxonomy": "sections", "type": "Concept", "uuid": "OA==-U2VjdGlvbnM=", "_rel": { "created": 1489079426607, "count": 1, "updated": 1489079426607 } }, { "nodeRank": 3735, "name": "Driverless Cars", "taxonomy": "topics", "uuid": "NjMzNzEzODEtZjAzNC00ZTQ0LWIzNTUtYzQyMDcyOTE5ZGMw-VG9waWNz", "_rel": { "created": 1488834945890, "count": 1, "updated": 1488834945890 } }, { "nodeRank": 6210, "name": "Tesla Motors", "taxonomy": "organisations", "uuid": "NmI1MjkxNGQtYjAyYi00NTFkLWIxYmItOWFiMmY4NjcxYTRl-T04=", "_rel": { "created": 1488834936239, "count": 1, "updated": 1488834936239 } }, { "nodeRank": 2365, "name": "Tim Harford", "taxonomy": "authors", "type": "Concept", "uuid": "Q0ItMDAwMDk4OQ==-QXV0aG9ycw==", "_rel": { "created": 1484559153611, "count": 2, "updated": 1488835709195, "instant": true } }, { "nodeRank": 2180, "name": "Artificial Intelligence and Robotics", "attributes": [], "taxonomy": "topics", "type": "Concept", "uuid": "ZTBiZDJjNjktZmM5OS00ZjMyLWE3MDgtNmMyZTRlOGQ3NjFj-VG9waWNz", "_rel": { "created": 1464793309223, "count": 1, "updated": 1464793309223 } }, { "nodeRank": 5359, "name": "Elon Musk", "taxonomy": "people", "uuid": "TnN0ZWluX1BOX0VudGVydGFpbm1lbnRfMjAwOV8xMF85XzQ0MTk2-UE4=", "_rel": { "created": 1462963787560, "count": 1, "updated": 1462963787560 } }], "count": 9 }

  expect(stub).to.have.been.calledWith(expected)
});

Because of this I've had to replace

expect(stub).to.have.been.calledWith(expected)

with

expect(stub.getCall(0).args[0]).to.deep.equal(expected)

If you do the same you'll see that it is a much nicer error (and I'm sure you'd agree that the calledWith syntax is much nicer).

What I propose somehow being able pass back to the user the deep.equal assertion error.

Thoughts?

If you think this would be a good idea (if it even is very possible, you'd know better than me) then I'd be happy to attempt a PR at some point

The error we get is directly from Sinon. You should file this on them.

Right sure thing. Thanks for letting me know. Does sinon-chai still just pass through any assertion error from sinon then?

Yep!