Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chai js expect property value empty array

I'm trying to write a unit test using chai js assertion, and was wondering how to expect arrays with zero length as values.

My Test function expect statement:

return expect(functionRetuningPromise()).to eventually.have.property("key1", []);

Console Output on running mocha:

AssertionError: expected { otherkey: otherVal, key1: [] } to have a property 'key1' of [], but got []

I have tried deep.property, key1:"[]" with no success

like image 276
Ashwini Khare Avatar asked Mar 14 '16 20:03

Ashwini Khare


People also ask

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 assert chai?

Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

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.


3 Answers

This should do the trick

expect(value).to.deep.equal([]);
like image 149
mstfgueclue Avatar answered Sep 19 '22 01:09

mstfgueclue


I think this is a little plainer

expect( value ).to.be.an( "array" ).that.is.empty
like image 43
Brandy Thomason Avatar answered Sep 17 '22 01:09

Brandy Thomason


What about

return 

expect(functionRetuningPromise()).to.eventually.have.property("key1").that.satisfy(function (value) {
  expect(value).to.be.instanceof(Array);
  expect(value).to.have.length.above(0);
  return true;
})
like image 36
Jim Pedid Avatar answered Sep 21 '22 01:09

Jim Pedid