0

We have a project built on React 16. It includes react-scripts test but I have failed to understand which testing framework is being used. So can we use react testing library to test react 16.4.

I would like to know how can I test react UI written in React v16.4. Today if I google for react testing mostly the results I am getting is on react-testing-library and react-hooks. Tried setting up enzyme but I am getting error from babel where my component is importing css import './style.css'.

So I would like to know the best way to test react components with redux on React version 16.4. If somebody can point me to tutorials or document about how to setup tests in react v16.4 (if it can be set up with react-testing-library) it would be helpful.

Upgrading react is currently not an option.

Thanks in advance.

Abhilash D K
  • 1,223
  • 1
  • 23
  • 39
  • Please provide a https://stackoverflow.com/help/minimal-reproducible-example and clarify your question – Lin Du Feb 08 '23 at 03:12

1 Answers1

0

Yes, you can use React Testing Library to test React UI written in React v16.4. React Testing Library is a popular testing library that works well with React, regardless of the version.

To set up React Testing Library, you'll need to install it along with Jest, a popular JavaScript testing framework. Here's how you can do it:

Install Jest and React Testing Library:

npm install --save-dev jest @testing-library/react

Create a tests directory in the same directory as your component, and create a test file for your component (e.g. MyComponent.test.js).

In the test file, import the necessary modules:

import React from 'react';
import { render } from '@testing-library/react';
import MyComponent from '../MyComponent';

Write your tests using the render function from React Testing Library to render your component and interact with it:

describe('MyComponent', () => {
  test('renders the component', () => {
    const { getByText } = render(<MyComponent />);
    const element = getByText('Hello, world!');
    expect(element).toBeInTheDocument();
  });
});

In this example, we're rendering MyComponent using the render function and checking that it contains the text "Hello, world!".

Note that in order to test components that import CSS files, you may need to configure Jest to handle those files properly. One option is to use a package like jest-css-modules to transform your CSS files during testing.

There are many resources available for learning how to use React Testing Library, including the official documentation (https://testing-library.com/docs/react-testing-library/intro/), various tutorials and blog posts, and online courses. You can also check the official Redux documentation (https://redux.js.org/recipes/writing-tests) for tips on testing components with Redux.

Jishnu Raj
  • 31
  • 1
  • Hi Thanks for the reply I tried all this. And after setting up when I run npm run test script started getting many babel related issues. Using jest and enzyme now. Working good now. So will continue with that and when we upgrade our React will use RTL. – Abhilash D K Feb 28 '23 at 08:15