0
votes

I am working on a normal express backed API project which is using firebase functions for some performing offline data processing. In many cases the API raises event to pub-sub which is later picked up by cloud function. A typical example is all the aggregations(total sales for day/month) are all pushed to cloud function.

I am looking at having an integration test suite using firebase emulators where I can test from the API layer till cloud functions getting triggered and its updates to data store. Having integration test suite on emulator provides me good advantage on speed and cost along with quick local validation.

I am calling my regular test script with firebase emulator

firebase emulators:exec "npm run test"

On running the tests I can all the pubsub functions initialised.

enter image description here

Subsequently I can see that message is also pushed to topic.

enter image description here

However I don't see corresponding subscription firebase functions executing. My firebase function skeleton is like

export const addIncentiveData = functions
    .region("asia-south1")
    .pubsub.topic("on_wf_data_added")
    .onPublish(async (message, context) => {
        const FUNCTION_NAME = `addIncentiveData`;
        console.log(`[${FUNCTION_NAME}] Entering `)
        const FUNCTION_NAME = `addIncentiveData`;
        logger.info(`[${FUNCTION_NAME}] : The function was triggered at ${context.timestamp}`);
        logger.debug(`[${FUNCTION_NAME}] : The message is ${JSON.stringify(message)}`);

        const payLoad = JSON.parse(Buffer.from(message.data, "base64").toString());
        logger.debug(`[${FUNCTION_NAME}] : The payload is ${JSON.stringify(payLoad)}`);
        
        //some processing

        logger.info(`[${FUNCTION_NAME}] : Completed processing`);
        return;
    });
1

1 Answers

1
votes

The issue was with my configuration. The mocha tests were calling the API and API's were pushing the message to real pubsub topic. I had to put in the variables to let my API know to work with emulators.

I added below variables to my .env

PUBSUB_EMULATOR_HOST=localhost:8085
PUBSUB_PROJECT_ID=my-project

Subsequently I could see all the messages getting pushed to emulator while emulator was running.

Source : Firebase PubSub Emulator not recieving messages