Hi. I am following the tutorial on <https://killer...
# pact-js
t
Hi. I am following the tutorial on https://killercoda.com/pactflow/scenario/pactflow-getting-started-js. And was wondering is there a way to get the test to just check that the client returns a response that matches the expected response in terms of the keys and types rather than an exact match on the values? Wouldn't this be more appropriate for a contract test than matching on exact values?
j
t
Hi Jan. Thanks for the response. In the 'like' is used in the MockProvider but its in the expect statement in the assert stage which is doing a deep.equal which seems to only pass if the response is exactly the same i.e structure, types and values.
const expectedProduct = { id: 10, type: "pizza", name: "Pepperoni" }; await mockProvider.addInteraction({ state: "a product with ID 10 exists", uponReceiving: "a request to get a product", withRequest: { method: "GET", path: "/products/10", }, willRespondWith: { status: 200, headers: { "Content-Type": "application/json; charset=utf-8", }, body: like(expectedProduct), }, }); // (5) Act const api = new ProductApiClient(mockProvider.mockService.baseUrl); const product = await api.getProduct(10); // (6) Assert that we got the expected response expect(product).to.deep.equal(new Product(10, "Margharita", "pizza")); }); });
Sorry I think I might have misunderstood how the consumer pact works. When I run the provider pact specs it passes successfully even if values are different.
which is want we want to see.
y
correct, by using matchers, you make the verification less brittle and reliant on the example data. Pact is specification by example, so we want something nice to render on the the screen if you view the pact, but you usually want to check the shape of the data, and not particular unique values themselves (bar within either a discrete type, or whatever bounds you place on it in your consumer test) The matchers in the consumer side generate example code, which you can use in your consumer tests to assert that your api client under test unmarshalls the correct response based on the example data you've provided (where you probably care about the value you passed in)
Pact is essentially a unit testing framework using specification by example. It just so happens that to be able to run those tests on the API consumer and provider side, it needs to generate an intermediate format to be able to communicate that structure - this is the specification, matchers and generators are encoded into this specification 🙂
t
ok, thanks for the explanation Yousef.
👍 1
Hi Yousef. Just one more question, Is the only reason you would create the Product class and assert against this because this particular client app uses this class. If this was a contract test between 2 apis could you just as easily do something like ..
expect(product).to.deep.equal(expectedProduct); where expectedProduct is ... const expectedProduct = { id: 10, type: "pizza", name: "Margharita"};