question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[jest-dom] TypeError: expect(...).toBeInTheDocument is not a function

See original GitHub issue

Current 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

  1. Create jest.setup.ts file in nx workspace root and import ‘@testing-library/jest-dom’.
  2. Add setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'] to global jest.config.js
  3. Use any jest-dom matcher in your test (Example: toBeInTheDocument).
  4. Run that test.

Failure Logs

image

Environment

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
silogoscommented, Jul 11, 2022

install devDepedencies @types/testing-library__jest-dom and add configuration at jest.config.js

{
  setupFilesAfterEnv: [
    "@testing-library/jest-dom/extend-expect"
  ]
}
4reactions
rizaldirnmcommented, Jul 30, 2022

Install:

  1. @testing-library/jest-dom
  2. @types/testing-library__jest-dom

Setting up

  1. Create setupTests.ts in root your project and add the following code
import '@testing-library/jest-dom'
  1. Edit **/apps/your-app-here/jest.config.ts with following the code
export default {
    ....
    setupFilesAfterEnv: ['../../setupTests.ts'],
}

From here you can run the Test smoothly… BUT you still have kind issue with typescript, to fix this follow next step

  1. Edit **/apps/your-app-here/tsconfig.json
{
   ...

   "include": [
      ....
       "../../setupTests.ts",
    ]

   ...
}

You should ready to Go.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found