next.jsjestjsreact-testing-librarytesting-library

jest-dom installed but @testing-library looking for dom


I installed jest, jest-environment-jsdom, @testing-library/react and @testing-library/jest-dom, as suggested on the Next.js site.

However, I am unable to run any test that involves working with the DOM, for example:

import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';

Here's the error I got:

Cannot find module '@testing-library/dom' from 'node_modules/@testing-library/react/`
`dist/pure.js'

I'm pretty sure that I installed jest-dom rather than dom (under the @testing-library scope). When I dig into pure.js of @testing-library/react, on line 46 it's indeed looking for dom:

var _dom = require("@testing-library/dom");

Why is this the case? Did I install anything wrong? Is the documentation incorrect/incomplete?


Solution

  • Thanks to @jonrsharpe, I'm posting an answer for reference.

    According to https://github.com/testing-library/react-testing-library/releases/tag/v16.0.0, testing-library changed dom into a peer dependency about 3 weeks ago at the time of writing, which means it should be installed. The next.js docs should be updated, and I'll try to create a pull request.