I’ve successfully created a Cypress test that creates a new token and user object. The problem is that, since I’ve hard coded the arguments accepted by the addUser mutation, the test only runs once. On a second run, the DB returns an error stating that the user already exists. I have a deleteUser mutation as well, but it takes in the userId to do so. The userId is dynamically created by MongoDB, so it will be different each time I run the test. I’m not quite sure how to both test the deleteUser mutation and ensure that the addUser test runs successfully each time. Any suggestions are greatly appreciated!
import {
addUser,
login,
allUsers,
findUserById,
deleteUser,
} from "../GraphQLBody/Users";
describe("User Queries and Mutations", () => {
const URL = "https://localhost:3001/graphql";
it("Creates a new user", () => {
cy.request({
url: URL,
method: "POST",
body: {
query: addUser,
},
}).then((res) => {
console.log(res.body);
expect(res.status).to.eq(200);
expect(res.body).to.have.property("data");
expect(res.body.data).to.have.property("addUser");
const { token, user } = res.body.data.addUser;
expect(token).to.be.a("string");
expect(user).to.be.an("object");
expect(user).to.have.property("name", "Test User");
expect(user).to.have.property("email", "[email protected]");
});
});
});