Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Library React vs Jest

I have a really big application with react(lot of pages, modals, tables,etc) and I'm using redux-saga for managing the state. I have a lote of stores and almost in all the components I use the useSelector method for getting the data from the store and many hooks inside them because of logic.

I want to start testing the application, specially to avoid that the app crashes when there is data undefined, invalid, etc. What library do you recommend me to apply in this case ?

like image 909
Avedis Maroukian Avatar asked Feb 23 '21 21:02

Avedis Maroukian


People also ask

Which testing library is best for React?

By default, Jest and React come with a react-test-renderer package which can transform components into JSON objects. This mechanism is used for snapshot testing. Jest is a perfect choice for standard unit tests of pure functions.

Is Jest a testing library?

Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue and more!

Is React testing library unit testing?

The React Testing Library is a DOM testing library, which means that instead of dealing with instances of rendered React components, it handles DOM elements and how they behave in front of real users. It's a great library, it's (relatively) easy to start using, and it encourages good testing practices.

Is Jest good for React?

Jest is a JavaScript test runner that lets you access the DOM via jsdom . While jsdom is only an approximation of how the browser works, it is often good enough for testing React components.


Video Answer


1 Answers

React Testing Library and Jest both have different responsibilities "react testing library" is not a test runner meaning when u enter command npm test or npm run test it is jest responsibility that collect all the files ending with .test.js and runs each test case and show pass and fail results in your console like below

enter image description here

react testing library provides you function to catch dom element and perform some action below are some of its function

render, fireEvent, waitFor, screen

you can also use Enzyme(another popular testing library) for such function to access dom element

EXTRA: developer often confuse among

  • jest
  • react-testing-library
  • Enzyme

Here Enzyme and react-testing-library are two similar things and alternatives to each other means you can use

  • enzyme with jest or
  • react-testing-library with jest
  • you can also use all three i.e react-testing-library+Enzyme with jest
  • but you can not use Enzyme and react-testing-library without jest or any other test runner eg: Mocha

where jest(testing-framework) will collect all .test.js files execute all the test cases and put the output in console with detail like how many pass and fail and react-testing-library or enzyme(both are testing library) will help you to perform event and accessing dom element

like image 85
Yusuf Khan Avatar answered Oct 08 '22 17:10

Yusuf Khan