Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match partial objects in Chai assertions?

Tags:

chai

I am looking for the best way to match the following:

expect([     {         C1: 'xxx',         C0: 'this causes it not to match.'     } ]).to.deep.include.members([     {         C1: 'xxx'     } ]); 

The above doesn't work because C0 exists in the actual, but not the expected. In short, I want this expect to PASS, but I'm not sure how to do it without writing a bunch of custom code...

like image 251
JayPrime2012 Avatar asked Apr 09 '15 08:04

JayPrime2012


People also ask

What is Chai's assertion style to compare the contents of objects?

Important Concept: By default, all assertions in Chai are performing a strict equality comparison. Thus, asserting that an array of objects has a member object will cause those two objects to be compared strictly. Adding in the deep flag signals to the assertion to instead use deep equality for the comparison.

Should I assert vs vs expect?

Note expect and should uses chainable language to construct assertions, but they differ in the way an assertion is initially constructed. In the case of should , there are also some caveats and additional tools to overcome the caveats. var expect = require('chai').

What is deep equal in Chai?

Chai plugin to match objects and arrays deep equality with arrays (including nested ones) being in any order. It works in similar way as deep. equal but it doesn't checks the arrays order (at any level of nested objects and arrays).

What assertion styles are present in Chai testing assertion library?

Chai is such an assertion library, which provides certain interfaces to implement assertions for any JavaScript-based framework. Chai's interfaces are broadly classified into two: TDD styles and BDD styles.


1 Answers

chai-subset or chai-fuzzy might also perform what you're looking for.

Chai-subset should work like this:

expect([   {     C1: 'xxx',     C0: 'this causes it not to match.'   } ]).to.containSubset([{C1: 'xxx'}]); 

Personally if I don't want to include another plugin I will use the property or keys matchers that chai includes:

([   {     C1: 'xxx',     C0: 'this causes it not to match.'   } ]).forEach(obj => {   expect(obj).to.have.key('C1'); // or...   expect(obj).to.have.property('C1', 'xxx'); }); 
like image 184
thom_nic Avatar answered Sep 21 '22 03:09

thom_nic