The why and how to reset the database after each test run (Cypress, Mongo 5.0.11, React example)
Tags: cypress, mongodb, react, testing
Published: 2023-02-27 07:03:16

---
title: The why and how to reset the database after each test run (Cypress, Mongo 5.0.11, React example)
published: true
description: 
tags: #cypress #mongo #react #testing
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-02-26 07:44 +0000
---

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04oq3c8op2yian7wd3hn.jpg)

As a developer, I frequently find myself working on projects that lack a dedicated testing team or resources. To minimize the amount of manual regression testing I have to do before deployment to staging, I've found that writing end-to-end (e2e) tests for the most critical scenarios is the most effective solution. My preferred tool for e2e testing is Cypress.

However, a question that often arises is: how can we ensure that each of these tests can be 

- run multiple times;
- run independently. 

That's why we need a way to reset the database after each test.

My approach is based on [Gleb Bahmutov's post](https://glebbahmutov.com/blog/testing-mongo-with-cypress/). The difference is that this post is for mongo 5.0.11, and his post is for older mongo versions. 

Another difference from Gleb's post is that all the tests I'll talk about will be about app entry flows: **Sign up**, **Log in** and **Logged in** tests.

Let's consider a following example (repository is [here](https://github.com/indra-uolles/restaurants-nodejs-mongo)) 

Btw Cypress [recommends](https://docs.cypress.io/guides/references/best-practices#Using-after-or-afterEach-hooks) to clean up database **before** each test. I wrote "after" in the post header only for SEO purposes. 

### Sign up Test

You have written a test for sign up scenario. First time test passed. You try to run the test again and get an error "Email address already exists":

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uyk5hxjn0efdq08ch8lq.jpg)

You might want to skip what I've written below and just take a look to branch **feature-cypress-reset** in the repository instead. There you will see a signup test that can be run an indefinite amount of times.

File cypress/support/db.js helps to connect to the database.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/823aih8dpd9jy4m8ay51.jpg)

If you are a frontend, you don't have to worry much about refactoring this file. Devops that will convert your PR into working version will change **uri** and **client.db("rests")** to something that works for your company. They will set up a separate testing environment that includes a test database.  This test environment will allow run tests without the fear of damaging or corrupting important data in the production system.

The most interesting file to look at is **cypress.config.ts**. I spent some time trying to understand why await **users.remove({})** doesn't work before I realised that it was deprecated in newer mongo and replaced with **deleteMany**!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64czo1vk0m904op0d0lu.jpg)

Then you just add a cypress command for cleanup:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocsoaduswrdnnrbjmc6w.jpg)

And use it in the test like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dr1li9nxh4l01t65zi0n.jpg)

### Login Test

When I've started to write tests for logged in users, I've discovered that Cypress doesn't like tests that take too much time to run. If test takes too much time, Cypress can throw one of the following errors (there are GitHub issues about that - [first](https://github.com/cypress-io/cypress/issues/6458), [second](https://github.com/cypress-io/cypress/issues/5480)):

- abort test running with some message about web sockets;
- abort typing into form fields, e.g. instead of typing "somebody@gmail.com" type "somebody@gma".

The best thing is you can do for such tests is to use shortcuts. E.g. instead if imitating user login by typing values into forms, send a POST request using Cypress command. Or change a database record directly using Cypress task.
Also, this setting might help:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqlxb1tabgihwljck1xa.jpg)

Cypress suggests to do [session switching inside tests](https://docs.cypress.io/api/commands/session#Switching-sessions-inside-tests) but we don't use it. Sadly I don't remember any details about why it wasn't useful for our current project.

Let's say you want to test login flow. You'll need to have a user in your DB before the test!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rld40fnh9pmxxcmy9xi.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2pzjk22g8esn2sxlv2y.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0lbzj3eqlx6w0t57s14k.jpg)

You might wonder how I got an encrypted version for password that I decided to use. I temporarily put in app.js the following code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bacdhkb9e51piyz47zpk.jpg)

### Logged in Test

Let's say we want to test something for a logged in user. We will use a shortcut for logging in, as it is advised in Cypress docs:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9543eugbyhqrrh6ophc.jpg)

Then we'll use it like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xvzrpegzfp58zyui8td.jpg)

### Conclusion

I would like to tell more about Cypress but this post is too long already :( The only thing that I would like to mention (again) is that if you are a dev who worries about QA but doesn't have much time for it Cypress is definitely the best choice based on the ratio how much time you spend writing/maintaining tests vs how much functionality you get tested.