I'm following a simple tutorial to learn react testing library.
I have a simple component like:
import React, {useState} from 'react';
const TestElements = () => {
const [counter, setCounter] = useState(0)
return(
<>
<h1 data-testid="counter" >{ counter }</h1>
<button data-testid="button-up" onClick={() => setCounter(counter + 1)}>Up</button>
<button data-testid="button-down" onClick={() => setCounter(counter - 1)}>Down</button>
</>
)
}
export default TestElements
And a test file like:
import React from 'react';
import {render, cleanup} from '@testing-library/react'
import TestElements from './TestElements';
afterEach(cleanup);
test('should equal to 0', () => {
const { getByTestId } = render(<TestElements />)
expect(getByTestId('counter')).toHaveTextContent(0)
});
But if I run the test, I'm getting the following error:
TypeError: expect(...).toHaveTextContent is not a function
I'm using create-react-app and the package.json
shows:
"@testing-library/jest-dom": "^4.2.4",
Do I need to add jest as well?
"@testing-library/jest-dom": "^4.2.4",
is included when you use create-react-app – Gendersrc/setupTests.js
(assuming you're using the default template). But what changes have you made, is this reproducible in a brand new CRA app? What's the tutorial? – Godsend@testing-library/jest-dom/extend-expect
at one point, maybe that tutorial uses an older version where that would work. Or you're welcome to give mine a go! – Godsend