Alan Alie
05/13/2022, 3:30 PM// 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
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:
// 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.Yousaf Nabi (pactflow.io)
Alan Alie
05/13/2022, 3:46 PMYousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Alan Alie
05/13/2022, 3:50 PMAlan Alie
05/13/2022, 3:51 PMAlan Alie
05/13/2022, 3:52 PMYousaf Nabi (pactflow.io)
Alan Alie
05/13/2022, 3:53 PMYousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Yeh, I never got that to workThat is mocha syntax, jest will be diff, unless you are just natively handling it with a try/catch
Matt (pactflow.io / pact-js / pact-go)
Alan Alie
05/16/2022, 8:44 AMAlan Alie
05/16/2022, 8:46 AMtry {
basketObj = await getData(provider.mockService.baseUrl + '/basket/' + BAD_BASKET_ID);
} catch (error) {
expect(error).toEqual({
errorCode: 'BS-002',
errorMessage: 'No basket has been found',
});
}
Matt (pactflow.io / pact-js / pact-go)