How to check whether image exist using chai or mocha?

by elisha_langworth , in category: Javascript , 8 months ago

How to check whether image exist using chai or mocha?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jerad , 7 months ago

@elisha_langworth 

To check whether an image exists using Chai or Mocha, you can first check if the image element exists in the DOM by using a selector or ID and then assert that the image's src attribute is not empty. Here is an example using Chai and Mocha:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const { expect } = require('chai');

describe('Image Existence Test', () => {
  it('should check if image exists', () => {
    const imageElement = document.querySelector('img'); // Assuming the image element exists in the DOM
    expect(imageElement).to.exist;
    
    const imageUrl = imageElement.getAttribute('src');
    expect(imageUrl).to.not.be.empty;
  });
});


This test will check if the image element exists in the DOM and if the src attribute of the image is not empty. If both conditions are met, the test will pass.

Related Threads:

How to check the type of a nested property in mocha-chai?
How to use promise using mocha and chai?
How to test vuex module using mocha and chai?
How to check if database row exist in laravel?
How to check whether index is being used or not in oracle?
How to check whether iframe loads content?