简体   繁体   中英

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?

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.
  });
});

This will work on the process of hack

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM