Alexander Elgin
Alexander Elgin

Reputation: 6956

What are the cases when should.deep.equal fails but comparison using JSON.stringify works fine?

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?

Upvotes: 1

Views: 843

Answers (1)

Alexander Elgin
Alexander Elgin

Reputation: 6956

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

Upvotes: 1

Related Questions