DEV Community

Mayra
Mayra

Posted on

Automating Unit Testing with Jenkins: A Complete Real-World Example

In the fast-paced world of software development, automation is key to delivering reliable code efficiently. Continuous Integration and Continuous Deployment (CI/CD) tools like Jenkins help developers automate repetitive tasks such as building, testing, and deploying applications.

In this article, we’ll explore how to use Jenkins to automate the execution of unit tests using a simple Node.js project. This includes a real-world working example, full code, and explanations. You’ll also find a public GitHub repository with everything set up.

What is Jenkins?
Jenkins is an open-source CI/CD server that allows developers to automate the building, testing, and deployment of software. It is highly customizable through a vast ecosystem of plugins and supports a wide range of languages, platforms, and tools.

Jenkins vs Other CI/CD Tools

Image description

Jenkins shines when you need maximum flexibility and control in your CI/CD pipelines.

Project Overview
We’ll build a simple Node.js application with a unit test, then automate testing using a Jenkins Pipeline. The project includes:

  • A basic JavaScript function (sum)
  • A test using Jest
  • A Jenkinsfile to define the pipeline

Project Structure:

jenkins-test-example/
├── Jenkinsfile
├── package.json
├── app.js
└── __tests__/
    └── app.test.js
Enter fullscreen mode Exit fullscreen mode

Jenkinsfile


pipeline {
    agent any

    stages {
        stage('Install Dependencies') {
            steps {
                sh 'npm install'
            }
        }

        stage('Run Tests') {
            steps {
                sh 'npm test'
            }
        }
    }

    post {
        success {
            echo '✅ Tests passed!'
        }
        failure {
            echo '❌ Tests failed!'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This Jenkinsfile defines two main stages: installing dependencies and running tests. It also provides a post-build message depending on success or failure.

package.json

{
  "name": "jenkins-test-example",
  "version": "1.0.0",
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "jest": "^29.0.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

app.js

function sum(a, b) {
  return a + b;
}

module.exports = sum;
Enter fullscreen mode Exit fullscreen mode

tests/app.test.js

const sum = require('../app');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});
Enter fullscreen mode Exit fullscreen mode

How to Run It Locally

  • Install Node.js
  • Clone or download the project
  • Run the following in terminal: npm install npm test You should see:
PASS  __tests__/app.test.js
✓ adds 1 + 2 to equal 3
Enter fullscreen mode Exit fullscreen mode

Public Repository

You can find the complete working project here:
https://github.com/mayrafc/Jenkins

Final Thoughts
Jenkins is a mature, flexible, and powerful tool for automating software development pipelines. With the example shown in this article, you can start creating your own automated testing workflows today.

If you're looking for full control and extensibility, Jenkins remains one of the best options in the DevOps world.

Top comments (0)