快照测试给出了React-17的错误,但使用React Testing库时的React-18可以很好地工作

发布于 2025-01-22 04:22:54 字数 1229 浏览 3 评论 0原文

我正在研究一个基于React-17的主要项目。使用React测试库快照测试时,我面临错误。该项目的JSON是---

"dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "react-test-renderer": "^17.0.2",
    "web-vitals": "^1.0.1"
  },

我创建了一个基于快照测试的React-18的小型侧面项目。在这里快照测试工作正常。 JSON为此---

"dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.1.1",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "react-scripts": "5.0.1",
    "react-test-renderer": "^18.0.0",
    "web-vitals": "^2.1.4"
  },

快照的代码来自侧面项目(在此工作正常),我也在主要项目中使用相同的代码来进行快照 -

import { render, screen, fireEvent, getByTestId } from '@testing-library/react';
import App from './App';
import React from "react";
import renderer from 'react-test-renderer';

test('snapshot test', () => {
  const component = renderer.create(<App/>).toJSON();
  expect(component).toMatchSnapshot();
});

我需要的是新的快照代码与主要项目React-17兼容。 (我不允许更新依赖关系) 错误是: 从.........

I am working on a major project which is based on react-17. I am facing an error while snapshot testing using React Testing Library. JSON for the project is ---

"dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "react-test-renderer": "^17.0.2",
    "web-vitals": "^1.0.1"
  },

I created a small side project which is based on react-18 for snapshot testing. Here snapshot testing is working fine. JSON for this ---

"dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.1.1",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "react-scripts": "5.0.1",
    "react-test-renderer": "^18.0.0",
    "web-vitals": "^2.1.4"
  },

This code for snapshot is from the side project (where it is working fine), i am using same code for snapshot in my major project as well ---

import { render, screen, fireEvent, getByTestId } from '@testing-library/react';
import App from './App';
import React from "react";
import renderer from 'react-test-renderer';

test('snapshot test', () => {
  const component = renderer.create(<App/>).toJSON();
  expect(component).toMatchSnapshot();
});

what i need is a new code of snapshot which will be compatible with major project react-17.
(I am not allowed to update dependencies)
The error is:
Cannot find module 'react-test-renderer' from .........

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文