Skip to content
Anudeep edited this page Feb 15, 2020 · 22 revisions

pactum

Build

pactum is a REST API Testing Tool that combines the implementation of consumer driven contract library Pact for JavaScript.

Documentation

Installation

npm install --save-dev pactum

Usage

Component Testing

Running a single component test expectation.

const pactum = require('pactum');

it('should be a teapot', async () => {
  await pactum
    .get('http://httpbin.org/status/418')
    .expectStatus(418)
    .toss();
});
# mocha is a test framework
mocha /path/to/test

Learn more about pactum as a component tester here

Contract Testing

Running a contract test with the help of a mock server & a single pact interaction. If the pact interaction is not exercised, the test will fail.

const pactum = require('pactum');

before(async () => {
  await pactum.mock.start();
});

it('GET - one interaction', async () => {
  await pactum
    .addPactInteraction({
      provider: 'projects-service',
      state: 'when there is a project with id 1',
      uponReceiving: 'a request for project 1',
      withRequest: {
        method: 'GET',
        path: '/api/projects/1'
      },
      willRespondWith: {
        status: 200,
        headers: {
          'content-type': 'application/json'
        },
        body: {
          id: 1,
          name: 'fake'
        }
      }
    })
    .get('http://localhost:9393/api/projects/1')
    .expectStatus(200)
    .expectJsonLike({
      id: 1,
      name: 'fake'
    })
    .toss();
});

after(async () => {
  await pactum.mock.stop();
});

Learn more about pactum as a contract tester here

Mock Server

Running pactum as a standalone mock server.

const pactum = require('pactum');
pactum.mock.setDefaultPort(3000);
pactum.mock.start();

Learn more about pactum as a mock server here


Component Testing