testing-library / react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
https://testing-library.com/react
MIT License
19.04k stars 1.11k forks source link

Do not want to install react & react-dom dependency in node_modules #1335

Open 21duxue opened 5 months ago

21duxue commented 5 months ago

My project is react introduced through external links。

Can I use it without installing react or react-dom?

If possible, how do I need to configure it? Thank you

jest + @testing-library/react

error

 FAIL  src/Header/index.test.js
  ● Test suite failed to run

    Cannot find module 'react' from '../../../node_modules/.pnpm/@testing-library+react@14.3.1/node_modules/@testing-library/react/dist/act-compat.js'

    Require stack:
      /node_modules/.pnpm/@testing-library+react@14.3.1/node_modules/@testing-library/react/dist/act-compat.js
      /node_modules/.pnpm/@testing-library+react@14.3.1/node_modules/@testing-library/react/dist/index.js
      Header/index.test.js

      20 |   })
      21 |
    > 22 | import { screen, render } from "@testing-library/react"
         | ^

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <div id="root"></div>
</body>
    <script ignore src="/assets/react/16.14.0/production/react.js"></script>
    <script ignore src="/assets/react/16.14.0/production/react-dom.js"></script>
</html>

webpack

externals: {
      react: "React",
      "react-dom": "ReactDOM",
}
MatanBobi commented 5 months ago

You can still install react and react-dom as dev dependencies, this is usually what people are doing in these kind of approaches.