Hone logo
Hone
Problems

Implementing Contract Testing with Jest and Pact

Contract testing ensures that your services interact correctly by verifying that they adhere to agreed-upon contracts. This challenge focuses on setting up and utilizing Jest with Pact to perform contract testing between a consumer (client) and a provider (API). You'll define a Pact contract, write tests for the consumer to verify it against the provider, and then write tests for the provider to ensure it satisfies the contract.

Problem Description

You are tasked with implementing contract testing for a simple scenario: a consumer application (consumer) retrieves user data from a provider API (provider). The consumer expects the provider to return a JSON object with a userId (integer) and a username (string). You need to use Jest and Pact to verify that the consumer and provider agree on this contract.

Specifically, you need to:

  1. Define a Pact Contract: Create a Pact contract that specifies the expected shape of the user data returned by the provider. This contract should include the userId and username fields with their respective data types.
  2. Consumer Verification Tests: Write Jest tests for the consumer that use Pact to verify that the provider fulfills the defined contract. These tests should mock the provider API based on the contract and assert that the consumer correctly handles the expected response.
  3. Provider Verification Tests: Write Jest tests for the provider that use Pact to verify that it satisfies the contract defined by the consumer. These tests should use Pact's provider verification to ensure that the actual API responses match the contract.

Key Requirements:

  • Use Jest as the test runner.
  • Use Pact for contract definition and verification.
  • The consumer should mock the provider API using Pact.
  • The provider should verify its API responses against the Pact contract.
  • The tests should be clear, concise, and well-documented.

Expected Behavior:

  • Consumer tests should pass if the provider fulfills the contract.
  • Consumer tests should fail if the provider deviates from the contract.
  • Provider tests should pass if the API responses match the contract.
  • Provider tests should fail if the API responses deviate from the contract.

Edge Cases to Consider:

  • Empty responses from the provider.
  • Unexpected data types in the provider's response.
  • Missing fields in the provider's response.
  • Ensure the Pact publisher correctly publishes the contract.

Examples

Example 1: Consumer Test (Successful Contract)

Input: Provider returns { userId: 123, username: "testuser" }
Output: Consumer tests pass.
Explanation: The provider fulfills the contract, so the consumer's mock verification passes.

Example 2: Consumer Test (Contract Violation - Missing Field)

Input: Provider returns { userId: 123 }
Output: Consumer tests fail.
Explanation: The provider is missing the 'username' field, violating the contract.

Example 3: Provider Test (Successful Verification)

Input: Provider API returns { userId: 456, username: "anotheruser" }
Output: Provider tests pass.
Explanation: The API response matches the contract, so the provider verification passes.

Constraints

  • You are free to choose the structure of your consumer and provider applications. Assume they are Node.js applications.
  • The contract should be relatively simple, focusing on the core userId and username fields.
  • The consumer and provider should be able to run independently.
  • Assume you have a Pact broker available for publishing and retrieving contracts. You'll need to configure your tests with the broker's URL.
  • The tests should be written in TypeScript.

Notes

  • Start by defining the Pact contract using Pact's DSL.
  • Consider using Pact's matchers to define more complex expectations for the data types.
  • Remember to configure your Pact tests with the correct broker URL and consumer/provider names.
  • Focus on the core concepts of contract testing: defining a contract, verifying the consumer against the contract, and verifying the provider against the contract.
  • You can use pact-provider-verifier for provider verification.
  • You can use pact-consumer-mock-service for consumer mocking.
  • The goal is to demonstrate a working contract testing setup, not to build a fully production-ready system.
Loading editor...
typescript