chaijs / sinon-chai

Extends Chai with assertions for the Sinon.JS mocking framework.
Other
1.09k stars 107 forks source link

Add better diffing for `calledWith` and `calledWithExactly` failures #124

Open bskiff opened 6 years ago

bskiff commented 6 years ago

Currently when a test fails it provides output the following way: e.g.

const fooObject = {
    foo: 'foo'
};
const barObject = {
    bar: 'bar'
};
const stub = sinon.stub();

stub(fooObject);

expect(stub).to.be.calledWithExactly(barObject);

prints

AssertionError: expected stub to have been called with exact arguments { bar: "bar" }
{ foo: "foo" } { bar: "bar" }

It would be nice if this was a proper diff similar to what Chai's deep equal assertions provide. e.g.

const fooObject = {
    foo: 'foo'
};
const barObject = {
    bar: 'bar'
};

expect(fooObject).to.deep.equal(barObject);

prints

 AssertionError: expected { foo: 'foo' } to deeply equal { bar: 'bar' }                        
 + expected - actual                                                                           

  {                                                                                            
 -  "foo": "foo"                                                                               
 +  "bar": "bar"                                                                               
  }                                                                                             

Editors such as IntelliJ IDEA have support for the latter style and will provide a nicely formatted diff in their diffing tools if that convention is followed.

troynguyen8 commented 5 years ago

Any status on this? My team is facing this issue. Using calledWithExactly is much cleaner than expect(spy.lastCall.args).to.equal(expectedCall)

domenic commented 5 years ago

https://mobile.twitter.com/slicknet/status/782274190451671040

kuceb commented 5 years ago

I've got a PR open here #141

AdRaslan commented 5 years ago

I had the same problem and it was solved by using JSON.stringify(). Try this:

const fooObject = { foo: 'foo' }; const barObject = { bar: 'bar' }; const stub = sinon.stub();

stub(fooObject);

expect(stub).to.be.calledWithExactly(JSON.stringify(barObject));

benjick commented 2 years ago

Any news on this?

alannaidon commented 2 years ago

Any news? :eyes:

eliasm307 commented 11 months ago

Any news on this?