Renan Santos
12/04/2023, 3:51 PMdotenv.config({ path: './.env.test' });
const port = getAvailablePort();
const API_BASE_URL = `${process.env.BASE_URL_AXIOS_REQUEST}:${port}`;
const ACCEPT_HEADER = { 'Content-Type': 'application/json' };
const pact = new PactV3({
consumer: 'KK-CHECKOUT-SERVICE',
provider: 'KK-PAYMENT-SERVICE',
port: port,
dir: path.resolve(process.cwd(), String(process.env.PACTS_PATH)),
logLevel: 'error'
});
const mockData = {
paymentMethodDtoMock: applyMatchersToDto(paymentMethodDtoMock)
};
describe('KK-CHECKOUT-SERVICE consuming from KK-PAYMENT-SERVICE', () => {
const PAYMENT_METHOD_PATH = '/payment-methods/checkout';
describe(`POST - ${PAYMENT_METHOD_PATH}`, () => {
it('Should create a new payment method into checkout', async () => {
pact.uponReceiving(`post - ${PAYMENT_METHOD_PATH}`)
.withRequest({
method: 'POST',
path: PAYMENT_METHOD_PATH,
headers: ACCEPT_HEADER,
body: eligiblePaymentMethodsDtoRequestMock
})
.willRespondWith({
status: 201,
headers: ACCEPT_HEADER,
body: [mockData.paymentMethodDtoMock]
});
return pact.executeTest(async () => {
const response = await <http://axios.post|axios.post>(
`${API_BASE_URL}${PAYMENT_METHOD_PATH}`,
eligiblePaymentMethodsDtoRequestMock,
{
headers: ACCEPT_HEADER
}
);
expect(response.status).toEqual(201);
});
});
});
});
Joshua Ellis
12/04/2023, 10:49 PMwithRequest
and willRespondWith
for the same route. You typically need to have some other differentiator though (e.g., one might be a POST
and another a `GET`; or there may be different provider states, set GET /user/1
might respond with a 200
or a 404
).
This Pact-JS example might be helpful if you haven't seen it:
https://github.com/pact-foundation/pact-js/blob/eb3110c0785f861c9befbcff354886eb37328dc8/examples/e2e/test/consumer.spec.js#L113-L156Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)