chai js expect property value empty array
Asked Answered
P

6

17

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

Phenomenology answered 14/3, 2016 at 20:17 Comment(2)
There is always the "empty" assertion chaijs.com/api/bdd/#emptyWinna
@JasonSperske: But will it work if I have to check the value of a property? And not just a simple variable?Phenomenology
P
10

I ignored there's a section of change in assertion for properties. So, what made it work for me was :

return expect(functionRetuningPromise()).to.eventually.have.property("key1").that.eql([]);
Phenomenology answered 14/3, 2016 at 20:38 Comment(2)
Are you sure that's working? [] == [] is false (reference equality)!Guereza
yes, this works, because of the use of eql instead of equal. Eql makes use of the deep-eql-project (medium.com/building-ibotta/…)Henrik
A
17

I think this is a little plainer

expect( value ).to.be.an( "array" ).that.is.empty
Ariannearianrhod answered 18/2, 2022 at 13:40 Comment(0)
P
10

I ignored there's a section of change in assertion for properties. So, what made it work for me was :

return expect(functionRetuningPromise()).to.eventually.have.property("key1").that.eql([]);
Phenomenology answered 14/3, 2016 at 20:38 Comment(2)
Are you sure that's working? [] == [] is false (reference equality)!Guereza
yes, this works, because of the use of eql instead of equal. Eql makes use of the deep-eql-project (medium.com/building-ibotta/…)Henrik
G
7

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;
})
Guereza answered 14/3, 2016 at 20:49 Comment(0)
M
4

This should do the trick

expect(value).to.deep.equal([]);
Montherlant answered 6/12, 2021 at 8:52 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Sermonize
Be careful, this solution is not correct. This will pass even when your array has elementsUndergraduate
K
1

In chaiJs, I do the following. Check if it is an array and has a length of 0.

expect(array).to.be.instanceOf(Array).and.lengthOf(0);
Kallman answered 27/4, 2023 at 19:24 Comment(0)
V
0

Here is a possible solution with chai.js and should:

const should = chai.should();

res.should.have.lengthOf(0)

Link for more information https://www.chaijs.com/guide/styles/#should.

Ventilate answered 8/9, 2022 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.