Search code examples
javascriptreactjsreact-testing-librarytesting-library

Why is my header component defined but never used in my test?


Im trying to get a react testing library test going but having lots of issues

import React from 'react'
import Header from './Header'
import {render, cleanup} from '@testing-library/react'

test("Renders's the header and required inputs", () => {
  const { getByTestId } = render(<Header/>);
  const Header = component.getByTestId ('header');
  expect(Header.textContent).toBe("Shipping Label")
})

The component exports Header.


Solution

  • import React from 'react'
    import Header from './Header'
    import {render} from '@testing-library/react'
    
    test("Renders's the header and required inputs", () => {
     const { getByText, getByTestId } = render(<Header />);
      expect(getByTestId("header")).toBeTruthy();
      expect(getByText("Shipping Label")).toBeTruthy();
    })
    

    Try something like this, I think you are trying to use "component" from enzyme or such, and it's not defined.

    Take a look at the testing library cheat sheet