Hi folks, We (my team) have a react consumer and ...
# general
g
Hi folks, We (my team) have a react consumer and a ruby provider, and we’re doing work refactoring one of the endpoints on the provider, by removing unused attributes from the returned JSON in order to improve performance. I want to write a test that would validate that after changes to the provider, the relevant component on the consumer can still render correctly. My idea so far is like this: 1. Update contract test on consumer to match what I expect the backend to return 2. Update my component’s unit test on the consumer to ensure the component will render correctly with the updated backend data 3. Once everything validates, make changes to the backend. Does this sound correct? If so, is there a smart way of not having to maintain two sample API responses on the consumer side (one for the contract test, one for the component’s unit test?) In other words, is there some best practice to reuse data setup for a contract test in a component’s unit tests?
m
You could use the data from the Pact tests in your component tests, so you don’t have separate mocks that could drift
In the 9.x.x version
extractPayload
in the matchers package lets you do this (it removes any matching rules and gives you the raw JSON.
g
Oh that’s very nice, I didn’t know about
extractPayload
. Thanks Matt!
👍 1
m
We need to port this to 10.x.x. let me know how you go and if this helps
g
extractPayload
has made my life much easier overall. We were going to upgrade to 10.x but if not having
extractPayload
available seems like a solid reason to hold back. Is there any alternatives in 10.x?
🙌 1
m
It’s a pretty simple little function. It just recurses the object and removes any Matchers.
g
Ah fair! I’ll take a look at the code and roll my own then
m
9.x.x
extractPayload
function: https://github.com/pact-foundation/pact-js/blob/c0f3d37b3a1f1843d4e92182b388bfe23d95e5c8/src/dsl/matchers.ts#L290-L309 It just needs one here (10.x.x): https://github.com/pact-foundation/pact-js/blob/master/src/v3/matchers.ts If you’re up for writing that, it would be ace. Please also call the new function
reify
and type alias it as
extractPayload
(or maybe we don’t bother keeping both, and add it to the upgrade guide?) Reify is the term used in other parts of the project and it has been pointed out we should make that consistent
g
Working on it 🙂
Never seen this syntax before:
Copy code
export function isMatcher(x: AnyTemplate): x is Matcher<AnyTemplate> {
  return x != null && (x as Matcher<AnyTemplate>).getValue !== undefined;
}
what is this
x is Matcher<AnyTemplate>
?
Oh it’s a type predicate. TIL
🙌 1