Testing Cloud Functions using Jenkins

Hi everyone, I’m working in a startup and now we’ve decided to use Jenkins as tool for CI/CD because in our opinion it was the best choice.
We’re using Firebase services as backend and we’ve written our Unit tests using mocha.
I wrote the Jenkinsfile for the pipeline of our backend but I’m having a lot of difficulties while working with Firebase emulators.
My idea is the following: start the emulator in Jenkins and then in a stage after that one I run the npm test to test my endpoints.
This should be easy but when I start the emulators it starts correctly buy then it idles forever and I can’t go on with my tests.
So as a workaround I’m using parallel stages, one of the stages starts the emulator and the other one tests it after 5 minutes (5 minutes because the emulator takes a lot of time to start).
I need to test it of course but I was wondering if there’s a better way to approach this problem. How would you do this?
This is my Jenkinsfile

pipeline {
    agent any
    tools {
        nodejs '19.4.0'
    }
    environment {
        API_KEY_OPEN_DOTA = credentials('API_KEY_OPEN_DOTA')
        FORTNITE_API_KEY = credentials('FORTNITE_API_KEY')
        CLIENT_ID = credentials('CLIENT_ID')
        STORM_TOKEN = credentials('STORM_TOKEN')
        STRIPE_KEY = credentials('STRIPE_KEY')
        FIREBASE_LOGIN_TOKEN = credentials('FIREBASE_LOGIN_TOKEN')
    }
    stages {    
        stage('setup') {
            steps {
                sh 'echo API_KEY_OPEN_DOTA=\"$API_KEY_OPEN_DOTA\" >> .env'
                sh 'echo FORTNITE_API_KEY=\"$FORTNITE_API_KEY\"  >> .env'
                sh 'echo CLIENT_ID=\"$CLIENT_ID\" >> .env'
                sh 'echo STORM_TOKEN=\"$STORM_TOKEN\" >> .env'
                
            }
        }
        stage('installing npm packages') {
            steps {
                sh 'npm install'
                sh 'cd functions && npm install'
                sh 'npm install -g firebase-tools'
            }
        }
        stage('jenkins') {
            when {
                branch 'jenkins'
            }
                parallel{
                    stage('starting emulators') {
                        steps {                            
                            sh 'firebase emulators:start --project myProjectName-64ea9'
                        }                        
                    }
                    stage('running tests on emulator') {
                        steps {
                            sleep(time: 5, unit: 'MINUTES')
                            sh 'npm run testUnix ./tests/endPoints/auth.test.ts'
                        }    
                    }
                }                                        
        }
    }
}

        
    

    

                
            
        
    

    
    





    

       
       

    

    

    


I don’t know specifics, but it :sounds: like you are starting up a forground daemon in parallel with running the script. A forground will never end.

Could you do something like (in serial)

sh 'firebase emulators:start --project myProjectName-
// i recommend wait-for or other script to determine its ready instead of waiting 5 min
sh 'npm run testUnix ./tests/endPoints/auth.test.ts'
sh 'firebase emulators:stop --project myProjectName-'

type thing?

or uglier:

stage('jenkins') {
  steps {
    sh 'firebase emulators:start --project myProjectName- &'
    // i recommend wait-for or other script to determine its ready instead of waiting 5 min
    sh 'npm run testUnix ./tests/endPoints/auth.test.ts'
  }
  post {
    always {
      sh('killall firebase')
    }
  }
}

Jenkins should clean up any process started and still attached (parent pid not changed) when the job is run, so you may not even need to kill/stop it and just let jenkins clean it up

Hi! Thanks for your answers. I will try your proposals after I try to run at least one test…at the moment my tests are not hitting the endpoints and I have no idea why