I had the same problem. My solution is to add the api "reset" endpoint to restore the database to its state before running the E2E test.
We use ASP.NET WebApi to create a RESTful service. Here is my code structure.
[HttpGet] [Route("reset")] public IHttpAction Reset(){
Thus, the api call to this test/reset method helps restore the database to its original state. We can put a call inside afterAll() (if you use JasmineJS). Or you can simply do a manual reset by making this GET call from Postman or Fiddler.
Sometimes I have several reset api methods to call. So I can check and reset by function groups. For example, run the e2d tests on user management functions and reset this piece of data, and then move on to the next function.
It should be noted that if the primary key in the table is ordered, deleting the data added to the test will violate this sequence. Make sure our tests do not "expect" this primary key.
The goal is to re-run the e2e tests and get the same result.
And, of course, if allowed, another good way out is to back up the database before our e2e testing and restore it after the test completes. Manual, stupid, but sometimes convenient and sufficient for our e2e test to be repeated again.
source share