In my case (TypeScript based project), I had to do the following to fix the error:
Add types for jest-dom package (@testing-library/jest-dom has already been added)
yarn add -D @types/testing-library__jest-dom
Add below under compilerOptions in tsconfig.json
"types": ["node", "jest", "@testing-library/jest-dom"],
Add below on top of your test files (please refer @Greg Wozniak's comment above to include below import statement only in setupTests.js rather than import it in every test file)
import '@testing-library/jest-dom'