GYLD
← All posts

22 July 2024

Mastering Modern Web Development Tools: Automated Testing and Continuous Integration with Jest and Jenkins

#Automated Testing · #Continuous Integration · #CI · #Jest · #Jenkins · #Development

Mastering Modern Web Development Tools: Automated Testing and Continuous Integration with Jest and Jenkins

Welcome back to our blog series on mastering modern web development tools. In this post, we'll explore automated testing and continuous integration (CI) with Jest and Jenkins. Automated testing ensures the reliability of your codebase, and CI enables rapid development cycles by integrating code changes frequently and running tests automatically. Let’s dive into the importance of automated testing, how to get started with Jest and Jenkins, and some best practices to follow.


The Importance of Automated Testing


Automated testing is crucial for maintaining code quality and ensuring that new changes don’t break existing functionality. It allows developers to detect bugs early, reduces manual testing effort, and provides confidence in code deployments. At The Gyld, we emphasize automated testing to maintain high standards of quality and reliability in our software projects.


Getting Started with Jest


Jest is a delightful JavaScript testing framework with a focus on simplicity. It provides a comprehensive testing solution, including support for unit tests, integration tests, and snapshot tests. Here’s how to get started with Jest:


1. Installing Jest


Install Jest as a development dependency in your project:


npm install --save-dev jest

2. Writing Your First Test


Create a test file with the extension .test.js and write your first test:


// sum.test.js
const sum = (a, b) => a + b;

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

3. Running Tests


Run your tests using the Jest command:


npx jest

Jest will automatically find and run all test files with the .test.js extension, providing a detailed report of the results.


4. Snapshot Testing


Jest also supports snapshot testing, which is useful for testing UI components. Create a snapshot test for a React component:


// MyComponent.test.js
import React from 'react';
import renderer from 'react-test-renderer';
import MyComponent from './MyComponent';

test('renders correctly', () => {
  const tree = renderer.create().toJSON();
  expect(tree).toMatchSnapshot();
});

Run the test, and Jest will create a snapshot file that you can use to track changes in the component’s output over time.


Getting Started with Jenkins


Jenkins is an open-source automation server that facilitates CI by automating the building, testing, and deployment of software. Here’s how to get started with Jenkins:


1. Installing Jenkins


Download and install Jenkins from the official website: Jenkins Downloads. Follow the installation instructions for your operating system.


2. Setting Up Jenkins


Once installed, start Jenkins and complete the setup wizard. Create a new job by clicking on "New Item", enter a name for your job, and select "Freestyle project".


3. Configuring a Build Job


In the job configuration, specify the repository URL and configure the build steps. Here’s an example configuration for a Node.js project:


# Example Build Steps
1. Source Code Management:
  - Git: https://github.com/your-repo.git
2. Build Triggers:
  - GitHub hook trigger for GITScm polling
3. Build Environment:
  - Provide Node & npm bin/ folder to PATH
4. Build Steps:
  - Execute shell: npm install
  - Execute shell: npm test

4. Setting Up Jenkinsfile


For more complex workflows, you can use a Jenkinsfile to define your pipeline as code. Create a Jenkinsfile in your project’s root directory:


pipeline {
    agent any

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

Commit the Jenkinsfile to your repository, and configure Jenkins to use the pipeline script from SCM.


Embracing Best Practices


To make the most out of Jest and Jenkins, follow these best practices:


1. Write Comprehensive Tests


Ensure that you write comprehensive tests covering all aspects of your application, including edge cases and error handling. This improves code coverage and ensures your application behaves as expected:


// Example comprehensive test
const multiply = (a, b) => a * b;

test('multiplies 2 * 3 to equal 6', () => {
  expect(multiply(2, 3)).toBe(6);
});

test('multiplies 0 * 5 to equal 0', () => {
  expect(multiply(0, 5)).toBe(0);
});

2. Integrate Tests into CI


Integrate your tests into the CI pipeline to ensure that tests are run automatically on every code change. This helps catch issues early and maintains code quality:


pipeline {
    agent any

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

3. Use Test Coverage Tools


Use tools like Jest’s built-in coverage report to monitor test coverage and identify untested parts of your code:


# Run tests with coverage
npx jest --coverage

Review the generated coverage report to ensure your tests cover all critical code paths.


4. Keep Your Jenkinsfile Simple and Modular


Keep your Jenkinsfile simple and modular by breaking down complex pipelines into smaller stages. This makes your pipeline easier to maintain and debug:


pipeline {
    agent any

    stages {
        stage('Install Dependencies') {
            steps {
                sh 'npm install'
            }
        }
        stage('Lint Code') {
            steps {
                sh 'npm run lint'
            }
        }
        stage('Run Tests') {
            steps {
                sh 'npm test'
            }
        }
        stage('Build') {
            steps {
                sh 'npm run build'
            }
        }
    }
}

The Gyld’s Commitment to Quality and Continuous Integration


At The Gyld, we prioritize quality and continuous integration to ensure our software is reliable and maintainable. By using Jest for automated testing and Jenkins for CI, we maintain high code quality and deliver updates efficiently.


We continuously review and refine our testing and CI strategies, participate in code reviews, and stay updated with the latest tools and techniques. This commitment to quality and continuous integration allows us to deliver high-quality software and maintain the trust of our clients.


Conclusion


Automated testing and continuous integration with Jest and Jenkins are essential for modern web development. By understanding and using these tools effectively, you can maintain high code quality, catch issues early, and deliver updates efficiently.


Stay tuned for the next post in our series, where we’ll explore progressive web apps (PWAs) with Workbox and Service Workers. Until then, keep learning, stay curious, and embrace change. That’s the Gyld way.