question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not sure how to clean up the database in a beforeEach()

See original GitHub issue

I’m submitting a…

  • Bug report
  • Content update
  • Process update (build, deployment, … )

Type of bug / changes

In Using after or afterEach hooks, it is recommended to clean up server/db state in beforeEach or before. I understand the rationale but I believe the text lacks some real use case. Here is a use case that I don’t know how to solve following the best practice.

Imagine I’m testing my own clone of github. To have a clean environment for my tests, I want Cypress to use a clean temporary user and a clean temporary repository. To avoid conflicts between multiple Cypress instances targeting the same server (e.g., multiple front-end developers testing their changes in parallel), there should be one user and one repository dedicated to each Cypress instance. This can be implemented by generating users and repositories with well-known random ids (e.g., temp-user-13432481 and temp-repo-134234). Cleaning up the mess in the database is just a removal of temp-* databases away.

The problem is when to clean up. If the clean up is done in a beforeEach() as is recommended, running a test in a Cypress instance will delete the data of other Cypress instances running in parallel.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jennifer-shehanecommented, Dec 16, 2019

Yes, the section does naively not really mention how this should be handled when running Cypress tests in parallel. Each specfile will run independently, so you’d likely need to create a new temp-user per specfile - make sure you do not overwrite any of the same data being generated in any other specfile that could be running.

0reactions
hershmirecommented, Aug 3, 2021

Our team is also running into a similar scenario and would love to better understand recommendations for creating test data that can be properly cleaned up in the before / beforeEach hooks while still supporting parallel test runs. This includes unique test users as well as other data models for each user.

@amirrustam is said data initialization guide located anywhere?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cleanup database state in a beforeEach? - cypress
The problem is when to clean up. If the clean up is done in a beforeEach() as is recommended, running a test in...
Read more >
How can I clean the DB before each test? - JBoss.org
DB cleanup is performed after each @Test. Using empty dataset will simply add "empty set" of data - meaning your db will remain...
Read more >
Setup and Teardown - Jest
beforeEach and afterEach can handle asynchronous code in the same ways that ... For example, let's say we had not just a city...
Read more >
API Reference | Vitest
If you want to skip running certain tests, but you don't want to delete the code due to any reason, you can use...
Read more >
Globals · Jest
This is often useful if you want to clean up some global setup state that is ... Here the beforeAll ensures that the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found