如何使用chai检查元素是否存在?

How to check whether the element exist using chai?

使用 Chai,如何查看元素 For example, a div with the class .avatar 是否存在?

我尝试了 to.exist 但它不起作用。

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 可能完全符合您的目的。总之,您正在寻找断言 div 和 class avatar 存在的代码看起来像这样:

expect($('div.avatar')).to.exist;