Chai.js: Object contains/includes

user1082754 picture user1082754 · Feb 28, 2013 · Viewed 22.9k times · Source

Chai has an include method. I want to test to see if an object contains another object. For example:

var origin = {
  name: "John",
  otherObj: {
    title: "Example"
  }
}

I want to use Chai to test if this object contains the following (which it does)

var match = {
  otherObj: {
    title: "Example"
  }
}

Doing this does not appear to work:

origin.should.include(match)

Answer

eagleeye picture eagleeye · Jul 15, 2014

Hei, just published chai-subset. Check this out: https://www.npmjs.org/package/chai-subset This should work for you)

 var chai = require('chai');
 var chaiSubset = require('chai-subset');
 chai.use(chaiSubset);

 var obj = {
     a: 'b',
     c: 'd',
     e: {
         foo: 'bar',
         baz: {
             qux: 'quux'
         }
     }
 };

 expect(obj).to.containSubset({
     e: {
         foo: 'bar',
         baz: {
             qux: 'quux'
         }
     }
 });