apurbabehera / evershop

🛍️ NodeJS E-commerce Platform
https://evershop.io/
GNU General Public License v3.0
0 stars 0 forks source link

Setup automated unit testing #10

Open apurbabehera opened 4 months ago

apurbabehera commented 4 months ago

Setup automated testing for the EverShop project:

sinchanav08999 commented 4 months ago

1.) Clone the evershop into the local machine. 2.) Installe the JEST framework to run the test cases for nodeJS program. 3.) Run the unit tests cases manually in VSCode using the commands " npx jest ./unit ". 4.) Run the test cases to see the code coverage using the command " npx jest --coverage ./unit ".

apurbabehera commented 4 months ago
  1. Add code to jest.config.js reporters: [ "default", [ "jest-junit", { suiteName: "jest tests" } ] ]

  2. Install jest-junit node module: npm i jest-junit

  3. Run tests to create test report (junit.xml)

References: https://jestjs.io/docs/configuration#reporters-arraymodulename--modulename-options https://github.com/jest-community/jest-junit

ManoharMP3 commented 4 months ago

To run code coverage for the unit tests

npx jest --coverage ./unit To view the reports in console and file format cd evershop\coverage\test-results

Add this code to jest.config.js file

reporters: [ 'default', ['jest-junit', { outputDirectory: './coverage/test-results', outputName: 'junit.xml' }] ], collectCoverage: true, coverageReporters: ['html', 'cobertura'], coverageDirectory: './coverage/test-results'

For code coverage, we have 2 options to view the report:

  1. cobertura-coverage.xml in coverage\test-results\cobertura-coverage.xml
  2. index.html in coverage\test-results\junit.xml
ManoharMP3 commented 4 months ago

Image

Image

sinchanav08999 commented 4 months ago

test cases

sinchanav08999 commented 4 months ago

coverage

ManoharMP3 commented 3 months ago
pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                // Checkout the code from the GitHub repository
                git branch: 'dev', url: 'https://github.com/ManoharMP3/evershop123.git'
            }
        }

        stage('Test') {
            steps {
                // Install jest-junit and run tests
                bat 'npm install regenerator-runtime'
                bat 'npx jest --coverage ./unit'
            }
            post {
                success {
                    echo 'Tests passed successfully!'
                }
                failure {
                    echo 'Tests failed! Please check the logs for more details.'
                    error 'Tests failed!'
                }
            }
        }
    }
    post {
        always {
            // Publish JUnit test results
            junit 'test-results/junit.xml'
            // Archive code coverage report
            archiveArtifacts 'test-results/junit.xml'
            archiveArtifacts 'test-results/cobertura-coverage.xml'
            archiveArtifacts 'test-results/index.html'

            // Calculate code coverage percentage
            script {
                def coverageXml = readFile('test-results/cobertura-coverage.xml')
                def matcher = coverageXml =~ /line-rate="([\d.]+)"/
                def coveragePercentage = matcher ? Float.parseFloat(matcher[0][1]) * 100 : null

                // Check if code coverage is below the threshold (e.g., 70%)
                if (coveragePercentage != null && coveragePercentage < 40) {
                    echo "Code coverage dropped below 70% (${coveragePercentage}%), failing the pipeline."
                    error "Code coverage dropped below 70% (${coveragePercentage}%)"
                } else {
                    echo "Code coverage (${coveragePercentage}%) meets or exceeds the threshold (70%)."
                }
            }
        }
        success {
            // Publish HTML code coverage report
            publishHTML(target: [
                allowMissing: true,
                alwaysLinkToLastBuild: true,
                keepAll: true,
                reportDir: 'test-results',
                reportFiles: 'index.html',
                reportName: 'Code Coverage Report'
            ])
        }
        failure {
            echo 'Tests failed! Please check the logs for more details.'
            error 'Tests failed!'

        }

    }

}