debitoor / chai-subset

"containSubset" object properties matcher for Chai
http://chaijs.com/plugins/chai-subset/
MIT License
82 stars 20 forks source link

AssertionError diff is confusing #59

Closed liquorvicar closed 7 years ago

liquorvicar commented 7 years ago
    it('should detect a subset correctly', function () {
        expect({ foo: "foo", bar: "bar" }).to.containSubset({ foo: "baz" });
    });

I would expected this to fail with a message along the lines of

      AssertionError: expected { foo: 'foo', bar: 'bar' } to contain subset { foo: 'baz' }
      + expected - actual

       {
      -  "foo": "foo"
      +  "foo": "baz"
       }

However it failed with the message

     AssertionError: expected { foo: 'foo', bar: 'bar' } to contain subset { foo: 'baz' }
      + expected - actual

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

(Adding the extra line highlighting the bar property into the diff).

Is this intended? Personally I found it very confusing (this was my first time with this chai plugin) as I thought initially the plugin wasn't working.

Given that my test has specified I'm not interested in the bar property, should a failure highlight that property? In my real example, the object I was comparing to the subset had a lot more properties so it was a confusing assertion error.

From what I can see it's the reporter that's generating the diff so not sure if the issue lies with that rather than this plugin?

Would be interested in your thoughts.

eagleeye commented 7 years ago

this should be addressed to core chai module, that we are using to generate diff: https://github.com/debitoor/chai-subset/blob/master/lib/chai-subset.js#L13

var Assertion = chai.Assertion;
andreyrd commented 7 years ago

I think the issue is that chai-subset still uses the core chai module to generate the diff. Chai's core diff is designed to highlight the differences between two objects for an exact match, not for a subset. A subset diff is only supposed to highlight differences that are missing, not extra differences.