Hi all, I’ve been using typescript to write ‘happy...
# pact-js
a
Hi all, I’ve been using typescript to write ‘happy path’ PACT tests without any problem using a pattern like this:
Copy code
// PACT test for getBasketById
describe('Given we are requesting specific basket', () => {
  test('When a basket exists', async () => {
    // set up Pact interaction
    await provider.addInteraction({
      state: 'When the basket exists',
      uponReceiving: 'get the specified basket',
      withRequest: interactionRequest.GET_BASKET,
      willRespondWith: interactionResponse.BASKET_NORMAL_RESPONSE,
    });

    // make request to PACT mock server
    await getData(provider.mockService.baseUrl + '/basket/' + OK_BASKET_ID);
  });
Now that I’m attempting error path tests (the response status is 404) I get the following error
Copy code
thrown: Object {
   "errorCode": "BS-002",
   "errorMessage": "No basket has been found",
  }

    111 |   // Failed PACT test for getBasketById
    112 |   describe('Given we are requesting specific basket', () => {
  > 113 |     test('When the basket does not exists', async () => {
        |     ^
    114 |       // set up Pact interaction
    115 |       await provider.addInteraction({
    116 |         state: 'basket does not exists',
My code for the ‘error path’ test is as follows:
Copy code
// Failed PACT test for getBasketById
describe('Given we are requesting specific basket', () => {
  test('When the basket does not exists', async () => {
    // set up Pact interaction
    await provider.addInteraction({
      state: 'basket does not exists',
      uponReceiving: 'get specified basket',
      withRequest: {
        method: HTTPMethod.GET,
        path: '/basket/' + BAD_BASKET_ID,
      },
      willRespondWith: {
        status: 404,
        headers: { 'Content-Type': 'application/json' },
        body: {
          errorCode: 'BS-002',
          errorMessage: 'No basket has been found',
        },
      },
    });

    // make request to Pact mock server
    await getData(provider.mockService.baseUrl + '/basket/' + BAD_BASKET_ID);
  });
});
How do I handle the thrown object? PS: If I change the response status in my code to 200 then the problem disappears - but that’s obviously not what I’m after.
y
are you using axios for a client?
a
No. I'm using Fetch
y
what happens in your client code when you get a non 2xx response
does it throw?
there is regular handling in most frameworks for handling catchs, to expect it to reject and then do your assertions
or you catch it yourself when invoking the system under test, and then assert
☝️ 1
a
Thanks
This looks like something I haven't tried 👍🏽
👍 1
I'll take a look at it on Monday 😁
y
a
Yeh, I never got that to work 😕
y
hmm, love for you to elaborate when you have time. I have ran that workshop so many times, and do have some niceties that I want to address and get some tests into the repo but it definitely works, possibly not quite OOB because of all things dependencies I believe it was refreshed as part of the CI/CD workshop which I think is maybe a more reliable path at the moment, the workshops are on my growing list of things to look at
👍🏽 1
Yeh, I never got that to work
That is mocha syntax, jest will be diff, unless you are just natively handling it with a try/catch
m
The issue is that http clients tend to throw when they get error status codes. Try/catch or using the JS test framework specific error handing (as above). This isn't a pact specific problem, so hopefully should be quite googleable
a
Hi @Yousaf Nabi (pactflow.io) /@Matt (pactflow.io / pact-js / pact-go), thanks for your help.....again 😁
You were correct in identifying that the test framework itself was was causing my problem and throwing the response object. In the end I resolved the issue by wrapping the getData() call in a try/catch block and checked the thrown object matched the expected response.
Copy code
try {
   basketObj = await getData(provider.mockService.baseUrl + '/basket/' + BAD_BASKET_ID);
} catch (error) {
   expect(error).toEqual({
          errorCode: 'BS-002',
          errorMessage: 'No basket has been found',
        });
}
👍 1
m
👍