Sorry, this is probably a super basic question, bu...
# pact-js
j
Sorry, this is probably a super basic question, but I cannot figure out how to get Matchers to work with my typescript consumer contract test. I have a contract test that is working, but I'm manually setting a UUID which the provider will generate, so I need the contract to accept any valid UUID. But when I change the element in my expected body from
Copy code
id: "ce118b6e-d8e1-11e7-9296-cec278b6b50a"
to
Copy code
id: Matchers.uuid("ce118b6e-d8e1-11e7-9296-cec278b6b50a")
I am now getting:
Copy code
Object {
-   "id": Object {
-     "data": Object {
-       "generate": "ce118b6e-d8e1-11e7-9296-cec278b6b50a",
-       "matcher": Object {
-         "json_class": "Regexp",
-         "o": 0,
-         "s": "^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$",
-       },
-     },
-     "getValue": [Function getValue],
-     "json_class": "Pact::Term",
-   },
+   "id": "ce118b6e-d8e1-11e7-9296-cec278b6b50a",
  }

> 92 |         expect(response).toEqual(responseData)
     |                          ^
This is all inside a
pactWith
block, because this is using the jest-pact library.
Like, it looks like pact simply isn't interpreting the matcher as a matcher and it's just comparing the matcher object to the uuid, which of course doesn't match because they're completely different types
Oh, I'm a dummy... I bet I can't use the matcher in my expected response, huh?
m
Hmm no that doesn't look like your fault. You can use marchers in most places. Ive seen it before where that happens but we've never been able to track it down reliably. I think removing the pact file and running again might help
t
I bet I can't use the matcher in my expected response, huh?
This is where you're supposed to use them
Can you share your whole test? You might be passing the matcher to the
expect
call, and you're not supposed to do that
1
The matcher is for pact. You use jest's
expect
to verify that the object you got out of the API call is correct - so it won't have matchers
If your
responseData
object has the matchers, you can remove them with
extractPayload(responseData)
- so you would do:
Copy code
expect(response).toEqual(extractPayload(responseData))
j
I should have followed up, this was exactly what I was doing. I was trying to use the same response data object in my pact mock response as in my jest
toEqual
expectation. Making separate objects for the two of them fixed it. I didn't know about
extractPayload
though, that's a really handy tip! Thank you
is
extractPayload
documented anywhere?