https://pact.io logo
#pact-js
Title
# pact-js
a

Akash Srivastava

05/08/2022, 3:25 PM
Hi folks. First off, pact is awesome !! We are new to contract testing and trying to write a set of consumer tests for a UI that consumes a graphql API. With this, we are aiming to define how the same graphql query would generate different response based on different provider states. But despite specifying the correct expected response for each use case (with same request query), we are seeing all subsequent tests except the first failing by expecting the response from the first one. This happens only when all specs are running together. When specs are run individually (by removing/commenting all others), they pass as expected. When changing the order of tests, the first one passes always, with the rest failing. We are using a combination of jest-pact with GraphQLInteraction from pact. Here is a very minimal example that will demo that problem we’re facing. https://github.com/akashdotsrivastava/pactjs-consumer-test-example Setup instructions in readme are just about cloning the repo, installing the dependencies and then running the solitary spec file. Any insight will be helpful. TIA 🙏
👋 1
m

Matt (pactflow.io / pact-js / pact-go)

05/09/2022, 3:32 AM
Thanks for taking the time to create an example - very helpful!
👍 1
I quickly had a look earlier, one thing I did note is that there is an invalid peer dependency (you have a later version of Jest). I don’t think that’s the issue, but worth noting
Copy code
➜  pactjs-consumer-test-example git:(master) ✗ cat log/pact.log                                                                                                                                                                                                                                             <aws:pact-prod>
I, [2022-05-09T14:39:39.419188 #78983]  INFO -- : Registered expected interaction POST /graphql
I, [2022-05-09T14:39:39.430839 #78983]  INFO -- : Received request POST /graphql
I, [2022-05-09T14:39:39.431203 #78983]  INFO -- : Found matching response for POST /graphql
I, [2022-05-09T14:39:39.439586 #78983]  INFO -- : Verifying - interactions matched
I, [2022-05-09T14:39:39.442503 #78983]  INFO -- : Cleared interactions
I, [2022-05-09T14:39:39.448380 #78983]  INFO -- : Registered expected interaction POST /graphql
W, [2022-05-09T14:39:39.457326 #78983]  WARN -- : Verifying - actual interactions do not match expected interactions.
Missing requests:
	POST /graphql



W, [2022-05-09T14:39:39.457368 #78983]  WARN -- : Missing requests:
	POST /graphql



I, [2022-05-09T14:39:39.486089 #78983]  INFO -- : Cleared interactions
I, [2022-05-09T14:39:39.497316 #78983]  INFO -- : Updating pact for App API at /private/tmp/pactjs-consumer-test-example/contract-tests/pacts/app_ui-app_api.json
I, [2022-05-09T14:39:39.498221 #78983]  INFO -- : *****************************************************************************
I, [2022-05-09T14:39:39.498300 #78983]  INFO -- : Updating existing file ./private/tmp/pactjs-consumer-test-example/contract-tests/pacts/app_ui-app_api.json as pactfile_write_mode is :update
I, [2022-05-09T14:39:39.498334 #78983]  INFO -- : Only interactions defined in this test run will be updated.
I, [2022-05-09T14:39:39.498351 #78983]  INFO -- : As interactions are identified by description and provider state, pleased note that if either of these have changed, the old interactions won't be removed from the pact file until the specs are next run with :pactfile_write_mode => :overwrite.
I, [2022-05-09T14:39:39.498368 #78983]  INFO -- : *****************************************************************************
That’s the pact log. It seems to show that it’s not receiving the second API client call. Which is strange, because the code looks good and obviously you can run the tests if you run only a single request
that led me to be suspicious of your API client
…which I then found had a cache on it:
Copy code
module.exports = new ApolloClient({
  cache: new InMemoryCache({}),
  link: createHttpLink({
    fetch: require("node-fetch"),
    uri: "<http://localhost:1234/graphql>",
  }),
});
So, unsurprisingly, the client wasn’t making any new requests because it had already made them and presumably because there were no headers on the previous call stating the contents weren’t cacheable, it went ahead and cached them
There are probably better ways to do this, but I made the tests pass by setting some defaults. For unit tests, you could probably override the client defaults to enable this behaviour
Copy code
module.exports = new ApolloClient({
  cache: new InMemoryCache({}),
  link: createHttpLink({
    fetch: require("node-fetch"),
    uri: "<http://localhost:1234/graphql>",
  }),
  defaultOptions: {
    watchQuery: {
      fetchPolicy: "no-cache",
      errorPolicy: "ignore",
    },
    query: {
      fetchPolicy: "no-cache",
      errorPolicy: "all",
    },
  },
});
a

Akash Srivastava

05/09/2022, 6:41 AM
Thanks a lot for the quick help Matt. This was the exact problem. We totally missed this while debugging. Thanks again 🤗
👍 1