By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Running Cypress Tests for Node.js Apps Inside a Docker Container | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Running Cypress Tests for Node.js Apps Inside a Docker Container | HackerNoon
Computing

Running Cypress Tests for Node.js Apps Inside a Docker Container | HackerNoon

News Room
Last updated: 2025/03/26 at 3:07 AM
News Room Published 26 March 2025
Share
SHARE

💡 Introduction

Welcome Dev to the world of testing and Containerisation. Today, we will be diving into an exciting project, where we will be testing a Node Js application using cypress and this whole project will be inside a docker container. So without further ado, Let’s get started!

💡 Pre-requisites

Before we delve into the project, we need to ensure that we have following requirements met in our system:

  • Docker and Docker-Compose: Since we are working inside a container, we will be needing docker and docker-compose for that.
  • Node installed: We are testing a node application, so we would be needing node to run in it.
  • Basic Understanding of Docker: We will be creating a dockerfile and Docker-compose file, a prior understanding of both would be appreciated.

💡 Setting up the project

We will start the project by creating a project dir named book-app-testing, inside the directory, we will be creating a docker-compose file with the following content:

services:
 book-reader-app:
   image: rajudandigam/book-reader-node:v1
   container_name: book-reader-app
   ports:
     - "3000:3000" 
   networks:
     - test-network


 cypress:
   build:
     context: ./cypress-test
     dockerfile: Dockerfile
   container_name: cypress
   depends_on:
    - book-reader-app


   environment:
     - CYPRESS_baseUrl=http://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

Now inside the project dir, we will create a new dir, named cyprus-test. We will navigate inside it and run the following commands:

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

This will open a new Window. Here Select E2e Testing and then click on continue and start testing E2E testing in Electron.

Cypress runningCypress running

Now, Click on Create a new Spec File and in the name put cypress/e2e/book-app-heading.cy.js and click Okay and run the Spec file.

This will run a demo test. Now go to thebook-app-testing/cypress-test/cypress/e2e and here you will have your book-app-heading.cy.js file, remove the existing content and put the full information in it:

describe('Book Reader Node App', () => {
 it('should have a heading that includes "Book App"', () => {
   // Use the baseUrl from environment variable (preferred approach)
   cy.visit('/'); // This appends to CYPRESS_baseUrl (http://book-reader-app:3000)


   // Alternatively, hardcode the service name (less flexible):
   // cy.visit('http://book-reader-app:3000');


   // Check if any heading (h1, h2, etc.) contains "Book App"
   cy.get('h1, h2, h3, h4, h5, h6').should('contain.text', 'Book App');
 });
});

This is a simple test which will test whether the application heading has Book App in it.

💡 Creating the Dockerfile

Now, we will be creating a dockerfile for Cypress with our Test in it, for that, go inside the book-app-testing/cypress-test and create a Dockerfile and put the following content it:

#Use Cypress base image

FROM cypress/included:13.0.0


WORKDIR /e2e


# Copy project files
COPY . .


# Install dependencies
RUN npm install


# Run Cypress tests
CMD ["npx", "cypress", "run"]

Now, we will navigate to our project dir and run the following command to run the docker-container:

docker-compose up –build

This will give you the following output and in that, we can see that the test we provided is passed.

cypress running in terminalcypress running in terminal

To enhance the testing process, you can add following test in the same file or create separate file for each test inside the cypress-test/cypress/e2e:

// Render Header component Properly
describe('Book Reader Node App', () => {
 beforeEach(() => {
   cy.visit('/');
 });


 it('should render the header component', () => {
   cy.get('header').should('be.visible');
 });


 it('should render the sidebar component', () => {
   cy.get('aside').should('be.visible');
 });
});
// Check Animation Tap and Hover
describe('Book Reader Node App - Animations', () => {
 beforeEach(() => {
   cy.visit('/');
 });


 it('should scale up on hover', () => {
   cy.get('ul li').first().trigger('mouseover')
     .should('have.css', 'transform')
     .and('contain', 'matrix');
 });


 it('should scale down on tap', () => {
   cy.get('ul li').first().trigger('mousedown')
     .should('have.css', 'transform')
     .and('contain', 'matrix');
 });
});
// Check Test Responses in various Devices
describe('Book Reader Node App - Responsiveness', () => {
 beforeEach(() => {
   cy.visit('/');
 });


 const sizes = [
   { width: 1280, height: 720 },  // Desktop
   { width: 768, height: 1024 },  // Tablet
   { width: 375, height: 812 }    // Mobile
 ];


 sizes.forEach((size) => {
   it(`should display correctly on ${size.width}x${size.height}`, () => {
     cy.viewport(size.width, size.height);
     cy.get('main').should('be.visible');
   });
 });
});
// Test Naviation
describe('Book Reader Node App - Navigation', () => {
 beforeEach(() => {
   cy.visit('/');
 });


 it('should navigate to book details page on click', () => {
   cy.get('ul li a').first().click();
   cy.url().should('include', '/book/');
 });


 it('should display book details on the details page', () => {
   cy.get('ul li a').first().click();
   cy.get('h1').should('be.visible');
   cy.get('img').should('be.visible');
 });
});

You can also visit __http://localhost:300__0 where our application is running.

book reader app book reader app

💡 Conclusion

This marks the end of our blog. In today’s blog, we learnt how to use Cypress to test our Web Application built using Node. We started with a basic test to verify the heading contains “Book App” and gradually saw how we can expand our testing coverage with more complex and meaningful tests. By incorporating Cypress, we can ensure our app’s functionality, UI interactions, and navigation work as expected, making our web applications more robust and reliable.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article AI powering the future of health tech – UKTN
Next Article Nothing adds camera capture feature to Phone 3A’s Essential Space
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

DNA Testing Firm 23andme Fined £ 2.3m by UK Regulator for 2023 Data Hack
Software
Nabla Lands $70M To Build AI Agents In Healthcare Settings
News
The ‘made-in-America’ Trump Mobile phone sure looks like a made-in-China phone
News
Alibaba to sell full stake in offline shopping chain Intime Retail for $1 billion · TechNode
Computing

You Might also Like

Computing

Alibaba to sell full stake in offline shopping chain Intime Retail for $1 billion · TechNode

1 Min Read
Computing

Inside the ReAct Design Pattern: How Modern AI Thinks and Acts | HackerNoon

13 Min Read
Computing

LangSmith Bug Could Expose OpenAI Keys and User Data via Malicious Agents

5 Min Read
Computing

AMD Shares More Details On The Ryzen Threadripper 9000 Series Review

3 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?