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




