Skip to content

tonyjoanes/contract-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Contract Testing Demo with Docker, Pact, 3 C# Backends, and React TypeScript Frontend

This project demonstrates contract testing using Pact with a complete microservices architecture featuring multiple APIs:

  • Frontend: React TypeScript application (Consumer) - βœ… READY
  • Backend API 1: User API - ASP.NET Core Web API (Provider) - βœ… READY
  • Backend API 2: Product API - ASP.NET Core Web API (Provider) - βœ… READY
  • Backend API 3: Order API - ASP.NET Core Web API (Provider) - βœ… READY
  • Pact Broker: Contract sharing and verification status - βœ… READY
  • Docker: Full containerization of all services - βœ… READY
  • Provider States: Implemented in User API and Product API - βœ… READY

🎯 Multiple API Demo

Run the complete multiple API demo:

# Start all services
docker-compose up --build -d

# Run the comprehensive demo
scripts/demo-multiple-apis.bat

This showcases how one frontend can have contracts with multiple backend services, demonstrating:

  • πŸͺ Single Consumer (React Frontend) with Multiple Providers (User API + Product API)
  • πŸ”„ Independent API Development - Teams can work on different APIs simultaneously
  • ⚑ Fast Integration Feedback - Catch breaking changes early
  • πŸ“‹ Living Documentation - Contracts serve as up-to-date API specs
  • πŸ›‘οΈ Safe API Evolution - Deploy with confidence knowing contracts are verified

Note: Pact verification tests are currently being updated to work with PactNet 5.0.0 API changes. The frontend consumer tests and provider state endpoints are fully implemented.

πŸ—οΈ Project Structure

contract-testing/
β”œβ”€β”€ frontend/                          # React TypeScript app
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ services/ApiService.ts    # API client layer
β”‚   β”‚   β”œβ”€β”€ __tests__/pact/           # Pact consumer tests
β”‚   β”‚   β”œβ”€β”€ App.tsx                   # Main application
β”‚   β”‚   └── index.tsx                 # Entry point
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── nginx.conf
β”œβ”€β”€ backend-api-1/                     # User API (with Provider States)
β”‚   β”œβ”€β”€ Program.cs                    # API implementation
β”‚   β”œβ”€β”€ backend-api-1.csproj
β”‚   └── Dockerfile
β”œβ”€β”€ backend-api-2/                     # Product API
β”‚   β”œβ”€β”€ Program.cs
β”‚   β”œβ”€β”€ backend-api-2.csproj
β”‚   └── Dockerfile
β”œβ”€β”€ backend-api-3/                     # Order API
β”‚   β”œβ”€β”€ Program.cs
β”‚   β”œβ”€β”€ backend-api-3.csproj
β”‚   └── Dockerfile
β”œβ”€β”€ backend-api-1.tests/              # Pact verification tests (WIP)
β”œβ”€β”€ scripts/                          # Helper scripts
β”œβ”€β”€ docker-compose.yml                # Container orchestration
└── README.md

πŸš€ Quick Start

Prerequisites

  • Docker and Docker Compose
  • Node.js 18+ (for local frontend development)
  • .NET 8 SDK (for local backend development)

1. Clone and Start All Services

# Clone the repository
git clone <repository-url>
cd contract-testing

# Start all services with Docker Compose
docker-compose up --build

2. Access the Applications

Once all containers are running:

3. Explore the APIs

Each backend API provides Swagger documentation:

πŸ§ͺ Contract Testing Workflow

Phase 1: Consumer Tests (Frontend)

The frontend generates contracts by writing Pact consumer tests:

cd frontend
npm install
npm run test:pact

This creates contract files in the pacts/ directory.

Phase 1.5: Publish Pacts to Broker

After running consumer tests, publish the pacts to the broker:

# From project root
scripts/publish-pacts.bat

# Or manually:
cd frontend
docker run --rm -v %CD%/pacts:/pacts pactfoundation/pact-cli:latest publish /pacts --consumer-app-version=1.0.0 --broker-base-url=http://host.docker.internal:9292

Phase 2: Provider Verification (Backends)

Provider verification tests are being updated to work with PactNet 5.0.0. The backend APIs implement provider states for proper contract testing.

Phase 3: Provider States

Our implementation includes provider states to ensure consistent test data:

  • "users exist" - Sets up sample users for testing
  • "user with id 1 exists" - Ensures specific user exists
  • "no users exist" - Clears all users for creation tests

See docs/provider-states-explained.md for detailed explanation.

Phase 4: View Results

Visit the Pact Broker at http://localhost:9292 to see:

  • All contracts between consumer and providers
  • Verification status
  • Contract evolution over time

