Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between using `react-testing-library` and `cypress`?

So, react-testing-library is used for unit/integration testing, and cypress is used for e2e testing. However, both appear to do the same thing:

react-testing-library

  • Facilitates mocking
  • Tests as a user would
  • Starts with the top-level component (not a hard and fast requirement, but if you don't you end up with a bunch of duplicate test cases in your sub-component testing)
  • Instant feedback, fast

cypress

  • Facilitates mocking
  • Tests as a user would
  • Starts with the top-level component (the page)
  • Delayed feedback, slow, but provides extra tooling (video proof, stepping through tests, etc.)

Aside from the feedback cycle, they appear to be almost identical. Can somebody clarify what the differences are? Why would you want to use both?

like image 983
jack.benson Avatar asked Dec 03 '19 17:12

jack.benson


People also ask

What is the difference between Jest and Cypress?

Cypress uses a browser. Jest uses fake DOM and isn't eligible for frontend e2e or intergration tests that need full DOM support, unless used with Puppeteer or else. Once you have a good idea what kind of test you're writing, the choice is quite straightforward.

Why is Cypress test library?

Cypress has many benefits over other end-to-end test runners: The best experience of writing and debugging tests. Ability to inspect the page at any moment during the test run using the browser developer tools. All commands wait for the DOM to change when necessary, which simplifies testing async behavior.

Which testing library is best 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.

What is Cypress used for in React?

The Cypress Component Test Runner is an alternative to a jsdom based testing environment, such as Jest and Vue Test Utils. Cypress component testing offers many benefits: Runs in a real browser. This means your tests are closer to what your users will be experiencing.


1 Answers

You've answered your question in the first line. If you want to test your React app end-to-end, connected to APIs and deployed somewhere, you'd use Cypress.

react-testing-library's aimed at a lower level of your app, making sure your components work as expected. With Cypress, your app might be deployed on an environment behind CDNs, using caching, and its data could come from an API. In Cypress you'd also write an end-to-end journey, a happy path through your app that might give you extra confidence once you've deployed.

like image 148
Dan Avatar answered Sep 23 '22 04:09

Dan