Most of the answers here seem to address primarily Babel. With anything else like eslint, tslint etc. and pure Typescript it’s enough to add @testing-library/jest-dom
to your types.
So a few quick steps:
Make sure you’ve got the library installed:
yarn add -D @testing-library/jest-dom
or
npm i @testing-library/jest-dom --save-dev
and then link it in your tsconfig.json:
"types": ["node", "jest", "@testing-library/jest-dom"],
Now we tackle Jest configuration. Rather than import it in every test file it is better to do it in the Jest config file (usually it’s called jest.config.js):
setupFilesAfterEnv: [
"<rootDir>/support/setupTests.js"
],
and then in the file setupTests.js
:
import '@testing-library/jest-dom/extend-expect'
or use to require()
if using pure JavaScript or different configuration.
The alternative (for TypeScript or if you don’t like adding setupTests.js) is to add globals.d.ts
(in my case to the project root directory) and add there the line above (import ...
).
Note: Both solution work without setting esModuleInterop
.