Hi there, I'm trying to run a test scenario where ...
# pact-js
l
Hi there, I'm trying to run a test scenario where a parameter is passed in an invalid format (and it should throw a 400), but it fails with the error message
with 30000 ms timeout for Pact
. I've tried to increase the timeout (quite significantly but it still fails with the same message). Is there any way to check why it times out? It seems due to a promise that doesn't resolve... Also it seems to be specifically when looking to get the provider to respond with status 400 ๐Ÿค” Code snippet in ๐Ÿงต
Copy code
it('should return an error when an invalid UPRN is passed', async () => {
            await provider.addInteraction({
                uponReceiving: 'an invalid UPRN',
                withRequest: {
                    method: 'GET',
                    path: '/property/m1xf%d5/sales'
                },
                willRespondWith: {
                    status: 400,
                    headers: default_headers,
                    body: {
                        error: 'BadRequest',
                        message: 'Invalid UPRN format',
                        statusCode: 400
                    }
                }
            });

            process.env.API_PROPERTY_TRANSACTIONS_URL =
                provider.mockService.baseUrl;

            const response = await api().transactionalHistoryByUprn('m1xf%d5');
            expect(response).toThrow('400: Bad Request');
        });
When running the test:
m
Copy code
const response = await api().transactionalHistoryByUprn('m1xf%d5'); <- throws
expect(response).toThrow('400: Bad Request'); <- to late, already thrown!
You can actually see the error in your terminal
You might need to wrap it like this:
Copy code
expect(() => {
  throw Error();
}).toThrow();
Copy code
expect(() => api().transactionalHistoryByUprn('m1xf%d5')).toThrow('400: Bad Request');
I could be wrong about the jest syntax, so best to check the
toThrow
docs. You might be able to just hand it a promise. But you are
await
ing the promise, so that will always throw before the
expect
y
https://jestjs.io/docs/asynchronous await expect(fetchData()).rejects.toMatch('error');
l
Thanks Matt, I wrapped it as suggested and it worked ๐Ÿ™‚
๐Ÿ™Œ 1
m
Iโ€™ve just added an example to the JS troubleshooting guide for others (this exact problem came up in hte very next post ๐Ÿ˜† https://github.com/pact-foundation/pact-js/blob/master/docs/troubleshooting.md#how-do-i-test-negative-scenarios-such-as-400