To Puppeteer, or to Cypress
Tags: showdev, testing, javascript
Published: 2020-07-22 20:21:46

---
title: To Puppeteer, or to Cypress
published: true
description: Puppeteer vs Cypress | the good the bad and the ugly 
tags: #showdev #testing #javascript
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/ivh35h1jahyp55cudq7c.png
---

Working on a layout is not easy when there are too many possibilities and it gets more complicated if it's related to mouse direction. It's not just up and down, you literally need to test all horizontal and vertical possibilities. Imagine you also study elements getting effected by mouse movement, where things getting harder and harder. 

In the nutshell, I am a hard unit-test believer, but in this case, you definitely need to do testing and not a unit test we are talking here about an end-to-end test where you see elements moving virtually and experience each case individually.


{% link https://dev.to/jalal246/using-jest-puppeteer-with-monorepo-3kj2 %}


The big question is how you do it, taking into consideration that your project already uses a unit test with Jest.

---

### Puppeteer it

The wise decision in my case is to use, Puppeteer. You integrate Puppeteer with Jest in `jest.integration.config.js` a whole new config and start writing your first integration test.

```js
{
  "preset": "jest-puppeteer"
}
```

Everything is `async` in Puppeteer so you go by something like this to get your element:

```js
const element = await page.waitForSelector("#id-something");
```

I need to get element box size so I added:

```js
const elmBox = await element.boundingBox();

const {x, y, width, height} = elmBox

// do something here...
```

You immediately start to notice that's Puppeteer is natural. I am not learning something new, it's easy to use and you supposedly can go further. Let's try mouse events. 

```js
// move the mouse to new coordinates 
await page.mouse.move(100, 200);

// triggers mouse down
await page.mouse.down();

// move the mouse to new horizontal position
await page.mouse.move(500, 200);

// triggers mouse up
await page.mouse.up();
```
Very easy. Very handy. Except there's a configuration related. you can't [keep the browser open](https://github.com/smooth-code/jest-puppeteer/pull/276), there's no time travel, and you can't repeat the test cases individually. So, if you test and develop by doing [TDD](https://en.wikipedia.org/wiki/Test-driven_development) it's nearly impossible to actually see what's going on. You can slow down the test but if you running a server and you wait to browser to re-launch that's means waiting for infinity. 

```js
launch: {
 devtools: true,
 slowMo: 500,
},
```
Even with Puppeteer reaching version 5 there's a gap in its ecosystem. Because it's not a testing framework.

> Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. 

---

### Trying Cypress

The first issue with Cypress, that you need to start from scratch. A new assertion, a new testing framework, with a new structure. 


![install cypress](https://dev-to-uploads.s3.amazonaws.com/i/loh41p05j32tcb7ub2m2.gif)

The installation includes examples that really help to know where you are going and what your test cases should look like. You use a chain of commands. You get something, trigger it, test it.

```js
cy.get("#id-something").then((elm) => {
elmBox = elm[0].getBoundingClientRect();

const {x, y, width, height} = elmBox
});
```

Cypress uses `trigger` to trigger an event

```js
cy.get("#id-something")
  .trigger("mousedown", { button: 0 })
  .trigger("mousemove", { clientX: 100, clientY: 200 });
```

It uses a whole different approach but once you read it you easily get it. Until you start to use assertion.

This is from Cypress documentation:

```js
cy.get('.error').should('be.empty')
cy.contains('Login').should('be.visible')
cy.wrap({ foo: 'bar' }).its('foo').should('eq', 'bar') 
``` 

You take a deep breath and ask yourself, should I really have to learn this? Even something like `beforeAll` in cypress is `before`. So, you get the sense that whatever transition you make it's not going to be smooth as you expect. 

But your test runs with zero-config, with time travel, and yes you can repeat it forever. And it's super-fast.

![Cypress is fast](https://dev-to-uploads.s3.amazonaws.com/i/jli1uoi14dmh9ze2jf17.gif)

However, my problem is not with speediness. I need, at a certain point to be able to repeat the same test command.

![repeate the test](https://dev-to-uploads.s3.amazonaws.com/i/mk9xkk5v2oubs62b99eg.gif)

It turns out, Cypress automatically takes a screenshot for every command and you simply can highlight it forever which means a totally new debugging experience.

----

### Conclusion

When it comes to increasing productivity, end-to-end test can be the real solution. Forget about all articles tell you about the benefits of testing and why you should use e2e. I personally experienced a huge difference in fixing bugs saving hours of chasing needles in a haystack. 

Puppeteer can be a solution for expanding your test cases. But if you are using it with development, the answer is Cypress.

Cypress is rich with examples, and a supportive community. It's designed for the end-to-end tests; therefore, you won't struggle using it. If you are familiar with [Mocha](https://mochajs.org/) it won't take you a long time to get familiar with assertation. And it's amazing how you can use it even with no previous background about this type of testing. Most importantly, you immediately start experiencing the benefits of each test you write instead of figuring out how to configure it with your project.