liveBook · Manning

Jest

As explained in Contract Testing in Action

Jest is a JavaScript testing framework that provides test structure, assertions, and test execution utilities.

When a web consumer depends on a Movies API, Jest provides the test structure and execution utilities for checking that the expected HTTP interaction works. It organizes scenarios with describe blocks and defines individual checks with test blocks. section 4.3.4

A Jest-based consumer contract test typically:

  • Groups the API and scenario with nested describe blocks. section 4.3.4
  • Registers a Pact interaction for a GET request to /movies and specifies the expected response. section 4.3.4
  • Runs the consumer against the mock provider and asserts that the returned movie matches the expected body. section 4.3.4
  • Runs the focused test through an npm script such as npm run test:web:consumer. section 4.3.4

Testing the Movies API contract

A test describes a GET request to /movies, configures Pact to return a 200 response containing movie objects, calls fetchMovies with the mock provider URL, and checks the first result with expect(movies[0]).toEqual(EXPECTED_BODY). The generated pact records the consumer, request, response, and matching rules. section 4.3.4

const path = require('path');
const { fetchMovies } = require('./consumer');
const { PactV3, MatchersV3 } = require('@pact-foundation/pact');

const provider = new PactV3({
  dir: path.resolve(process.cwd(), 'pacts'),
  consumer: 'WebConsumer',
  provider: 'MoviesAPI',
});
const EXPECTED_BODY = { id: 1, name: "My movie", year: 1999 }

describe('Movies Service', () => {
  describe('When a GET request is made to /movies', () => {
    test('it should return all movies', async () => {
      provider
        .uponReceiving('a request to all movies')
        .withRequest({
          method: 'GET',
          path: '/movies',
        })
        .willRespondWith({
          status: 200,
          body: MatchersV3.eachLike(EXPECTED_BODY),
        });

      await provider.executeTest(async mockProvider => {
        const movies = await fetchMovies(mockProvider.url);
        expect(movies[0]).toEqual(EXPECTED_BODY);
      })
    });
  });
});

The complete listing shows how Jest, PactV3, the mock provider, the consumer call, and the assertion fit together. section 4.3.4

How Contract Testing in Action covers it

How deep it goes
Explains describe, test, asynchronous execution, and assertions through a complete Pact example, without detailing Jest internals.
Examples
Uses nested Movies API scenarios, a GET /movies interaction, a movie response body, generated pact JSON, and an npm test script.
What it assumes
Assumes JavaScript project dependencies and introduces Jest usage alongside Node.js, npm, and Pact setup.
What you can do after
Apply it via Jest to write, run, and assert consumer contract tests against Pact mock providers.
How it connects
Ties Jest to Pact, consumer-driven contract testing, HTTP APIs, event-driven architecture, and asynchronous testing.

Also explained in

Compare how 5 books explain Jest →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Contract Testing in Action. The section links point to the parts of the book it draws on.