πŸ“Š API Endpoints

User API (Port 4001) - βœ… WORKING

  • GET /api/users - Get all users
  • GET /api/users/{id} - Get user by ID
  • POST /api/users - Create new user
  • GET /health - Health check
  • Provider States:
    • POST /provider-states - Set up test state
    • DELETE /provider-states - Tear down test state

Product API (Port 4002) - βœ… WORKING

  • GET /api/products - Get all products
  • GET /api/products/{id} - Get product by ID
  • POST /api/products - Create new product
  • GET /health - Health check

Order API (Port 4003) - βœ… WORKING

  • GET /api/orders - Get all orders
  • GET /api/orders/{id} - Get order by ID
  • POST /api/orders - Create new order
  • GET /health - Health check

πŸ”§ Local Development

Testing Individual APIs

All APIs build and run successfully:

# User API
cd backend-api-1
dotnet run          # Starts on http://localhost:5000

# Product API
cd backend-api-2
dotnet run          # Starts on http://localhost:5000

# Order API
cd backend-api-3
dotnet run          # Starts on http://localhost:5000

Frontend Development

cd frontend
npm install
npm start           # Start development server
npm run test:pact   # Run Pact consumer tests
npm run build       # Build for production

Docker Development

# Build individual services
docker-compose build backend-api-1
docker-compose build backend-api-2
docker-compose build backend-api-3
docker-compose build frontend

# Start all services
docker-compose up --build

Environment Variables

The application uses these environment variables:

Frontend:

Backends:

  • PACT_BROKER_BASE_URL - Pact Broker URL for verification
  • ASPNETCORE_ENVIRONMENT - ASP.NET Core environment

πŸ“‹ Technology Stack

Component Technology Status
Frontend React + TypeScript βœ… Working
Backend APIs ASP.NET Core 8 βœ… Working
Contract Testing Pact (@pact-foundation/pact, PactNet) 🟑 Partial
Containerization Docker + Docker Compose βœ… Working
API Documentation Swagger/OpenAPI βœ… Working
Broker Pact Broker βœ… Working
Provider States User API Implementation βœ… Working

πŸ§ͺ Testing Strategy

βœ… What's Working

  1. All Backend APIs: Build successfully and run in Docker
  2. Frontend: React app with API integration (builds and runs successfully)
  3. Provider States: Implemented in User API for consistent testing
  4. Docker Orchestration: All services containerized and working
  5. API Documentation: Swagger UI available for all backends
  6. Pact Consumer Tests: All tests passing for User API and Product API
  7. Pact Broker: Running and accessible, multiple contracts published successfully
  8. Contract Publishing: Automated scripts for publishing pacts to broker
  9. Multiple API Demo: Complete workflow demonstrating frontend contracts with multiple providers

🟑 Work in Progress

  1. Pact Verification Tests: Being updated for PactNet 5.0.0 compatibility

πŸ”§ Quick Test Commands

# Test API builds
cd backend-api-1 && dotnet build  # βœ… Works
cd backend-api-2 && dotnet build  # βœ… Works  
cd backend-api-3 && dotnet build  # βœ… Works

# Test Docker builds
docker-compose build backend-api-1  # βœ… Works
docker-compose build backend-api-2  # βœ… Works
docker-compose build backend-api-3  # βœ… Works

# Start all services
docker-compose up --build  # βœ… Works

# Run contract testing workflow
scripts/run-contract-tests.bat  # βœ… Runs consumer tests + publishes pacts

# Run comprehensive multiple API demo
scripts/demo-multiple-apis.bat  # βœ… Full demo with User + Product APIs

# Just publish pacts (after running consumer tests)
scripts/publish-pacts.bat  # βœ… Publishes to broker

🚨 Known Issues

  1. PactNet Version: Upgrading from 4.6.3 to 5.0.0 requires API changes in verification tests
  2. Test Project Structure: Moved out of main API directories to prevent build conflicts

πŸ”— Next Steps

To complete this demo:

  1. Fix PactNet 5.0.0 compatibility in verification tests
  2. Add provider states to Product API and Order API
  3. Implement complete consumer tests for all three APIs
  4. Add automated testing scripts

🎯 Key Benefits Demonstrated

  1. Independent Development: Teams can develop and test services independently
  2. Contract Evolution: Safe API changes with contract compatibility checking
  3. Fast Feedback: Catch integration issues early without full integration tests
  4. Documentation: Contracts serve as living documentation
  5. Confidence: Deploy with confidence knowing contracts are verified

πŸ“š Learn More

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all APIs build successfully
  5. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

πŸ§ͺ Trust but verify β€” contract testing made practical.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published