Speeding up Cypress Automation Tests
Tags: cypress
Published: 2021-09-18 15:56:36

---
title: Speeding up Cypress Automation Tests
published: true
description: 
tags: cypress
//cover_image: https://direct_url_to_image.jpg
---

Here are a few tips on making your [Cypress](https://docs.cypress.io/guides/overview/why-cypress) automation tests runs faster. 

NOTE: Some of these may seem trivial but they are all actual mistakes made by me. I hope you'll learn from my mistakes.

### Write one big test rather than several small ones

A real-world integration test typically involves signon, etc before testing the actual functionality. Each test you add will therefore add to the time taken. Also, as Cypress's best practices document [explains](https://docs.cypress.io/guides/references/best-practices#Creating-tiny-tests-with-a-single-assertion), Cypress does some housekeeping between each test. This will also slow you down if there are too many small tests.

### Use `before` & `beforeEach` judiciously.

Consider this example:
```javascript
	beforeEach(()=>{
		cy.login();
		cy.get('[.homePage]').should('be.visible');
	});
	//10 tests
```


Here, the test will login 10 times - once before each test. What I actually wanted was to login once and so should replace `beforeEach` with `before`

```javascript
	before(()=>{
		cy.login();
		cy.get('[.homePage]').should('be.visible');
	});
	//10 tests
```

### Avoid waiting for arbitrary periods of time. 

Cypress explains that `cy.wait(Number)` is an anti-pattern and you can almost always replace it with an assertion. Read Cypress's [detailed explanation and examples](https://docs.cypress.io/guides/references/best-practices#Unnecessary-Waiting) to understand more. If you succumb to the temptation to add cy.wait() anyway, they will eventually become a time-sink.

### Tweak Cypress's configuration to remove unnecessary disk I/O

Cypress aims to "just work" and does this admirably. A configuration file is automatically created by Cypress on the first run. Some of the options here increase the disk I/O and hence slow down Cypress itself. The main culprits are:

- [video](https://docs.cypress.io/guides/references/configuration#Videos)
- [videoUploadOnPasses](https://docs.cypress.io/guides/references/configuration#Videos) _NOTE:_ This one only applies if you are also using Cypress's dashboard. By default Cypress uploads all videos when connected to Dashboard which you probably don't need.
- [screenshotOnRunFailure](https://docs.cypress.io/guides/references/configuration#Screenshots)

Turn these off or turn on only for nightly runs etc. where you are not worried about the time taken.
	
### Tag tests and run only the ones you need to
Cypress's list of [curated plugins](https://docs.cypress.io/plugins/directory) includes [cyress-grep](https://github.com/cypress-io/cypress-grep). Using this plugin, you can run tests by grepping their titles or custom tags you've added to each test. Use this ability to run only the necessary [smoke](https://en.wikipedia.org/wiki/Smoke_testing_(software)) or feature-specific tests.
	
### Incorporate Cypress into your CI/CD
This one is a little harder to implement but worth the effort. Take the time to read and understand how Cypress can be plugged into your [CI/CD pipeline](https://docs.cypress.io/guides/continuous-integration/introduction). As with everything else, Cypress's documentation here is extensive. As your test suite grows, offloading running the suite to some other system may be more performant than running all the tests locally on your laptop.