react-testing-library: expect(...).toHaveTextContent is not a function
react-testing-libraryversion: 8.0.1reactversion: 16.8.6nodeversion: 10.15.2npm(oryarn) version: 6.4.1
Relevant code or config:
Component
import React, { useState, useEffect } from 'react';
const ClickCounter = (props) => {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
}, [count]);
return (
<div>
<p>You clicked {count} times</p>
<button data-testid="click-counter-button" onClick={() => setCount(count => count + 1)}>
Click Me
</button>
</div>
)
}
export default ClickCounter;
Test
test('component updates count when clicked', () => {
const { getByText, getByTestId, asFragment } = render(
<ClickCounter />
);
fireEvent.click(getByText('Click Me'));
// const countTextNode = getByText(/You click.*\d.*times/i);
const countTextNode = getByTestId('click-counter-button');
expect(countTextNode).toHaveTextContent('You clicked 1 times');
});
What you did:
Just following the react-testing-library example to write a test for the React Hooks example.
What happened:
TypeError: expect(...).toHaveTextContent is not a function
Reproduction:
https://github.com/atav32/react-testing-toHaveTextContent-bug
Just run npm test
Problem description:
I expected the test to pass, since it’s in the Readme example.
Did something change in jest-dom and the example needs to be updated? I took a peek at that library and the toHaveTextContent() is still part of the API.
This is probably just me not understanding the jest-dom API or a dependencies issue.
Suggested solution:
Not really a solution, but a workaround seems to work
expect(countTextNode.textContent).toBe('You clicked 1 times');
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 14
- Comments: 18 (5 by maintainers)
Commits related to this issue
- refactor(tab): remove tabIndex set and unset for jsdom (#379) — committed to lucbpz/react-testing-library by juanca 4 years ago
- add App.test.tsx test with react testing library - enhance jest.config.js 1. add moduleDirectories: ['node_modules', 'src'], so jest can find modules in src folder instead of writing test like this... — committed to bobbyquennell/peggy by bobbyquennell 3 years ago
For me this fixed:
import "@testing-library/jest-dom/extend-expect";Ok nvm, I missed this line
My preference would be to keep the example as barebones as possible in order to highlight how to use
react-testing-library. Throwing in a function fromjest-domwas a red herring.I never expect examples to have additional dependencies that I have to manually install, unless they’re explicitly stated in big bold instructions. I thought
jest-domwas automatically included inreact-testing-library, just likejsdomis included injest.My suggestion would be to remove the
jest-domdependency from the example and change the lineto
If the authors/maintainers agree, then I can make a PR.
Create that
setupTest.jsfile and put this in it:That’ll do it 😃
You might also appreciate https://github.com/kentcdodds/bookshelf as an example of a create-react-app based app that does this.
Agreed, I think we should just add a comment explaining where the assertion is coming from
In my project I wasn’t using any transformers so
importdidn’t work in my jest-setup.js, but this did:Oh! I had understood to do this:
so close, but no cigar. THanks Kent!
what is the trick with a create-react-app & react-testing to have
setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],implemented correctly?If I try to put
setupFilesAfterEnvinside thejestobject of package.json, it tells me to create asetupTests.jsfile…so I do that. Then when I try to use an expected assertion such astoBeCheckedortoHaveAttributeit says they are not functions.Without create-react-app if I have a jest.config file the setup is easier IMO. Please advise. thanks!
Workable solution: setupTests.js
import "@testing-library/jest-dom/extend-expect";I’ve updated the README to be more descriptive.
I’m not sure I agree with your suggestion. That is an example of the usage of react testing library with jest, hence the addition of the jest-dom helpers. react testing library also does not depend on jest, it can be used with other test runners and assertion libraries. That’s why jest-dom is not included by default. But if the example already uses jest, why not use also jest-dom? As you said the instruction to import the dependency was right there.
If you remove it, you’d also have to avoid using the
toHaveAttribute('disabled')in the line next to the one that failed withtoHaveTextContent.