Cleaning Up After the Tests
Understand the importance of clearing the database before each test to prevent conflicts caused by persistent data. Learn to implement a custom Cypress task that resets the database using Prisma migrations, ensuring your tests are independent, repeatable, and maintain unique constraints.
We'll cover the following...
We'll cover the following...
In the following two tests, we insert a meetup into the database using our custom task and then visit the URL matching the slug to assert that it exists. Try to guess the result of this test and click the “Run” button to see what happens.
describe("Meetup detail page", () => {
it("Should exist when it is in database", () => {
cy.task("seed:events", [
{
id: 1,
name: "Test Meetup",
slug: "test-meetup",
description: "",
location: "Online",
dateTime: new Date(2030, 0, 1),
},
]);
cy.visit("/test-meetup");
});
it("Should exist also when it is in database", () => {
cy.task("seed:events", [
{
id: 1,
name: "Test Meetup 2",
slug: "test-meetup-2",
description: "",
location: "Online",
dateTime: new Date(2030, 0, 1),
},
]);
cy.visit("/test-meetup-2");
});
});
export {};
Cypress tests
Note: Our first test passes, but the second one fails with the following error:
Unique constraint failed on the fields: (`id`).
Lines 3–12: ...