← Back: Documentation Overview
This document outlines the steps for setting up Cypress in your project. Cypress is an end-to-end testing framework designed to make it easy to write and run tests for web applications. This guide will cover how to set up Cypress both in a Docker container and locally on your machine.
Cypress is a powerful, open-source testing framework for web applications that enables developers to write tests with a simple, easy-to-understand syntax. It automatically handles the complexity of dealing with asynchronous code, making it easier to write and maintain reliable tests.
With Cypress, you can test your application by simulating real user interactions and verifying that your application behaves as expected under various conditions. You can learn more about Cypress on the official website .
To run your Cypress tests in a Docker container, follow these steps:
- Run the Docker Compose command to start the test environment:
docker-compose up --profile tests- Create a
.docker.envfile in the root of your project, if it does not exist, and configure the necessary environment variables. The file must include at least the following variables:
# .frontend/.env.docker
VITE_BACKEND_DOMAIN=http://localhost:8000
VITE_CYPRESS_BACKEND_DOMAIN=http://backend:8000To set up Cypress and run tests locally, follow these steps:
- Install the necessary dependencies by running the following command in ./frontend:
yarn install- Navigate to the
frontendfolder and run the Cypress tests using the following command:
yarn run cypress run- Create a
.env.localfile in thefrontendfolder, if it does not exist, and configure the necessary environment variables. The file must include at least the following variables
# frontend/.env.local
VITE_BACKEND_DOMAIN=http://localhost:8000
VITE_CYPRESS_BACKEND_DOMAIN=http://localhost:8000With these steps, you should now be able to successfully set up and run Cypress tests both in Docker and locally on your machine.
The Docker Compose configuration sets up a Cypress service for running end-to-end tests in a multi-container environment. Below is a short explanation of each configuration option in the file.
cypress:
depends_on:
- frontend
- backend
profiles:
- tests
build:
context: ./frontend
dockerfile: Dockerfile.cypress
platform: linux/arm64/v8
env_file:
- ./frontend/.env.docker
environment:
- IS_DOCKER=YES
- CYPRESS_baseUrl=http://frontend:3000
volumes:
- ./frontend/cypress:/frontend/cypress
command: "yarn run cypress:run"depends_on: Specifies that thecypressservice depends on thefrontendandbackendservices. This ensures that thefrontendandbackendcontainers are up and running before thecypressservice starts.profiles: Assigns thetestsprofile to thecypressservice. This allows you to selectively start thecypressservice using the--profileoption withdocker compose.build: Defines the build context and Dockerfile for thecypressservice. The build context is set to the./frontenddirectory, and the Dockerfile is namedDockerfile.cypress.platform: Specifies the target platform for thecypressservice, which is set tolinux/arm64/v8in this case.env_file: Loads environment variables from the specified file./frontend/.env.docker. This file contains the environment settings needed for thefrontendservice.environment: Defines additional environment variables for thecypressservice.IS_DOCKER=YESis used to indicate that the service is running inside a Docker container, andCYPRESS_baseUrlis set to the URL of thefrontendservice.volumes: Creates a volume mapping between the./frontend/cypressfolder on the host machine and the/frontend/cypressfolder inside the container. This allows the Cypress tests to be stored and accessed in the container.command: Specifies the command to run when thecypressservice starts, which isyarn run cypress:runin this case. This command will execute the Cypress tests within the container.