JSON Diff in CI/CD Testing
Using JSON comparison in automated tests — contract testing, API snapshot testing, and change detection.
Published:
Tags: JSON diff CI testing, API snapshot testing JSON, contract testing JSON
JSON Diff in CI/CD Testing The JSON data interchange format is defined in RFC 8259 and ECMA-404. Pact contract testing specification is defined at pact.io, and the JSON Patch standard (RFC 6902) defines the format for expressing JSON document changes. CI/CD pipelines using JSON validation catch configuration errors 95% of the time before production deployment, according to DevOps Insights 2024 Breaking API changes cost time — a renamed field in a webhook handler, a missing key in a configuration response, an integer becoming a string. JSON diff in CI catches these before they reach production. --- What is three levels of json comparison in ci? | Approach | What it catches | Tooling | |---------|----------------|---------| | Deep equality | Any difference, including new fields | , | |…
Frequently Asked Questions
How do I use JSON diff in automated tests?
In Jest or Vitest, use expect(actual).toEqual(expected) for deep structural comparison. For richer diff output on failure, use the deep-diff library to compute the changes and include them in the error message. Snapshot testing with toMatchSnapshot() is another approach — it writes the first result and diffs against it on subsequent runs.
What is API contract testing?
API contract testing verifies that a provider API still fulfils the expectations of its consumers. Consumer tests define what response shape they depend on; provider tests verify those shapes are still delivered. Pact is the most popular tool for consumer-driven contract testing with JSON.
How do I snapshot test JSON responses?
On first run, write the API response to a JSON file in a __snapshots__ directory. On subsequent runs, compare the fresh response to the snapshot using a structural diff. If differences exist, the test fails with a report of what changed. Update the snapshot file when you intentionally change the API.
How do I detect breaking API changes?
A breaking change removes or renames a required field, changes a field's type, or narrows an enum's allowed values. Add schema validation (with ajv or JSON Schema) to your CI pipeline to detect these automatically. Non-breaking changes add optional fields or widen types — these pass validation.
What is Pact contract testing?
Pact is a consumer-driven contract testing tool. The consumer writes tests that define what API shape they expect; Pact records these as a 'pact' JSON file. The provider runs this pact against its actual implementation. If the provider breaks the expected shape, the test fails. It's used to catch breaking API changes before deployment.
All articles · theproductguy.in