debitoor / chai-subset

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

Is this lib still necessary? #73

Open stevenvachon opened 4 years ago

stevenvachon commented 4 years ago

... now that chai has deep.contain()

aloisklink commented 4 years ago

I believe it is, since deepInclude only works from the first level (see https://www.chaijs.com/api/assert/#method_deepinclude):

const subset = {
    a: 'b',
    e: {
                // missing "foo": "bar"
        baz: {qux: 'quux'},
    }
};
const obj = {
    a: 'b',
    c: 'd',
    e: {
        foo: "bar",
        baz: {qux: "quux"},
    }
};

// PASSES
expect(obj).to.containSubset(subset);

// FAILS, as obj.e does not contain "foo": "bar".
expect(obj).deep.contain(subset);
kkm000 commented 3 years ago

Another feature not offered by chai, AFAIK, is the ability to provide an asserting function at any level.

Test that an object contains the key axes, which in turn contains distro, which in turn has something under an empty string key, do not care about the value:

    assert.containSubset(control.BBCI, {
      axes: { distro: { '': () => true } }
    });

Or even a more complex scenario with a nested assertion:

    assert.containSubset(control.BBCI.stages, {
      gcb_stage: {
        gcb: (x: unknown[]) => {
          assert.strictEqual(2, x.length);
          return true; } } });