Empty assertion
Empty string
cy.wrap('').should('be.empty')
cy.wrap('Hello').should('not.be.empty')
Note: trying to use "empty" assertion with undefined
, null
, or numbers will lead to an error.
// 🚨 DOES NOT WORK
cy.wrap(undefined).should('be.empty')
cy.wrap(null).should('be.empty')
cy.wrap(0).should('be.empty')
Empty array
cy.wrap([]).should('be.empty')
cy.wrap(['Hello']).should('not.be.empty')
// any value inside the array
// makes it not empty
cy.wrap(['']).should('not.be.empty')
cy.wrap([0]).should('not.be.empty')
cy.wrap([null]).should('not.be.empty')
cy.wrap([undefined]).should('not.be.empty')
Empty object
cy.wrap({}).should('be.empty')
cy.wrap({ name: 'Joe' }).should('not.be.empty')
jQuery object with length zero is never empty
<ul>
<li class="count">One</li>
<li class="count">Two</li>
</ul>
Even if there are not DOM elements found, the jQuery object is not empty.
cy.get('li.selected')
.should('not.be.empty')
.and('have.length', 0)
cy.get('li.count').should('not.be.empty').and('have.length', 2)
jQuery element
<ul id="one"></ul>
<ul id="two">
<li>One</li>
</ul>
A jQuery object without children is empty, but the list of its children ... is not empty 🤣
cy.get('ul#one')
.should('be.empty')
.invoke('children')
.should('not.be.empty')
.and('have.length', 0)
Let's look at the list element with one child.
cy.get('ul#two')
.should('not.be.empty')
.invoke('children')
.should('not.be.empty')
.and('have.length', 1)