Search code examples
unit-testingpact

Do we need assertions in addition to the provider interaction declaration with Pact?


I'm working on pact and I'm often see example that do assertions on request's response along the contract testing part like

test('Check endpoint for post questions on success', async () => {
  await provider.addInteraction(…);

  const response = await createQuestion(…);

  expect(response.status).toBe(201); // <------------
});

Question

Is there added value, to contract testing, to add such assertions on the request's response? Or declaring the interaction embed the whole value of contract testing.


Solution

  • In short, no.

    I wouldn’t do that assertion personally, as all that that is just checking is that Pact did what you asked it to do - i.e. testing the mock.

    Think of a Pact test as a unit test of your API client, that just so happens to also produce a contract.

    Therefore, it’s much more important to check the behaviour of your API client being tested, and Pact will actually check the real response of the provider later on.

    In your case, I'd expect createQuestion to return some useful data (or potentially a Question domain model), so you should check that object is correct. e.g.

    const question = await createQuestion(...);
    expect(question.id).toBeDefined();