title: Chai assertion testing whether object structure contains at least other object structure
body: <p>I'm using Mocha for unit testing and Chai for assertions.</p>
  
  <p>I'd like to find an easy to use solution to check if an object has the structure and properties as defined in my comparison object. But I don't need the objects to be completely equal. The subject under test should contain <em>at least</em> all the properties in my test object, but it might also contain other properties which are not under test at that moment. </p>
  
  <p>So, I want to test a unit to check if the object it returns has at least a property named 'foo', which itself is an object that at least contains the property 'bar' with value 10. So, I have this expected outcome to test against:</p>
  
  <pre><code>var expected = {foo: {bar: 10}};
  </code></pre>
  
  <p>I call my unit and have my test subject in a variable <code>sut</code>:</p>
  
  <pre><code>var sut = MyUnit.myFunction();
  </code></pre>
  
  <p>So for various suts, I expect these outcomes:</p>
  
  <pre><code>// Success. Exact match
  {foo: {bar: 10}}
  
  // Fail. Structure is ok, but property value is wrong.
  {foo: {bar: 11}}
  
  // Fail. property bar is missing.
  {foo: {qux: 20}}
  
  // Success. All properties match. Extra properties (baz) in sut are ignored:
  {baz: 'a', foo: {bar: 10, baz: 20}}
  </code></pre>
  
  <p>And then I want to compare it in a convenient way. I could test all properties individually or split it in a number of tests, but I hoped I could do something like</p>
  
  <pre><code>sut.should.deep.contain.all(expected);
  </code></pre>
  
  <p>However, I got the following surprising result, even if the objects are exactly the same:</p>
  
  <blockquote>
    <p>AssertionError: expected { foo: { bar: 10 } } to have a property 'foo' of { bar: 10 }, but got { bar: 10 }</p>
  </blockquote>
  
  <p>Of course I tried this, and a couple of other variations. The simplest testing for equality, doesn't work if the object contains extra properties.</p>
  
  <pre><code>sut.should.eql(expected);
  </code></pre>
  
  <blockquote>
    <p>AssertionError: expected { foo: { bar: 10, qux: 20 } } to deeply equal { foo: { bar: 10 } }</p>
  </blockquote>
  
  <p>I've tested other combinations of <code>have</code> and <code>contains</code> together with <code>deep</code>, <code>any</code> or <code>all</code>, but none satisfied my wish. </p>
  
  <p>I found the duplicate question "<a href="https://stackoverflow.com/questions/29368318/chai-deep-contains-assertion-on-nested-objects">Chai deep contains assertion on nested objects</a>", but the only (downvoted) answer doesn't make sense. It calls <code>deep.eql</code> which is redundant, and above that it just doesn't work, because it tests for strict equality.</p>
  
  <p>I do know I can test all properties separately, but I would be nice to have a readable, single-statement way to test if one object is 'a subset of' the other.</p>
  
  <p><strong>Update:</strong>
  I ended up using the <a href="http://chaijs.com/plugins/chai-shallow-deep-equal" rel="noreferrer">Shallow Deep Equal</a> plugin for Chai.</p>