testing-library / jest-dom

:owl: Custom jest matchers to test the state of the DOM

Home Page:https://testing-library.com/docs/ecosystem-jest-dom

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Upgrade to 6.0.0 no longer augments `expect`

Naddiseo opened this issue · comments

commented
  • @testing-library/jest-dom version: 6.0.0
  • node version: 18.17.1
  • npm (or yarn) version: yarn 3.5.1
  • dom-testing-library version: (if applicable): 9.3.1
  • react-testing-library version: (if applicable): 14.0.0
  • jest: 29.6.2
  • typescript: 5.1.6

Relevant code or config:

  • Project uses "type": "module", typescript, nextjs
  • We (used to) import "@testing-library/jest-dom/expect-extend" in our setupTests.js, since upgrading to 6.0.0, I see that we're now supposed to import just "@testing-library/jest-dom"
  • However, now matchers don't seem to be there

What you did:

Upgrading @testing-library/jest-dom to version 6.0.0

What happened:

  • We (used to) import "@testing-library/jest-dom/expect-extend" in our setupTests.js, since upgrading to 6.0.0, I see that we're now supposed to import just "@testing-library/jest-dom"
  • However, now matchers don't seem to be there
Property 'toBeInTheDocument' does not exist on type 'Matchers<void, HTMLElement | null>'.

Reproduction:

https://codesandbox.io/s/react-testing-library-demo-forked-l27vm5

Problem description:

The extended matchers don't seem to be there any more.

Suggested solution:

I haven't verified, but I suspect it might be because I'm using "type":"module". The code sandbox link above manages to reproduce the issue I'm seeing

Can you try converting your setupTests.js file to a TypeScript file (setupTests.ts) and then including that in your tsconfig.json?

Also, CodeSandbox won't work with this setup because it doesn't support includeFiles in tsconfig:

image
commented

@jgoz changing the setupTests to a .ts file seems to have worked, thanks! I don't understand why though. Perhaps the need for a .ts setup file can be documented?

Great! Glad it worked. It is documented in the With TypeScript section:

If you're using TypeScript, make sure your setup file is a .ts and not a .js to include the necessary types.

Having the same problem here, and I have always had a setup-tests.ts and the correct line in it. Seems to just affect the VSCode /ts compiler underlining in red:

image

import '@testing-library/jest-dom';

@httpete And setup-tests.ts is listed in includes or files in your tsconfig.json?

I was able to fix this by including this in my test-utils.ts. I guess it was working by accident before, picking up the types in the context. I am set thanks!

import '@testing-library/jest-dom';