Jenkins Pipelines – Code Coverage Stage Issue (No Steps)

Hi KodeKloud :wave:,

I’m working on enhancing my Jenkins Pipeline to include code quality checks, testing, and code coverage reporting. I’ve added a stage for code coverage, but when I run the pipeline, I get this warning:

“Code Coverage: No steps. This stage has no steps.






Chapter Context:
:books:course: Jenkins Pipelines – Code Quality and Testing
:mag: Subtopic: Code Coverage and Catching Errors
my Jenkinsfile:
pipeline {
agent any

tools {
    nodejs 'nodeJS 20.19.2'
}

environment {

MONGO_URI = “mongodb+srv://superclister.d83jj.mongodb.net/superData”
}

options {
    disableResume()
    disableConcurrentBuilds abortPrevious: true
}
stages {
    stage('Installing Dependencies') {
        options { timestamps() }
        steps {
            sh 'npm install --no-audit'
        }
    }

    stage('Dependency scanning') {
        parallel {
            stage('NPM Dependency Audit') {
                steps {
                    sh 'npm audit --audit-level=critical || true'
                }
            }

            stage('OWASP Dependency Check') {
                steps {
                    dependencyCheck additionalArguments: '''--scan . \
                         --project "solar-system" \
                         --format ALL \
                         --out dependency-check-report \
                         --prettyPrint \
                         --failOnCVSS 7 \
                         --enableExperimental \
                         --suppression my-suppression.xml''',
                    odcInstallation: 'OWASP-Depcheck-12'

                    dependencyCheckPublisher(
                        failedTotalCritical: 1,
                        pattern: 'dependency-check-report.xml',
                        stopBuild: true
                    )

                    junit allowEmptyResults: true, testResults: 'dependency-check-junit.xml'

                    publishHTML([
                        allowMissing: true,
                        alwaysLinkToLastBuild: true,
                         keepAll: true,
                        reportDir: './',
                        reportFiles: 'dependency-check-jenkins.html',
                        reportName: 'Dependency Check HTML Report',
                        useWrapperFileDirectly: true
                    ])
                }
            }
        }
    }

    stage('Unit Testing') {
        options { retry(2) }
        steps {
                withCredentials([usernamePassword(credentialsId: 'mongo-db-cred	', passwordVariable: 'MONGO_PASSWORD', usernameVariable: 'MONGO_USERNAME')]) {

}

            junit allowEmptyResults: true, testResults: 'test-results.xml'                }
   }
}

}

   stage('Code Coverage') {
steps {
    withCredentials([usernamePassword(credentialsId: 'mongo-db-cred', passwordVariable: 'MONGO_PASSWORD', usernameVariable: 'MONGO_USERNAME')]) {
        catchError(message: 'Oops! it will be fixed in future releases', stageResult: 'UNSTABLE') {
            sh 'npm run coverage'
        }
    }

    publishHTML([
        allowMissing: true,
        alwaysLinkToLastBuild: true,
        keepAll: true,
        reportDir: 'coverage/lcov-report',
        reportFiles: 'index.html',
        reportName: 'Code Coverage Report'
    ])
}

}

Hard to tell, especially since you’re using screenshots of your code, rather than putting it in

codeblocks
  like this!!

Since I can’t try your code, I can’t really check the syntax, which is what I’d need to do in order to figure out if there is something wrong with your “Code Coverage” stage of the Jenkinsfile.

1 Like

I’ve edited my questions—please check them

My version of the file seems to work better – I don’t get the Java exception you get. It looks like this:

pipeline {
    agent any
    tools { nodejs 'NodeJS 16.20.2' }
    environment {

       MONGO_URI = "mongodb+srv://supercluster.d83jj.mongodb.net/superData"
    }
    stages {
        stage('Install Dependencies') {
            steps {

                sh 'npm install --no-audit'

            }
        }
        stage('Run Unit Tests') {
		    // some block

            steps {
		withCredentials([usernamePassword(credentialsId: 'mongo-db-credentials', passwordVariable: 'MONGO_PASSWORD', usernameVariable: 'MONGO_USERNAME')]) {
                    sh 'npm test'
               }
            }
       }

       stage('Code Coverage') {
	 steps {
	    withCredentials([usernamePassword(credentialsId: 'mongo-db-credentials', passwordVariable: 'MONGO_PASSWORD', usernameVariable: 'MONGO_USERNAME')]) {
		catchError(message: 'Oops! it will be fixed in future releases', stageResult: 'UNSTABLE') {
		    sh 'npm run coverage'
		}
	    }

	    publishHTML([
		allowMissing: true,
		alwaysLinkToLastBuild: true,
		keepAll: true,
		reportDir: 'coverage/lcov-report',
		reportFiles: 'index.html',
		reportName: 'Code Coverage Report'
	    ])
	}
	}
    }
}

I’ve omitted the dependency check (takes too long) and note that my mongo db credential has a slightly different name, but I seem to have avoided a syntax error that you may have encountered.

One way to help with that is to use good indentation in your Jenkinsfile code. Jenkins will run a file with bad indentation fine, but it can be hard to find your errors if your style isn’t better. So you may want to fix that as well, to make sure that your code is set up the way you think it is.

My code executed; publishHTML did not run, but it was happier:

ERROR: Coverage for lines (88.88%) does not meet global threshold (90%)
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |   88.88 |       50 |    87.5 |   88.88 |                   
 app.js   |   88.88 |       50 |    87.5 |   88.88 | 21,47-48,56       
----------|---------|----------|---------|---------|-------------------
[Pipeline] }
ERROR: Oops! it will be fixed in future releases
ERROR: script returned exit code 1
Setting overall build result to FAILURE
[Pipeline] // catchError
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] publishHTML
[htmlpublisher] Archiving HTML reports...
[htmlpublisher] Archiving at BUILD level /var/lib/jenkins/workspace/lar-system_feature_enabling-cicd/coverage/lcov-report to /var/lib/jenkins/jobs/gitea-organization/jobs/solar-system/branches/feature-enabling-cicd.do7rqr/builds/12/htmlreports/Code_20Coverage_20Report
[htmlpublisher] Copying recursive using current thread
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
[Gitea] Notifying branch build status: FAILURE There was a failure building this commit
[Gitea] Notified
[Gitea] do not publish assets due to build being non-Successfully
Finished: FAILURE