Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether the element exist using chai?

Tags:

chai

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.

like image 712
THpubs Avatar asked Apr 29 '16 06:04

THpubs


People also ask

How do you assert chai?

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.

Should you expect chai?

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.

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.

Which assertion styles are considered BDD style in testing?

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.


1 Answers

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;
like image 115
Nathan Thompson Avatar answered Oct 22 '22 20:10

Nathan Thompson