Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
nodejs unit test PubSub.publish not sending data

I'm having issues doing a unit test with the following environment:

"pubsub-js": "^1.9.2",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.2.0",
"chai": "^4.2.0",
"firebase-functions-test": "^0.2.3",
"mocha": "^8.2.1",
"ts-node": "^9.1.1",
"ts-sinon": "^2.0.1",

When publish a message to my pubsub the data received by it is always: Message { data: undefined, attributes: {}, _json: undefined } and I can't figure out why.

There is some code to describe my scenario:

pubsub-myFunc.ts

export const pubsubMyFunc= functions.pubsub
  .topic("on-myTopic")
  .onPublish(async (message) => {
     console.log("version 1")

     console.log(message)
     /**
      * Received message from topic
      */
     const myMessage = Buffer.from(message.data, "base64").toString("utf-8")

pubsub-myFunc.spec.ts

import * as functions from 'firebase-functions';
import * as PubSub from 'pubsub-js';
import * as tsSinon from 'ts-sinon';

import { pubsubMyFunc } from './pubsub-myFuncr';
import * as sendEmail from './send-mail';

describe("PubSub tests", () => {
  beforeEach(() => {
    process.env.GCLOUD_PROJECT = "my env"
  })

  it("Should call sendNotificationMessage", function (done) {
    // this.timeout(60000)
    const today = new Date()
    const data = {
      dateCreated: today,
      expireDate: today.getDate() + 30,
      objId: "id",
      objParentId: "parentId",
    }

    const spy = tsSinon.default.spy(sendEmail, "sendNotificationMessage")

    const dataBuffer = Buffer.from(JSON.stringify(data))

    const pubsubMessage = new functions.pubsub.Message(dataBuffer)

    PubSub.subscribe("on-myTopic", pubsubMyFunc)

    console.log("publish")
    PubSub.publish("on-myTopic", pubsubMessage)

    setTimeout(() => {
      // check if spy was called
      tsSinon.default.assert.calledOnce(spy)
      done()
    }, 15000)
  })
})

I have tried to pass directly the dataBuffer but without any luck as well, the outputs of my console logs are:

publish
version 1
Message { data: undefined, attributes: {}, _json: undefined }

Is there any reason for my Message.data to be undefined?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I'm not sure if pubsub-js's subscribe function is compatible with the output of firebase-functions' onPublish method.

You could use firebase's emulators to run your functions locally and test them by sending messages like you would normally instead of going through pubsub-js.

export const pubsubMyFunc= functions.pubsub
  .topic("on-myTopic")
  .onPublish(async (message) => {
     console.log("version 1");

     console.log(message);
     /**
      * Received message from topic
      */
     const myMessage = message.json;
...

Make sure pubsubMyFunc is being exported at firebase functions entry point.

Install and configure firebase emulators, make sure PUBSUB_EMULATOR_HOST env var is set to localhost:$PORT where $PORT is set to the port you have configured for pub sub emulator, located in firebase.json under emulators.pubsub.port.

Once that's all setup, you can send a message to pubsub by:

import {PubSub} from "@google-cloud/pubsub";

cont getTopic = () => {
  const topic = new PubSub().topic("on-myTopic");

  topic.exists().then(([exists]) => exists
    ? topic
    : topic.create().then(([newTopic]) => newTopic)
  );
};

describe("PubSub tests", () => {
  let topic;

  beforeEach(async() => {
    process.env.GCLOUD_PROJECT = "my env"
    topic = await getTopic();
  });

  it("should test something", async() => {
    await topic.publishJSON(const data = {
      dateCreated: today,
      expireDate: today.getDate() + 30,
      objId: "id",
      objParentId: "parentId",
    });

    // rest of your code here.
  });
});
over 4 years ago · Santiago Trujillo Report

0

This will work on the process of hack

   export const pubsubMyFunc= functions.pubsub
      .topic("on-myTopic")
      .onPublish(async (message) => {
         console.log("version 1");
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!