New Transaction Overview & Setup

In this section, we will be discussing the various tests located within the cypress/tests/ui/new-transaction.spec.ts file.

Let's breakdown what is happening within the beforeEach() as it is important to understand what is going on since this hook is running before each test.

beforeEach()

describe("New Transaction", () => {
  const ctx: NewTransactionTestCtx = {};

  beforeEach(() => {
    cy.task("db:seed");

    cy.intercept("GET", "/users*").as("allUsers");

    cy.intercept("GET", "/users/search*").as("usersSearch");

    cy.intercept("POST", "/transactions").as("createTransaction");

    cy.intercept("GET", "/notifications").as("notifications");
    cy.intercept("GET", "/transactions/public").as("publicTransactions");
    cy.intercept("GET", "/transactions").as("personalTransactions");
    cy.intercept("PATCH", "/transactions/*").as("updateTransaction");

    cy.database("filter", "users").then((users: User[]) => {
      ctx.allUsers = users;
      ctx.user = users[0];
      ctx.contact = users[1];

      return cy.loginByXstate(ctx.user.username);
    });
  });

You can find out more information about the custom Cypress commands used here.

The first thing we are doing is seeding our database using a custom Cypress task.

cy.task("db:seed")

Next, we are using cy.intercept() to intercept and alias several requests.

cy.intercept("GET", "/users*").as("allUsers")
cy.intercept("GET", "/users/search*").as("usersSearch")
cy.intercept("POST", "/transactions").as("createTransaction")
cy.intercept("GET", "/notifications").as("notifications")
cy.intercept("GET", "/transactions/public").as("publicTransactions")
cy.intercept("GET", "/transactions").as("personalTransactions")
cy.intercept("PATCH", "/transactions/*").as("updateTransaction")

Next, we are using a custom Cypress command cy.database() to get some users from the database. We then store some of this information in the ctx object. Lastly, we log in as one of the users returned from the cy.database() command.

const ctx: NewTransactionTestCtx = {}
// ...

cy.database("filter", "users").then((users: User[]) => {
  ctx.allUsers = users
  ctx.user = users[0]
  ctx.contact = users[1]

  return cy.loginByXstate(ctx.user.username)
})