BookNest's tests need PostgreSQL 1,289 , and Docker's compose.yaml already has a test service, built from the Dockerfile's test stage (which runs npm 2,036 ci), that waits for a healthy db. The pipeline reuses it:
stage('Test') {
steps {
sh 'mkdir -p test-results'
sh '''docker compose --profile test run --rm --build \
-e NPM_CONFIG_UPDATE_NOTIFIER=false \
-v "$WORKSPACE/test-results:/app/test-results" test npm run test:ci'''
}
post {
always {
junit 'test-results/*.xml'
sh 'docker compose --profile test down -v --rmi local'
}
}
}Parallel builds stay apart: COMPOSE_PROJECT_NAME (set in Image Tagging) gives each build its own containers, network and volume, and DB_PORT=0 publishes PostgreSQL on a free host port. test:ci, a script Sam added to package.json, runs node --test with two reporters, spec to the console and junit to test-results/junit.xml, which reaches the workspace through the bind mount (hence the same-path workspace of Docker Agents):
curl -s -u "admin:$JENKINS_TOKEN" http://localhost:32080/job/booknest/job/main/5/consoleText \
| grep -E 'db-1 (Started|Healthy)|✔|^ℹ (tests|pass|fail)|Recording test' | cut -c1-95Container l2-ci-main-e2d7a8a-5-db-1 Started Container l2-ci-main-e2d7a8a-5-db-1 Healthy ✔ GET /health reports ok without touching the database (149.978111ms) ... ✔ GET / serves the static front end (15.588601ms) ℹ tests 9 ℹ pass 9 ℹ fail 0 Recording test results
The stage took 21 seconds, mostly building the test image and starting PostgreSQL.