Afternoon all. I’m looking at using Pact to verify...
# pact-js
m
Afternoon all. I’m looking at using Pact to verify message event communication via postMessage. Can’t seem to find anything on the web that covers this. The scenario: • a parent web app embeds an iframe • and needs to send data to that iframe • so it uses
postMessage
to do so • and the child iframe also sends data back to the parent frame via
postMessage
So I want to be able to verify the shape of the data that is sent via
postMessage
. Ideas on where to start? I’ve had a look at a lot of the videos and articles, and from what I can see, a Pact Provider needs to be one of the existing providers listed here: https://docs.pact.io/implementation_guides/workshops#providers But I might be missing something 🤓
t
This is a cool use case. It’s not really obvious from the pact message documentation, but a message pact is really just a function call
You would have your provider verification mock
postMessage
. You would have your consumer test implement something that calls
postMessage
with the data you’re expecting.
Probably what you'd do is actually test your listener - and not involve the iframe at all
1
most of the Pact message examples do this - eg, an example for SQS doesn't have a real SQS queue at any point. This is different to how Pact does HTTP testing
m
@Timothy Jones Brilliant, thanks for that! I was wondering if something that might be the case. I realised that my scenario was analogous to a queue, like the Kafka example, but couldn’t quite work out how to string it together. I’ll have another dig around, and if we end up going for it, will post back here.
🙌 2
t
It's definitely an appropriate use case. You might have to jump through more hoops if you want to actually send a message across a real iframe as part of the test, but it's possible.
m
I’ll try and keep it as simple as possible 🤓
🙌 1
m
we really could do with better docs / examples on what “message pact” can do. It’s not obvious it’s very general and can be used in a lot of weird and wonderful ways
If you get ^^ to work and you’re up for it, perhaps a PR to https://docs.pact.io/recipes would be helpful?
m
@Matt (pactflow.io / pact-js / pact-go) thanks for the message 🇦🇺 Yes, will try to do so if we go with Pact. Do you have anything else to add to what @Timothy Jones said?