Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using should.js how can I test for an empty object?

user1Stats.should.be.instanceof(Object);
(user1Stats).should.have.keys();

I get the following error:

Error: keys required
    at Object.Assertion.keys

The instanceof(Object) works, but I want to make sure there is no data in it.

like image 823
chovy Avatar asked Aug 17 '14 00:08

chovy


People also ask

How do you test for an empty object?

keys(object) method: The required object could be passed to the Object. keys(object) method which will return the keys in the object. The length property is used to the result to check the number of keys. If the length property returns 0 keys, it means that the object is empty.

How do you check if an object has a blank key value?

keys method to check for an empty object. const empty = {}; Object. keys(empty). length === 0 && empty.

Is Empty object true in JS?

There are only seven values that are falsy in JavaScript, and empty objects are not one of them. An empty object is an object that has no properties of its own. You can use the Object.


1 Answers

user1Stats.should.be.an.Object();
user1Stats.should.be.empty();

or, using .and to chain both asserts:

user1Stats.should.be.an.Object().and.be.empty();

P.S. By the way, your code also looks fine.

like image 114
Leonid Beschastny Avatar answered Sep 19 '22 16:09

Leonid Beschastny