In our JS consumer we add a timeout in our http re...
# pact-js
a
In our JS consumer we add a timeout in our http request, is there a way to include this in a pact test? If I leave the timeout the pact test fails because of an "After each hook" error. And if it is commented out the test passes
y
Can you share some example code.
In our JS consumer we add a timeout in our http request
What do you mean exactly by this? HTTP clients tend to have a default timeout value, do you mean you have modified this to be something different than the default? Test frameworks normally have the ability to set timeouts, which test framework are you referring to?
a
Sure. The code below is the method that sends the HTTP request. This method is being called in the pact test to get the actual repsonse.
Copy code
const response = await fetchRequest(
      this.resolveUri(config.SSO_TOKEN_PATH),
      {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        body: createFormBody(reqBody),
        timeout: config.API_DEFAULT_TIMEOUT,
      }
    );
config.API_DEFAULT_TIMEOUT
is set to
60000
. When I leave that timeout in, the pact test doesn't work
Is there a way to add a timeout in the pact test interaction, so that the mock API can expect this timeout
y
pass it in the config object when you instantiate your client, or set a timeout in your unit test framework
a
Turns out I understood this timeout thing wrong, basically the timeout property specifies the time that the request must be completed in before being terminated. I don't know why this would effect the pact test though. When the property is not there the pact test works and passes, and when it is there the pact test fails
y
Turns out I understood this timeout thing wrong, basically the timeout property specifies the time that the request must be completed in before being terminated.
Correct
I don't know why this would effect the pact test though. When the property is not there the pact test works and passes, and when it is there the pact test fails
If you can share your test code, it would help, otherwise its hard to guess
it could be any number of things, is a pact file being generated when the test does pass?
a
Yes
y
Ok well in order to help it would be useful to follow the guide for asking for help https://docs.pact.io/help/how_to_ask_for_help#4-ask-a-good-question • A brief description of what you're trying to achieve, what your expected outcome is, and what the actual behaviour is. • The relevant language and versions of the software you are using. • Details of the environment you're using (Mac/Window/Linux, Docker). • Example code. • Any relevant logs.
If you're trying to demonstrate a problem or diagnose a bug, the best possible thing you can do is provide a minimal reproducible example that doesn't contain any organisational confidential information.
a
Copy code
mockProvider.addInteraction({
        state: "",
        uponReceiving: "",
        withRequest: {
          method: "POST",
          path: "/token",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
          },
          body: request,
        },
        willRespondWith: {
          status: 200,
          headers: {
            "Content-Type": "application/json; charset=UTF-8",
          },
          body: like(response),
        },
      });

      const expectedToken= jwtBody

      const actualToken = await retrieveToken(session);

      // Assert - did we get the expected response
      assert.deepStrictEqual(
        actualIdToken,
        expectedIdToken
      );
This is the code, above the interaction declaration is just variables being added
y
I can't run that and reproduce the problem, and it doesn't contain the full test, I don't know what version of pact-js you are using
a
I've looked at the logs, the request is matched, and the response is sent back, but when i console log the response it's null
y
I can't see how your client is being instantiated. If you can imagine if you were on the other end buddy trying to help someone out
where are your passing in
config.API_DEFAULT_TIMEOUT
a
So the method
retrieveToken
imports the config file and makes the HTTP request from there, the one I sent earlier
t
It’s not possible to reproduce this from the examples you’ve sent above.
I’ve looked at the logs, the request is matched, and the response is sent back, but when i console log the response it’s null
If this is the case, then I expect the bug is in the calling code, not pact.
To reproduce the problem, we’d need the implementation of
fetchRequest
(and several other functions and variables). If you can’t compile the snippets on their own, neither can we.
If I leave the timeout the pact test fails because of an “After each hook” error.
What is the error? This doesn’t sound like it is coming from pact. What after each hooks are you using?
Can you tell us what versions of pact dependencies you are using?
a
pact version:
"@pact-foundation/pact": "^10.1.1"
After each error message:
Copy code
2) Pact Tests
       "after each" hook for "user is authenticated, and sends a valid body":
     Error: Error in native callback
      at Object.mockServerMismatches (node_modules\@pact-foundation\pact-core\src\consumer\index.ts:94:13)
      at Pact.verify (node_modules\@pact-foundation\src\httpPact\index.ts:202:39)
      at Context.<anonymous> (tests\contract\contractTesting.spec.js:20:32)
      at processImmediate (node:internal/timers:466:21)
I can't share my code, as it's company code, I've shared as much as I can. If you cannot help me, then I understand and thank you for your help
t
Well, that definitely looks like a pact problem 🤔
a
This has been solved, it wasn't a pact issue
Thanks for your help anyways
y
Cheers for coming back to us chap, glad you got sorted
party parrot 1