Running Cypress tests on Node.js with Docker

Learn to test Node.js applications with Cypress inside a Docker container. Discover how to set up your environment, create automated tests, and improve your code quality with interface and navigation testing.

miércoles, 26 de marzo de 2025 • 2 min read • Q2BSTUDIO Team

Company-Software-Apps-ArtificialIntelligence

?? Introduction

Welcome Dev to the world of testing and containerization. Today we will explore an exciting project where we will test a Node.js application using Cypress, all inside a Docker container. Without further ado, let's get started!

?? Prerequisites

Before you begin, make sure you have the following requirements on your system:

  • Docker and Docker-Compose: They are necessary to run the project in a container environment.
  • Node installed: Required to run the application under test.
  • Basic knowledge of Docker: We will create a Dockerfile and a Docker-compose file, so it is advisable to have a prior idea of how they work.

?? Project setup

We start the project by creating a directory called book-app-testing. Inside this directory, we will create a docker-compose file with the following content:

services:
 book-reader-app:
   image: node:14
   container_name: book-reader-app
   working_dir: /app
   volumes:
     - .:/app
   ports:
     - '3000:3000'
   command: ['npm', 'start']
   networks:
     - test-network

 cypress:
   build:
     context: ./cypress-test
     dockerfile: Dockerfile
   container_name: cypress
   depends_on:
     - book-reader-app
   environment:
     - CYPRESS_baseUrl=https://book-reader-app:3000
   volumes:
     - ./cypress-test:/e2e
   networks:
     - test-network
   command: ['npx', 'cypress', 'run', '--spec', 'cypress/e2e/book-app-heading.cy.js']

networks:
 test-network:
   driver: bridge

Next, inside the project directory, we create a folder called cypress-test and run the following commands:

npm init -y
npm install cypress --save-dev
npx cypress open

This will open a new window where we select E2E Testing and continue with the configuration.

?? Creating the test file

Inside the directory book-app-testing/cypress-test/cypress/e2e, we create the file book-app-heading.cy.js with the following content:

describe('Book Reader Node App', () => {
 it('should have a heading that includes Book App', () => {
   cy.visit('/');
   cy.get('h1, h2, h3, h4, h5, h6').should('contain.text', 'Book App');
 });
});

?? Creating the Dockerfile

To run Cypress in a container, inside book-app-testing/cypress-test, we create a Dockerfile with the following content:

FROM cypress/included:13.0.0
WORKDIR /e2e
COPY . .
RUN npm install
CMD ['npx', 'cypress', 'run']

Then, from the project directory, we run:

docker-compose up --build

This will run the tests and display the results in the terminal.

?? Conclusion

In this article we learned how to use Cypress to test a Node.js application inside a Docker container. We started with a basic test to verify the application title and progressed to more complex tests that validate aspects such as navigation and appearance on different devices. With Cypress, we ensure the functionality and stability of our web applications.

At Q2BSTUDIO, we offer innovative technological solutions and custom software development to drive your business growth. Our experience in automated testing and containerization allows us to optimize the performance and security of web applications. Contact us to take your project to the next level.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.