Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jest and react-test library (…).toBeVisible is not a function

My unit test for react component throw error even if debug print the correct result.

TypeError: expect(…).toBeVisible is not a function

debug(screen.getByText('contains'))
expect(screen.getByText('contains')).toBeVisible();

this print:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

console.log
      <div
        class="sc-jOhDuK iDFeaX"
      >
        contains
      </div>

I can’t figure out the reason why toBeVisible is not a function even if debug print the correct result.

>Solution :

You will need to import or require in your individual test file.

import '@testing-library/jest-dom';

or

require('@testing-library/jest-dom');

For example:

import { render, cleanup, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading