Search code examples
reactjsjestjsreact-testing-library

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:

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';