Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chai: assert array includes all items

Tags:

Is it possible to assert that an array includes multiple specific items using chai?

For example, I would expect this to work:

['foo', 'bar'].should.include(['foo', 'bar']) 

Instead chai throws: "expected [ 'foo', 'bar' ] to include [ 'foo', 'bar' ]"

I also tried this but it only asserts that the first item is present:

['foo', 'bar'].should.include('foo', 'bar') // variable args instead of array 

What am I missing?

like image 660
mockaroodev Avatar asked Jul 01 '15 00:07

mockaroodev


People also ask

How do you assert chai?

var assert = require('chai'). assert , foo = 'bar' , beverages = { tea: [ 'chai', 'matcha', 'oolong' ] }; assert. typeOf(foo, 'string'); // without optional message assert. typeOf(foo, 'string', 'foo is a string'); // with optional message assert.

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

In Chai. js, the equal assertion, along with most other included Chai assertions, uses Javascipt's strict equality. During testing, it often is the case that strict equality is not desired, but instead equality in the sense that two objects are “equivalent” in that they have the same content.

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.

What is deep equal in Chai?

So when comparing objects, Chai will check for reference. When you use deepEqual , Chai will go down the object hierarchy and compare every value of every property. Example: const a = {"a": "a"}; const b = {"a": "a"}; expect(a).


1 Answers

You can use members to expect array members.

Ex:

expect(['foo', 'bar']).to.include.members(['foo', 'bar'])
like image 99
Douglas Liu Avatar answered Oct 05 '22 12:10

Douglas Liu