firebase / firebase/firebase-functions-test

makeMessage is incompatible with wrapV2

Open
#235 1 comment 1 reaction 0 assignees View on GitHub
documentation type: bug
Dominant language
TypeScript
Stars
248
Forks
63
PR merge metrics
No merged PRs in 30d

Description

### Version info

**firebase-functions-test:** 3.3.0

**firebase-functions:** 5.0.1

**firebase-admin:** 12.1.1

### Test case

```
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](https://github.com/firebase/firebase-functions-test/blob/c3c9abc4582231d827b026a75839d5b87b58b943/spec/v2.spec.ts#L1391).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.