Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use both Cypress and Jest together?

I'm learning both Jest and Cypress at same time. I know that they are not direct competitors because Cypress is focused on E2E and Jest on unit testing. For now I have implemented in my project both Jest and Cypress with few tests.

But actually most things I can test in both Cypress and Jest, and often I have hard time to decide with what write my test. Also it is harder to maintain compared to single test library.

I'm wondering - how often is Cypress (or alternative) and Jest (or alternative) used together? Is it really standard and good practice to use both? Or most developers/teams sticks with single one solution and it is fine?

like image 879
norr Avatar asked Feb 16 '21 01:02

norr


People also ask

Why using Cypress is better than unit testing?

One major difference is that Cypress enables you to write your unit tests and integration tests in the same tool, as opposed to splitting up this work across both Karma and Protractor. Also, Protractor is very much focused on AngularJS , whereas Cypress is designed to work with any JavaScript framework.

Can we use testing with Cypress?

Cypress is an open-source testing framework based on JavaScript that supports web application testing. Unlike Selenium, Cypress automation testing works completely on a real browser without the need for driver binaries.

Can Cypress be used for end to end testing?

Cypress is a great tool for those who want to create useful end to end tests with very little effort. It also makes it very easy to debug issues with its live preview, snapshots, videos and screenshots. It does fall down in certain areas, and may not be the best solution for certain projects.

Is Cypress end to end or integration?

Cypress runs end-to-end tests the same way users interact with your app by using a real browser, visiting URLs, viewing content, clicking on links and buttons, etc. Testing this way helps ensure your tests and the user's experience are the same.


1 Answers

short answers: It's very common to use Jest and Cypress in the same codebase.

Unit, Integration, or E2E tests

With component Libraries like Vue and React, the line between integration and unit tests can get a bit fuzzy. We can even use the same tools(Jest & Cypress)for both cases, which makes things even more confusing. I recommend that you aim to test "user stories" or, in other words, make sure that users can always perform key actions. For example:

  1. Can the user fill and submit a form?
  2. Can the user add products to the cart?
  3. Does the hamburger menu responds to clicks?

Some of these tests will involve one component, others will involve two and some will require the entire application. I prefer writing smaller tests(unit and integration) using Jest and testing library because of the quick feedback loop. I get to develop and run my tests at, almost, the same time.

Eventually, you'll run into cases that involve so many moving parts(components) that using Jest is not an option. This is where Cypress shines, it's great for testing your end-to-end workflows.

like image 78
randyjp Avatar answered Sep 18 '22 17:09

randyjp