This PR implements comprehensive contract testing for all external service integrations in StrellerMinds-Backend using Pact. This ensures API compatibility and early detection of breaking changes across our microservices architecture.
-
Stellar API Integration (
stellar.consumer.pact.test.ts)- Account details fetching
- Transaction monitoring
- Error handling scenarios
- Provider:
Stellar-Horizon-API
-
Email Service Integration (
email.consumer.pact.test.ts)- Single email sending
- Bulk email sending
- Email delivery failures
- Provider:
EmailService
-
Cloud Storage Integration (
storage.consumer.pact.test.ts)- File uploads (images, videos)
- File deletion
- AWS S3 integration
- Cloudinary transformations
- Provider:
CloudStorageService
-
Payment Gateway Integration (
payment.consumer.pact.test.ts)- Customer creation
- Payment intent creation and confirmation
- Webhook signature verification
- Error handling for invalid payments
- Provider:
Stripe-API
-
Notification Service Integration (
notification.consumer.pact.test.ts)- Firebase Cloud Messaging (FCM) push notifications
- SMS notifications via Twilio
- Batch notification sending
- Error handling for invalid tokens/numbers
- Provider:
Firebase-Cloud-Messaging
- Provider Verification Tests (
provider-verification.test.ts)- Verifies StrellerMinds-Backend implements all expected contracts
- Automated compatibility checks
- Deployment safety validation
jest.contract.config.js- Dedicated configuration for contract testsapps/backend/tests/contract/setup.ts- Test setup and teardownapps/backend/tests/contract/global-setup.ts- Global test setupapps/backend/tests/contract/global-teardown.ts- Global test cleanup
.pactrc- Pact CLI configuration file- Environment variable templates for Pact Broker integration
scripts/pact-setup.js- Pact environment initialization and managementscripts/pact-ci-integration.sh- CI/CD pipeline integrationscripts/test-contract-pipeline.js- Pipeline testing and validation
{
"test:contract": "jest --testPathPattern=apps/backend/tests/contract/ --config jest.contract.config.js",
"test:contract:watch": "jest --testPathPattern=apps/backend/tests/contract/ --config jest.contract.config.js --watch",
"test:contract:coverage": "jest --testPathPattern=apps/backend/tests/contract/ --config jest.contract.config.js --coverage",
"test:contract:provider": "jest apps/backend/tests/contract/provider-verification.test.ts --config jest.contract.config.js",
"pact:setup": "node scripts/pact-setup.js init",
"pact:cleanup": "node scripts/pact-setup.js cleanup",
"pact:validate": "node scripts/pact-setup.js validate",
"pact:publish": "pact-broker publish ./pacts --consumer-app-version=$npm_package_version --broker-base-url=$PACT_BROKER_URL --broker-token=$PACT_BROKER_TOKEN",
"pact:verify": "pact-broker verify --provider-base-url=http://localhost:3000 --provider-app-version=$npm_package_version --broker-base-url=$PACT_BROKER_URL --broker-token=$PACT_BROKER_TOKEN",
"pact:can-i-deploy": "pact-broker can-i-deploy --pacticipant StrellerMinds-Backend --version $npm_package_version --to-environment production --broker-base-url=$PACT_BROKER_URL --broker-token=$PACT_BROKER_TOKEN",
"pact:consumer": "./scripts/pact-ci-integration.sh consumer",
"pact:provider": "./scripts/pact-ci-integration.sh provider",
"pact:verify-deployment": "./scripts/pact-ci-integration.sh verify",
"pact:full": "./scripts/pact-ci-integration.sh full",
"pact:test-pipeline": "node scripts/test-contract-pipeline.js"
}- Consumer Tests: Run contract tests and publish to broker
- Provider Verification: Verify provider implementation against contracts
- Deployment Safety: Check compatibility before production deployment
- Complete Pipeline: End-to-end contract testing workflow
The implementation includes scripts that integrate seamlessly with GitHub Actions:
- Contract test execution in build phase
- Automatic contract publishing to Pact Broker
- Provider verification in staging environment
- Pre-deployment compatibility checks
- Updated:
docs/PACT_CONTRACT_TESTING_GUIDE.md- Comprehensive setup and usage guide - New:
CONTRACT_TESTING_IMPLEMENTATION_SUMMARY.md- Implementation overview and next steps
Each contract test suite includes:
- ✅ Success Scenarios: Normal operation flows with proper response validation
- ❌ Error Scenarios: Invalid inputs, service unavailability, authentication failures
- 🔄 Edge Cases: Boundary conditions and unusual but valid inputs
- 📊 Response Validation: Complete request/response contract definitions
- Ensures external service integrations remain compatible
- Catches breaking changes before production deployment
- Validates both success and error response formats
- Identifies integration issues during development
- Prevents production failures due to API changes
- Reduces debugging time for integration problems
- Contracts define expected behavior from consumer perspective
- Forces clear API specifications
- Enables independent service development
- CI/CD pipeline automatically verifies contract compliance
- No manual testing required for contract validation
- Immediate feedback on compatibility issues
- Prevents deployment of incompatible versions
- Environment-specific compatibility checks
- Rollback triggers for contract failures
- One Test Per Interaction: Each test focuses on a single API interaction
- Descriptive Names: Clear, descriptive test names and interaction descriptions
- State Management: Proper provider states for test setup
- Mock Services: Service mocks match actual API behavior
- Type Safety: All TypeScript files compile without errors
- Jest Integration: Proper test execution and reporting
- Pact Matchers: Flexible response validation using Pact matchers
- Error Handling: Comprehensive error scenario coverage
# Initialize Pact environment
npm run pact:setup
# Run contract tests
npm run test:contract
# Test complete pipeline
npm run pact:test-pipeline
# Publish contracts (after broker setup)
npm run pact:publish- Set up Pact Broker URL and token in
.env - Configure environment variables for CI/CD
- Run initial contract tests to generate contracts
- Set up automated verification in deployment pipeline
- 5 External Services: 100% coverage of current integrations
- Multiple Interaction Types: GET, POST, PUT, DELETE requests
- Error Scenarios: Comprehensive error handling validation
- Response Formats: Complete request/response contract definitions
- Stellar Blockchain API: Account and transaction operations
- Email Service: SMTP and bulk email operations
- Cloud Storage: File upload/download operations
- Payment Gateway: Stripe payment processing
- Notification Service: Push notifications and SMS
@pact-foundation/pact: ^15.0.1@pact-foundation/pact-node: ^10.18.0
apps/backend/tests/contract/
├── stellar.consumer.pact.test.ts
├── email.consumer.pact.test.ts
├── storage.consumer.pact.test.ts
├── payment.consumer.pact.test.ts
├── notification.consumer.pact.test.ts
├── provider-verification.test.ts
├── setup.ts
├── global-setup.ts
└── global-teardown.ts
scripts/
├── pact-setup.js
├── pact-ci-integration.sh
└── test-contract-pipeline.js
Configuration:
├── jest.contract.config.js
└── .pactrc
- Add contract tests for additional external services
- Implement contract versioning strategy
- Add performance testing contracts
- Integrate with monitoring and alerting systems
- Contract tests are designed to scale with new service integrations
- Modular structure allows easy addition of new providers
- CI/CD integration supports multiple environments
- Broker integration enables team collaboration
- ✅ All contract test files exist and are properly structured
- ✅ Configuration files are in place and valid
- ✅ Scripts and utilities are created and functional
- ✅ Service dependencies are available and compatible
- ✅ Package.json scripts are configured correctly
- ✅ Pact dependencies are installed and working
- ✅ TypeScript compilation passes without errors
- ✅ Jest configuration is valid and functional
- Contract tests follow established patterns
- Error scenarios are comprehensively covered
- Service mocks match actual API behavior
- TypeScript types are properly defined
- Documentation is clear and comprehensive
- Jest configuration is optimized for contract tests
- Pact configuration supports all required features
- Environment variables are properly documented
- CI/CD scripts are production-ready
- All contract tests can be executed
- Provider verification works correctly
- Pipeline scripts function as expected
- Error handling is robust
- Pact Broker instance must be configured
- Environment variables must be set in CI/CD
- External services must be accessible for provider verification
- Database migrations must be applied if needed
- Monitor contract test execution in CI/CD
- Set up alerts for contract failures
- Train team on contract testing workflows
- Document any additional configuration needed
This PR establishes a robust contract testing foundation that will:
- Improve reliability of external service integrations
- Reduce production incidents through early error detection
- Enable faster development with automated compatibility checks
- Support team collaboration through shared contract definitions
The implementation is production-ready and follows industry best practices for contract testing with Pact.