我正在尝试将我的React代码(前端)的Jest测试(单元测试)作为Jenkins管道的一部分运行。在开发人员中运行测试时,它们可以正常工作而没有任何问题。但是,在Jenkins中运行时,我的所有测试都会显示以下错误,例如:
FAIL js/components/Common/__tests__/dateTimePickerFormControl.spec.js
● Test suite failed to run
Cannot find module 'joi' from 'index.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)at Object.<anonymous> (../db/models/User/index.js:3:13)
我已将 joi 添加到我的 package.json 中,甚至在jenkins步骤中为joi运行了npm install,以确保我拥有该软件包。另外,我已经在 / node_modules 目录中确认joi存在。
这是我的Jest配置:
"jest": {
"collectCoverage": true,
"notify": true,
"verbose": true,
"transform": {
".*": "<rootDir>/node_modules/jest-css-modules"
},
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"coverageReporters": [
"text",
"cobertura"
],
"moduleFileExtensions": [
"",
"js",
"jsx",
"json"
],
"setupFiles": [
"<rootDir>/test-env.js",
"mock-local-storage",
"./setupTests.js"
],
"moduleNameMapper": {
"^.+\\.(css|less|scss)$": "babel-jest"
},
"testPathIgnorePatterns": [
"/node_modules/",
"/Support/"
]
}
这是Jenkins管道脚本:
#!/usr/bin/env groovy
def wraps(body) {
wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm', 'defaultFg': 1, 'defaultBg': 2]) {
wrap([$class: 'TimestamperBuildWrapper']) {
body()
}
}
}
def notifySlack(String buildStatus = 'STARTED') {
// Build status of null means success.
buildStatus = buildStatus ?: 'SUCCESS'
def color
if (buildStatus == 'STARTED') {
color = '#0673ce'
wrap([$class: 'BuildUser']) {
msg = "${buildStatus}: ${env.JOB_NAME} - #${env.BUILD_NUMBER} - `${env.ENVIRONMENT}` Started by user ${BUILD_USER}"
}
} else if (buildStatus == 'SUCCESS') {
color = '#3ce04a'
msg = "${buildStatus}: ${env.JOB_NAME} - #${env.BUILD_NUMBER} - `${env.ENVIRONMENT}`\n ${env.BUILD_URL}"
} else if (buildStatus == 'UNSTABLE') {
color = '#d14536'
msg = "${buildStatus}: ${env.JOB_NAME} - #${env.BUILD_NUMBER} - `${env.ENVIRONMENT}`\n ${env.BUILD_URL}"
} else {
color = '#d14536'
msg = "${buildStatus}: ${env.JOB_NAME} - #${env.BUILD_NUMBER} - `${env.ENVIRONMENT}`\n ${env.BUILD_URL}"
}
print msg
slackSend(color: color, message: msg)
}
node() {
try{
notifySlack()
wraps {
stage('Clear Workspace') {
deleteDir()
}
stage('Docker cleanup'){
build job: 'cleanup-docker'
}
stage('Terraform build for Staging Environment'){
if(env.Environment == 'STAGING'){
build job: 'BUILD-Terraform-Env',
parameters: [[$class: 'StringParameterValue', name: 'BRANCH', value: String.valueOf(BRANCH)]]
}
}
stage('SCM Checkout') {
checkout([
$class: 'GitSCM', branches: [[name: '*/${BRANCH}']],
userRemoteConfigs: [[url: '${GIT_SERVER}',credentialsId:'xxxxxxx']]
])
}
stage('Git Config'){
sh "git config push.default current"
}
stage('Dist Cleanup'){
dir ('assets') {
sh "rm -rf dist"
}
}
stage('NPM Install & Test'){
try {
dir ('assets') {
sh "npm i"
sh "npm run test"
}
}
finally {
[$class: 'CoberturaPublisher', coberturaReportFile: 'output/coverage/jest/cobertura-coverage.xml']
}
}
stage('NPM Build'){
dir ('assets') {
sh "npm run build"
}
}
stage('Docker Build and Publish'){
app = docker.build("myapp", "-f Dockerfile-ECS --pull --rm .")
sh "`aws ecr get-login --region eu-west-1`"
docker.withRegistry('https://9871259817.dkr.ecr.eu-west-1.amazonaws.com') {
app.push("${BRANCH}-${BUILD_NUMBER}")
}
}
stage('Deploy on ECS'){
sh "ecs deploy --region eu-west-1 MYAPP-\${Environment} newjavascript-app -t \${BRANCH}-\${BUILD_NUMBER} --timeout 600 --ignore-warnings"
}
stage('Version Bump'){
if(env.Environment == 'PROD'){
dir ('assets') {
sh "npm run patch"
}
}
}
stage('Git Push'){
sh "git push origin HEAD:\${BRANCH}"
sh "git push --tags"
}
stage('Docker cleanup'){
build job: 'cleanup-docker'
}
}
} catch (e) {
currentBuild.result = 'FAILURE'
throw e
} finally {
notifySlack(currentBuild.result)
}
}