Andrew Gilliland

Testing Types

Last Updated: November 5, 2024

Cypress is an end-to-end testing framework that allows you to write tests for websites and web applications. It supports various types of testing, including:

  • End-to-End Testing
  • Integration Testing
  • Component Testing
  • API Testing

End-to-End Testing

End-to-end tests simulate real user interactions with your website or application. They test the entire application flow from start to finish, ensuring all components work together as expected. An example would be testing a user login flow, from entering credentials to verifying successful a login.

describe("User Login Flow", () => {
  it("should allow a user to log in", () => {
    cy.visit("/login");
    cy.get('input[name="username"]').type("testuser");
    cy.get('input[name="password"]').type("password123");
    cy.get('button[type="submit"]').click();
    cy.url().should("include", "/dashboard");
    cy.contains("Welcome, testuser").should("be.visible");
  });
});

Integration Testing

Integration tests focus on testing the interactions between different parts of your website or application. They ensure that components or modules work together correctly. An example would be testing a form submission that involves multiple components, such as input fields, validation, and API calls.

describe("Form Submission", () => {
  it("should submit the form and display a success message", () => {
    cy.visit("/form");
    cy.get('input[name="name"]').type("John Doe");
    cy.get('input[name="email"]').type("john.doe@example.com");
    cy.get('button[type="submit"]').click();
    cy.contains("Form submitted successfully").should("be.visible");
  });
});

Component Testing

Component tests isolate individual components and test them in isolation. This type of testing ensures each component behaves as expected on its own. An example would be testing a button component to ensure it renders correctly and triggers the appropriate actions when clicked.

describe("Button Component", () => {
  it("should render the button and handle click events", () => {
    cy.mount(
      <Button label="Click Me" onClick={() => alert("Button clicked!")} />
    );
    cy.get("button").should("contain", "Click Me");
    cy.get("button").click();
    cy.on("window:alert", (str) => {
      expect(str).to.equal("Button clicked!");
    });
  });
});

API Testing

API tests focus on testing the backend APIs that your application interacts with. They ensure that the APIs return the expected responses and handle various scenarios correctly. An example would be testing a REST API endpoint to verify that it returns the correct data for a given request.

describe("API Endpoint", () => {
  it("should return the correct data", () => {
    cy.request("/api/data").then((response) => {
      expect(response.status).to.equal(200);
      expect(response.body).to.have.property("data");
      expect(response.body.data).to.be.an("array");
    });
  });
});

Resources

Testing Types | Cypress Documentation