Using Chai, how can I see whether the element For example, a div with the class .avatar
exist?
I tried to.exist
but it's not working.
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.
Differences between expect and should First of all, notice that the expect require is just a reference to the expect function, whereas with the should require, the function is being executed. var chai = require('chai') const expect = chai. expect const should = chai.
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.
BDD style assertions with Chai. Chai is a TDD and BDD library, and comes with several flavors for assertions: should, expect and assert. In the following lessons we will take a look at both styles and see how to use them in practice.
The exist
language chain in vanilla Chai is used to verify that a plain old JavaScript object is neither undefined
nor null
. It sounds like in your case you actually want to be asserting against a DOM element, which vanilla Chai can't do. Instead, you'll need to pull in a Chai plugin like chai-jquery
along with jQuery
for DOM manipulation. chai-jquery
allows you to write assertions against elements you have located using jQuery
, and even provides and overriden form of exist
that would likely serve your purpose exactly. All together, the code you're looking for to assert a div
with class avatar
exists would look something like this:
expect($('div.avatar')).to.exist;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With