Unit testing in jenkins is failing but working in local

Unit testing stage in jenkins is failing with below error,
22:26:53 > mocha app-test.js --timeout 10000 --reporter mocha-junit-reporter --exit
22:26:53
22:26:54 (node:311670) [MONGOOSE] DeprecationWarning: Mongoose: the strictQuery option will be switched back to false by default in Mongoose 7. Use mongoose.set('strictQuery', false); if you want to prepare for this change. Or use mongoose.set('strictQuery', true); to suppress this warning.
22:26:54 (Use node --trace-deprecation ... to show where the warning was created)
22:26:54 /jenkinsdata/devops_playground/gb-testing/gb-public-cicd/workspace/node_modules/mocha-junit-reporter/index.js:217
22:26:54 return testsuites[testsuites.length - 1].testsuite;
22:26:54 ^
22:26:54
22:26:54 TypeError: Cannot read properties of undefined (reading ‘testsuite’)
22:26:54 at lastSuite (/jenkinsdata/devops_playground/gb-testing/gb-public-cicd/workspace/node_modules/mocha-junit-reporter/index.js:217:46)
22:26:54 at MochaJUnitReporter. (/jenkinsdata/devops_playground/gb-testing/gb-public-cicd/workspace/node_modules/mocha-junit-reporter/index.js:262:5)
22:26:54 at Runner.emit (node:events:519:35)
22:26:54 at Runner.fail (/jenkinsdata/devops_playground/gb-testing/gb-public-cicd/workspace/node_modules/mocha/lib/runner.js:502:8)
22:26:54 at Runner._uncaught (/jenkinsdata/devops_playground/gb-testing/gb-public-cicd/workspace/node_modules/mocha/lib/runner.js:1032:12)
22:26:54 at process.emit (node:events:507:28)
22:26:54 at process._fatalException (node:internal/process/execution:154:25)
22:26:54
22:26:54 Node.js v23.8.0

But working fine in local,

npm test

Solar [email protected] test
mocha app-test.js --timeout 10000 --reporter mocha-junit-reporter --exit

(node:21064) [MONGOOSE] DeprecationWarning: Mongoose: the strictQuery option will be switched back to false by default in Mongoose 7. Use mongoose.set('strictQuery', false); if you want to prepare for this change. Or use mongoose.set('strictQuery', true); to suppress this warning.
(Use node --trace-deprecation ... to show where the warning was created)
Server successfully running on port - 3000
PS solar-system-23> echo $?
True

Can anyone suggest something here?

What does your Jenkinsfile look like, to start with. And do the variables it references actually exist in Jenkins?

I am just following the lecture of Jenkins Pipelines. I have declared variables for mongo db uri, uname and password.

pipeline{
agent any
tools {
nodejs ‘Nodejs-gb-23.8.0’
}

environment {
    MONGO_URI = "mongodb+srv://supercluster.d83jj.mongodb.net/superData"
}

stages{
    stage("Checking node version in jenkins and installing dependencies") {
        steps{
            sh '''
                node -v
                npm -v
                npm install --no-audit
            '''
    }
    }

    stage ("Dependency Scanning"){
        parallel {
            stage ("NPM DEP CHECKING") {
                steps{
                    sh '''
                        npm audit --audit-level=critical
                        echo $?
                    '''
                }
            }
            stage ("OWASP DEP CHECKING"){
                steps{
                    dependencyCheck additionalArguments: '''--scan \'./\'
                        --out \'./\'
                        --format \'ALL\'
                        --prettyPrint''', odcInstallation: 'owasp-12.1.1'
                    
                    dependencyCheckPublisher failedTotalCritical: 1, pattern: 'dependency-check-report.xml', stopBuild: true

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

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

                }
            }
        }
    }

    stage ("Executing unit tests"){
        steps{
            withCredentials([usernamePassword(credentialsId: 'mongo-db-gb', passwordVariable: 'MONGO_PASSWORD', usernameVariable: 'MONGO_USERNAME')]) {  
                 sh 'npm test'
        }
            junit allowEmptyResults: true, skipOldReports: true, stdioRetention: '', testResults: 'test-results.xml'     
        }
    }
}

}

What were the values of the credentails you used? They are all case dependent, so if you get the case wrong, it won’t work. The code itself looks right to me. Also: what was the exact error you see in the Jenkins logs?

Hi Rob,

Error has been resolved, after changing the PORT number used in the application. thanks for your support.