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
