liveBook · Manning

Loose matching

As explained in Contract Testing in Action

Loose matching is a comparison technique that permits flexible correspondence between actual and expected interaction data instead of requiring static values to match exactly.

Loose matching verifies the shape of an interaction without requiring every returned value to equal a fixed example. In consumer contract testing, it lowers coupling between a consumer and provider: ordinary data changes can remain compatible, while changes that matter to the consumer still fail the contract. section 4.1.3 section 4.3.4

Exact response values can make a contract test brittle. A provider changing a returned ID, name, or date-like value may break a tightly coupled test even when the consumer can still use the response. Loose matchers instead express what must remain stable, such as a field type, an array structure, or a regular-expression pattern. section 4.1.3 section 3.7.2

Use loose matching to state the consumer’s real requirements:

  • Match a field by type when its particular value is not significant, such as requiring an integer ID and string user fields. section 4.1.3
  • Match array elements with a rule such as eachLike when every returned item must follow the expected object shape. section 4.3.4
  • Use a regular expression when a response needs a permitted format rather than one static value. section 4.1.3
  • Keep a matcher strict where the consumer depends on it: an integer-to-string change in an ID should fail. section 4.3.4

Matching a movies response

A consumer requesting GET /movies expects movie objects with id, name, and year. Pact’s eachLike rule allows the provider to return different movie values while requiring items to match the expected data types. The contract remains tolerant of value changes but rejects an incompatible type, such as a string movie ID. section 4.3.4

const EXPECTED_BODY = {
  id: like(1),
  username: like(‘marie’),
  fullname: like(‘marie cruz’)
}

The listing shows type-oriented matchers for a user response: example values establish the expected field types rather than fixed production values. section 4.1.3

Loose matching is not a reason to make contracts vague. Use it when actual values are incidental to the consumer’s needs, while preserving rules for the response shape and types the consumer relies on. This helps keep isolated consumer contract tests trustworthy rather than flaky. section 4.1 section 4.5

How Contract Testing in Action covers it

How deep it goes
Explains type, array, and regular-expression matchers through a Pact movies test, without detailing matcher internals.
Examples
Uses user fields and a GET /movies response matched with Pact MatchersV3.eachLike.
What it assumes
Assumes Node.js is installed and introduces matching rules within consumer contract tests.
What you can do after
Apply it via Pact matchers to tolerate value changes while failing incompatible response types.
How it connects
Connects consumer-driven contract testing with coupling, flaky tests, mocks, stubs, response schemas, and provider-driven contract testing.

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.