-
Notifications
You must be signed in to change notification settings - Fork 1.1k
expect(...).toHaveTextContent is not a function #379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Just realized that it's preferred to ask these types of questions on Spectrum. Feel free to close and reply here, https://spectrum.chat/testing-library/help-react/expect-tohavetextcontent-is-not-a-function~1b79c850-369b-4b67-88e8-ab27fa5d3bfe |
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 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 My suggestion would be to remove the expect(getByTestId('greeting-text')).toHaveTextContent('hello there') to expect(getByTestId('greeting-text').textContent).toBe('hello there') If the authors/maintainers agree, then I can make a PR. |
Thanks for pointing this out. Yes, a PR would be welcome to remove usage of extra dependencies from the example. |
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 |
Agreed, I think we should just add a comment explaining where the assertion is coming from |
@kentcdodds I'm on board with that @gnapse good point that |
I've updated the README to be more descriptive. |
For me this fixed:
|
what is the trick with a create-react-app & react-testing to have If I try to put Without create-react-app if I have a jest.config file the setup is easier IMO. Please advise. thanks! |
Create that // src/setupTest.js
import '@testing-library/jest-dom/extend-expect' 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. |
Oh! I had understood to do this:
so close, but no cigar. THanks Kent! |
That's what you'd normally do, but CRA doesn't allow you to customize Jest, so you have to put the import in the |
Workable solution: |
@stephyswe That worked for me |
I think there is a typo here. File name should be |
@chety in |
- enhance jest.config.js 1. add moduleDirectories: ['node_modules', 'src'], so jest can find modules in src folder instead of writing test like this: import greeter from './greeter' we can: import greeter from 'greeter'; 2. add setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'] to support extended handy expect check: testing-library/react-testing-library#379 (comment) - add "esModuleInterop": true in tsconfig.json to fix: Cannot read property 'createElement' of undefined using TypeScript testing-library/react-testing-library#374 (comment) esModuleInterop vs allowSyntheticDefaultImports note: Enabling esModuleInterop will also enable allowSyntheticDefaultImports. - add a new text example for react component: App.test.tsx
In my project I wasn't using any transformers so
|
Thanks! |
react-testing-library
version: 8.0.1react
version: 16.8.6node
version: 10.15.2npm
(oryarn
) version: 6.4.1Relevant code or config:
Component
Test
What you did:
Just following the react-testing-library example to write a test for the React Hooks example.
What happened:
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 thetoHaveTextContent()
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
The text was updated successfully, but these errors were encountered: