gitlab-org--gitlab-foss/doc/development/testing_guide/contract/consumer_tests.md

10 KiB

stage group info
none Development To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments

Writing consumer tests

This tutorial guides you through writing a consumer test from scratch. To start, the consumer tests are written using jest-pact that builds on top of pact-js. This tutorial shows you how to write a consumer test for the /discussions.json endpoint, which is actually /:namespace_name/:project_name/-/merge_requests/:id/discussions.json.

Create the skeleton

Start by creating the skeleton of a consumer test. Create a file under spec/contracts/consumer/specs called discussions.spec.js. Then, populate it with the following function and parameters:

The pactWith function

The Pact consumer test is defined through the pactWith function that takes PactOptions and the PactFn.

const { pactWith } = require('jest-pact');

pactWith(PactOptions, PactFn);

The PactOptions parameter

PactOptions with jest-pact introduces additional options that build on top of the ones provided in pact-js. In most cases, you define the consumer, provider, log, and dir options for these tests.

const { pactWith } = require('jest-pact');

pactWith(
  {
    consumer: 'Merge Request Page',
    provider: 'Merge Request Discussions Endpoint',
    log: '../logs/consumer.log',
    dir: '../contracts',
  },
  PactFn
);

The PactFn parameter

The PactFn is where your tests are defined. This is where you set up the mock provider and where you can use the standard Jest methods like Jest.describe, Jest.beforeEach, and Jest.it. For more information, see https://jestjs.io/docs/api.

const { pactWith } = require('jest-pact');

pactWith(
  {
    consumer: 'Merge Request Page',
    provider: 'Merge Request Discussions Endpoint',
    log: '../logs/consumer.log',
    dir: '../contracts',
  },

  (provider) => {
    describe('Discussions Endpoint', () => {
      beforeEach(() => {
        
      });

      it('return a successful body', () => {
        
      });
    });
  },
);

Set up the mock provider

Before you run your test, set up the mock provider that handles the specified requests and returns a specified response. To do that, define the state and the expected request and response in an Interaction.

For this tutorial, define four attributes for the Interaction:

  1. state: A description of what the prerequisite state is before the request is made.
  2. uponReceiving: A description of what kind of request this Interaction is handling.
  3. withRequest: Where you define the request specifications. It contains the request method, path, and any headers, body, or query.
  4. willRespondWith: Where you define the expected response. It contains the response status, headers, and body.

After you define the Interaction, add that interaction to the mock provider by calling addInteraction.

const { pactWith } = require('jest-pact');
const { Matchers } = require('@pact-foundation/pact');

pactWith(
  {
    consumer: 'Merge Request Page',
    provider: 'Merge Request Discussions Endpoint',
    log: '../logs/consumer.log',
    dir: '../contracts',
  },

  (provider) => {
    describe('Discussions Endpoint', () => {
      beforeEach(() => {
        const interaction = {
          state: 'a merge request with discussions exists',
          uponReceiving: 'a request for discussions',
          withRequest: {
            method: 'GET',
            path: '/gitlab-org/gitlab-qa/-/merge_requests/1/discussions.json',
            headers: {
              Accept: '*/*',
            },
          },
          willRespondWith: {
            status: 200,
            headers: {
              'Content-Type': 'application/json; charset=utf-8',
            },
            body: Matchers.eachLike({
              id: Matchers.string('fd73763cbcbf7b29eb8765d969a38f7d735e222a'),
              project_id: Matchers.integer(6954442),
              ...
              resolved: Matchers.boolean(true)
            }),
          },
        };
        provider.addInteraction(interaction);
      });

      it('return a successful body', () => {

      });
    });
  },
);

Response body Matchers

Notice how we use Matchers in the body of the expected response. This allows us to be flexible enough to accept different values but still be strict enough to distinguish between valid and invalid values. We must ensure that we have a tight definition that is neither too strict nor too lax. Read more about the different types of Matchers.

Write the test

After the mock provider is set up, you can write the test. For this test, you make a request and expect a particular response.

First, set up the client that makes the API request. To do that, either create or find an existing file under spec/contracts/consumer/endpoints and add the following API request.

const axios = require('axios');

exports.getDiscussions = (endpoint) => {
  const url = endpoint.url;

  return axios
    .request({
      method: 'GET',
      baseURL: url,
      url: '/gitlab-org/gitlab-qa/-/merge_requests/1/discussions.json',
      headers: { Accept: '*/*' },
    })
    .then((response) => response.data);
};

After that's set up, import it to the test file and call it to make the request. Then, you can make the request and define your expectations.

const { pactWith } = require('jest-pact');
const { Matchers } = require('@pact-foundation/pact');

const { getDiscussions } = require('../endpoints/merge_requests');

pactWith(
  {
    consumer: 'Merge Request Page',
    provider: 'Merge Request Discussions Endpoint',
    log: '../logs/consumer.log',
    dir: '../contracts',
  },

  (provider) => {
    describe('Discussions Endpoint', () => {
      beforeEach(() => {
        const interaction = {
          state: 'a merge request with discussions exists',
          uponReceiving: 'a request for discussions',
          withRequest: {
            method: 'GET',
            path: '/gitlab-org/gitlab-qa/-/merge_requests/1/discussions.json',
            headers: {
              Accept: '*/*',
            },
          },
          willRespondWith: {
            status: 200,
            headers: {
              'Content-Type': 'application/json; charset=utf-8',
            },
            body: Matchers.eachLike({
              id: Matchers.string('fd73763cbcbf7b29eb8765d969a38f7d735e222a'),
              project_id: Matchers.integer(6954442),
              ...
              resolved: Matchers.boolean(true)
            }),
          },
        };
      });

      it('return a successful body', () => {
        return getDiscussions({
          url: provider.mockService.baseUrl,
        }).then((discussions) => {
          expect(discussions).toEqual(Matchers.eachLike({
            id: 'fd73763cbcbf7b29eb8765d969a38f7d735e222a',
            project_id: 6954442,
            ...
            resolved: true
          }));
        });
      });
    });
  },
);

There we have it! The consumer test is now set up. You can now try running this test.

Improve test readability

As you may have noticed, the request and response definitions can get large. This results in the test being difficult to read, with a lot of scrolling to find what you want. You can make the test easier to read by extracting these out to a fixture.

Create a file under spec/contracts/consumer/fixtures called discussions.fixture.js. You place the request and response definitions here.

const { Matchers } = require('@pact-foundation/pact');

const body = Matchers.eachLike({
  id: Matchers.string('fd73763cbcbf7b29eb8765d969a38f7d735e222a'),
  project_id: Matchers.integer(6954442),
  ...
  resolved: Matchers.boolean(true)
});

const Discussions = {
  body: Matchers.extractPayload(body),

  success: {
    status: 200,
    headers: {
      'Content-Type': 'application/json; charset=utf-8',
    },
    body: body,
  },

  request: {
    uponReceiving: 'a request for discussions',
    withRequest: {
      method: 'GET',
      path: '/gitlab-org/gitlab-qa/-/merge_requests/1/discussions.json',
      headers: {
        Accept: '*/*',
      },
    },
  },
};

exports.Discussions = Discussions;

With all of that moved to the fixture, you can simplify the test to the following:

const { pactWith } = require('jest-pact');

const { Discussions } = require('../fixtures/discussions.fixture');
const { getDiscussions } = require('../endpoints/merge_requests');

pactWith(
  {
    consumer: 'Merge Request Page',
    provider: 'Merge Request Discussions Endpoint',
    log: '../logs/consumer.log',
    dir: '../contracts',
  },

  (provider) => {
    describe('Discussions Endpoint', () => {
      beforeEach(() => {
        const interaction = {
          state: 'a merge request with discussions exists',
          ...Discussions.request,
          willRespondWith: Discussions.success,
        };
        return provider.addInteraction(interaction);
      });

      it('return a successful body', () => {
        return getDiscussions({
          url: provider.mockService.baseUrl,
        }).then((discussions) => {
          expect(discussions).toEqual(Discussions.body);
        });
      });
    });
  },
);