Hello everyone, Does anyone know if it's possible...
# pact-js
r
Hello everyone, Does anyone know if it's possible for a consumer to publish to the pactBroker with two possibilities for "withRequest" and "willRespondWith" for the same route? The rest of the request structure is the same for both, with the only difference being that there could be two distinct forms for the request body and response body. Below is my consumer-side test script:
Copy code
dotenv.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);
            });
        });
    });
});
j
You certainly can have multiple
withRequest
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-L156
1
👍 1
☝️ 1
m
Exactly. Generally speaking, you would differentiate with the scenario name (description), provider state(s) and response codes/bodies.
When writing Pact tests, it’s good to think of a “given/when/then” / BDD style, as it makes the scenario meanings clearer