import {makeMessage} from "firebase-functions-test/lib/providers/pubsub";
import {onMessagePublished} from "firebase-functions/v2/pubsub"
import {wrapV2} from "firebase-functions-test/lib/v2";
import {expect} from "chai";
describe("example", () => {
it("makeMessage breaks PubSub testing",
async () => {
const helloBase64 = "aGVsbG8="
const rawPartial = {data: {message: {data: helloBase64}}}
const partialUsingMakeMessage= {data: {message: makeMessage(helloBase64)}}
const getEventData = onMessagePublished(
"custom-test-topic",
(event) => {
return event.data.message.data
}
)
/*
The raw partial looks like:
{
data: {
message: { data: 'aGVsbG8=' }
}
}
*/
console.log("rawPartial: ", rawPartial)
/*
The makeMessage partial looks like:
{
data: {
message: Message { data: 'aGVsbG8=', attributes: {}, _json: undefined }
}
}
*/
console.log("partialUsingMakeMessage: ", partialUsingMakeMessage)
const eventDataUsingRawPartial = wrapV2(getEventData)(rawPartial)
expect(eventDataUsingRawPartial).equals(helloBase64)
// This will throw an error: SyntaxError: Unexpected token 'h', "hello" is not valid JSON.
const eventDataUsingMakeMessage = wrapV2(getEventData)(partialUsingMakeMessage)
// This line won't be reached.
expect(eventDataUsingMakeMessage).equals(helloBase64)
})
})
Steps to reproduce
Just run the code as a mocha test.
Expected behavior
If I wrap a v2 cloud function that consumes PubSub events with wrapV2 and then invoke the wrapped function on {data: {message: makeMessage(someBase64Data}, the cloud function gets invoked, and the event passed to it has a data.message.data field that matches someBase64Data.
Actual behavior
Some code decodes the base64 data and then tries to parse it as JSON, which results in a SyntaxError.
Additional thoughts
My code doesn't provide or read any JSON because it's using protobufs.
Maybe makeMessage isn't meant to work with wrapV2, but then it's unclear to me what it's supposed to be used for.
I think that the library could use more documentation around what to pass to the wrapped cloud functions. It makes it clear that I need to call wrap(myCloudFunction), what to pass to that wrapped function varies with the event type (Firestore document creation vs PubSub message) and was unclear to me after reading the documentation.
Alternatively, you could just point people to the library's internal tests for examples. The internal tests provide examples for cases that the officially published sample code doesn't cover. I figured out how to make things work based on this test.
Version info
firebase-functions-test: 3.3.0
firebase-functions: 5.0.1
firebase-admin: 12.1.1
Test case
Steps to reproduce
Just run the code as a mocha test.
Expected behavior
If I wrap a v2 cloud function that consumes PubSub events with
wrapV2
and then invoke the wrapped function on{data: {message: makeMessage(someBase64Data}
, the cloud function gets invoked, and theevent
passed to it has adata.message.data
field that matchessomeBase64Data
.Actual behavior
Some code decodes the base64 data and then tries to parse it as JSON, which results in a SyntaxError.
Additional thoughts
makeMessage
isn't meant to work with wrapV2, but then it's unclear to me what it's supposed to be used for.wrap(myCloudFunction)
, what to pass to that wrapped function varies with the event type (Firestore document creation vs PubSub message) and was unclear to me after reading the documentation.