'Snapshot test gives error with react-17 but works fine react-18 while using React Testing Library

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 .........



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source