debitoor / chai-subset

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

Introduce strict equality check in compare() #57

Closed oleg-codaio closed 7 years ago

oleg-codaio commented 7 years ago

If two objects are the same, then inherently they're subsets of each other. This also fixes a bug I've run into with testing recursive data structures.

eagleeye commented 7 years ago

Hi @vaskevich! Can you write a test that shows that problem or bug?

oleg-codaio commented 7 years ago

Hey @eagleeye - sorry for the delay; this fell off my radar!

Sure, here's a simple, contrived test case that fails with a stack overflow:

'use strict';
const testHelper = require('../../test_helper');

describe('Subset test', () => {
  it('should pass', () => {
    const child = {};
    const parent = {
      children: [child],
    };
    child.parent = parent;

    const myObject = {
      a: 1,
      b: 'two',
      c: parent,
    };
    assert.containSubset(myObject, {
      a: 1,
      c: parent,
    });
  });
});

Essentially, parent and child are circular data structures, and because containSubset keeps trying to recurse down, this test results in a stack overflow. With the proposed change, the test would pass, and that change also serves as an optimization as well.

oleg-codaio commented 7 years ago

bump!

eagleeye commented 7 years ago

@vaskevich Thanks! Published as chai-subset@1.5.0 with test that you provided!

oleg-codaio commented 7 years ago

Woohoo! 🙌