React Testing库给出了ReactDom的控制台错误。

发布于 2025-01-18 08:52:03 字数 513 浏览 3 评论 0 原文

在更新了React 18或从创建新的React 18应用程序后,当我运行 YARN TEST 命令时,它给出了 Console.error 作为渲染在任何测试中使用的方法的警告:

Console.Error
警告:ReactDom.Render不再支持React 18中的支持。改用Croteroot。在您切换到新的API之前,您的应用程序将表现得好像正在运行17。 ://reactjs.org/link/switch-to-createroot

如屏幕截图:

作为React测试库似乎不支持React 18方法论。

After updating to React 18 or creating a new React 18 app from create-react-app, when I run the yarn test command, it gives a console.error as a Warning for each of the render methods used in any of the tests as:

console.error
Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot

As seen in the screenshot:
react testing library warning

As React Testing Library doesn't seem to support React 18 methodology as of now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅此而已 2025-01-25 08:52:03

解决react测试库错误:

“ReactDOM.render在React 18中不再支持,请更新react测试库的版本。”

在项目的根目录中打开终端并运行以下命令:

npm install --save-dev @testing-library/react@latest
npm install --save-dev @testing-library/jest-dom@latest
npm install --save-dev @testing-library/user-event@latest

确保更新您正在使用的所有反应测试库包的版本。

您的index.js 文件应使用新的createRoot API 来呈现您的应用程序。

index.js

import React from 'react';
import ReactDOM from "react-dom/client";

import App from './App';

const root = ReactDOM.createRoot(document.getElementById("root"));

root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

现在您应该能够开始测试而不会出现错误。

应用程序.test.js

import {render, screen} from '@testing-library/react';
import App from './App';

test('renders react component', () => {
  render(<App />);
  const divElement = screen.getByText(/hello world/i);
  expect(divElement).toBeInTheDocument();
});

To solve the react testing library error:

"ReactDOM.render is no longer supported in React 18, update the version of the react testing library."

Open your terminal in the root directory of your project and run the following commands:

npm install --save-dev @testing-library/react@latest
npm install --save-dev @testing-library/jest-dom@latest
npm install --save-dev @testing-library/user-event@latest

Make sure to update the versions of all react testing library packages you are using.

Your index.js file should use the new createRoot API to render your application.

index.js

import React from 'react';
import ReactDOM from "react-dom/client";

import App from './App';

const root = ReactDOM.createRoot(document.getElementById("root"));

root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

Now you should be able to start your tests without getting the error.

App.test.js

import {render, screen} from '@testing-library/react';
import App from './App';

test('renders react component', () => {
  render(<App />);
  const divElement = screen.getByText(/hello world/i);
  expect(divElement).toBeInTheDocument();
});
土豪 2025-01-25 08:52:03

卸载 @testing-library/react-hook 并从 @testing-library/react 导入 renderHook

import { renderHook } from "@testing-library/react";

请参阅此处 https://github.com/testing-library/react-hooks-testing-library/issues/826#issuecomment-1100650242

uninstall @testing-library/react-hook and import renderHook from @testing-library/react

import { renderHook } from "@testing-library/react";

see here https://github.com/testing-library/react-hooks-testing-library/issues/826#issuecomment-1100650242

月亮邮递员 2025-01-25 08:52:03

有关所有库的快速更新,您可以使用下一个命令:

  1. 要更新到新的主要版本:
    npm install -g npm-check-updates
  2. package.json中的升级版本:
    NCU -U
  3. 或NPM安装,以防您的项目没有node_modules:
    NPM Update

For quick update of all the libraries you can use next commands:

  1. for update to new major version:
    npm install -g npm-check-updates
  2. upgrade versions in package.json:
    ncu -u
  3. or npm install, in case your project hasn't node_modules:
    npm update
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文