Hi. Is there an example somewhere where MessageCon...
# pact-js
s
Hi. Is there an example somewhere where MessageConsumerPact is using withContent with matchers from v3 package? this example compiled without errors
Copy code
import {MessageConsumerPact, synchronousBodyHandler} from "@pact-foundation/pact"
import {uuid} from "@pact-foundation/pact/src/dsl/matchers"

it("uuid", async () => {
    return new MessageConsumerPact({
        consumer: "consumer",
        provider: "provider",
    })
        .expectsToReceive("uuid")
        .withContent({
            uuid: uuid("cc85f093-abe3-48d8-b187-32439d5b763e"),
        })
        .verify(synchronousBodyHandler(async () => new Promise((resolve) => {
            resolve("OK")
        })))
})
but this (use uuid from v3 package)
Copy code
import {MessageConsumerPact, synchronousBodyHandler} from "@pact-foundation/pact"
import {uuid} from "@pact-foundation/pact/src/v3/matchers"

it("uuid", async () => {
    return new MessageConsumerPact({
        consumer: "consumer",
        provider: "provider",
    })
        .expectsToReceive("uuid")
        .withContent({
            uuid: uuid("cc85f093-abe3-48d8-b187-32439d5b763e"),
        })
        .verify(synchronousBodyHandler(async () => new Promise((resolve) => {
            resolve("OK")
        })))
})
throw error like this
Copy code
TS2345: Argument of type '{ uuid: RegexMatcher; }' is not assignable to parameter of type 'AnyTemplate'.   Types of property 'uuid' are incompatible.     Type 'RegexMatcher' is not assignable to type 'string | number | boolean | JsonArray | JsonMap | Matcher<AnyTemplate> | ArrayMatcher<AnyTemplate> | TemplateMap | ArrayTemplate'.       Type 'RegexMatcher' is not assignable to type 'TemplateMap'.         Index signature for type 'string' is missing in type 'RegexMatcher'.
m
Hmm might be a type issue
It might accept the v2 types but not the v3 ones 🤔
s
yes, looks like type issue but where I can find an example for MessageConsumerPact with v3 messages? Or do we have separate MessageConsumerPact*V3* for v3 types?
m
Can you please raise an issue so we can address? In the mean time, can you stick with v2 matchers for now? Is there a v3 type you need? the uuid matcher I think is just a regex, so should be easily replicated in the v2 matchers
t
@Matt (pactflow.io / pact-js / pact-go) I think we have to back out the
AnyJson
types
It’s a breaking change, but we’ll keep seeing these issues if we don’t. There’s no way to express JSON as a type in typescript, unfortunately.
I can draw up a PR for it if you like
The benefit of the
AnyTemplate
type is that it prevents things like
Date
being inappropriately passed to the JSON expectations. The drawback is that it won’t let you use things that are
interfaces
without an index signature or freezing the object.
I think this drawback is worse than the advantage (especially as the advantage is that you get a cryptic error message from Typescript). It would be better to detect weird stuff being passed in and throw an error.
I feel bad about this - I forgot about these types when I did maintainer handover / when we talked about release checklists - I planned to have them released in the beta for a while to see if it was a problem, and then consider the design carefully before release.
There are a few other things that need to be fixed that would be good breaking-change candidates (eg, the conflated state function setups)
@SSh For now you I think can work around it by saying
as AnyTemplate
.