1

A function returns an array of objects. When I compare the actual result with an expected result using JSON.stringify the values are identical. However comparison using should.deep.equal or _.isEqual fails. Had anyone encountered such issue?

Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50
  • 1
    Could you add some sample objects to your question? – Quill Apr 22 '16 at 03:46
  • 1
    I'm not familiar with `should.js`, but is it possible that they are checking not just values but references also? The underscore issue seems to be the same as [this SO question](http://stackoverflow.com/questions/19316043/underscore-isequal-and-json). – Matthew Herbst Apr 22 '16 at 03:46

1 Answers1

1

I've realized what the issue was. It is caused by the fact that JSON.stringify misses fields with undefined values. E.g. result of applying JSON.stringify to the following object {field: 'value', undefinedField: undefined} is {"field": "value"}. Hence

JSON.stringify({field: 'value', undefinedField: undefined}) === JSON.stringify({field: 'value'})`

But

{field: 'value', undefinedField: undefined}.should.deep.equal({field: 'value'})

fails

Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50