[jest-dom] TypeError: expect(...).toBeInTheDocument is not a function
See original GitHub issueCurrent Behavior
I have configured jest.config.js globally and added setupFilesAfterEnv as explained in https://github.com/testing-library/jest-dom
But setupFilesAfterEnv is not injecting jest.setup.ts file to tests. Thus, compiler cannot find matcher functions of jest-dom.
Note: If I configure it on app/lib level, it’s working. But global configuration is not working
Expected Behavior
import "testing-library/jest-dom"
should be injected to every test file.
Steps to Reproduce
- Create jest.setup.ts file in nx workspace root and import ‘@testing-library/jest-dom’.
- Add
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts']
to global jest.config.js - Use any jest-dom matcher in your test (Example: toBeInTheDocument).
- Run that test.
Failure Logs
Environment
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:14 (3 by maintainers)
Top Results From Across the Web
react-testing-library why is toBeInTheDocument() not a function
Has anyone got any ideas why this is happening? My other tests to render and snapshot the component all work as expected. As...
Read more >expect(...).toBeInTheDocument is not a function
In our last article, we have introduced the usage of Jest to test our JavaScript code Start Testing...
Read more >react-testing-library why is toBeInTheDocument() not a ...
Instead of doing: expect(queryByText('test')).toBeInTheDocument(). you can find and test that it is in the document with just one line by using
Read more >Javascript – react-testing-library why is toBeInTheDocument() not a ...
I keep getting the error TypeError: expect(…).toBeInTheDocument is not a function ... is not part of RTL. You need to install jest-dom to...
Read more >.not.tobeinthedocument() | The AI Search Engine You Control
import '@testing-library/jest-dom/extend-expect' it('does not contain element', async () => { const { getByText, queryByText } = await render(<MyComponent />); ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
install devDepedencies
@types/testing-library__jest-dom
and add configuration atjest.config.js
Install:
Setting up
setupTests.ts
in root your project and add the following code**/apps/your-app-here/jest.config.ts
with following the codeFrom here you can run the Test smoothly… BUT you still have kind issue with typescript, to fix this follow next step
**/apps/your-app-here/tsconfig.json
You should ready to Go.