This project demonstrates contract testing for the Employee Management API using Pact-JS and TypeScript.
Contract testing ensures that the communication between different services remains consistent and reliable. This project implements both consumer and provider contract tests for the Employee Management API.
├── api-spec/
│ └── emp.yaml # OpenAPI specification
├── src/
│ ├── types/
│ │ └── employee.types.ts # TypeScript interfaces
│ ├── consumer/
│ │ └── employee-client.ts # API client implementation
│ └── provider/
│ └── mock-employee-provider.ts # Mock provider for testing
├── tests/
│ ├── consumer/
│ │ └── employee-consumer.test.ts # Consumer contract tests
│ └── provider/
│ └── employee-provider.test.ts # Provider contract tests
├── pacts/ # Generated Pact files
└── logs/ # Pact logs
- Complete API Coverage: Tests all CRUD operations for employees
- HATEOAS Support: Validates hypermedia links in responses
- Error Handling: Tests various error scenarios (404, 400, etc.)
- Type Safety: Full TypeScript support with strict typing
- Realistic Data: Uses proper employee roles and validation rules
GET /employees- Get all employees with pagination and HATEOAS linksGET /employees/{id}- Get employee by ID with HATEOAS linksPOST /employees- Create new employee with validationPUT /employees/{id}- Update existing employeeDELETE /employees/{id}- Delete employee
The API supports the following employee roles:
- Developer
- Manager
- Designer
- QA Engineer
- DevOps
- Product Manager
-
Install dependencies:
npm install
-
Build the project:
npm run build
Consumer tests verify that the client can interact with the provider according to the contract:
npm run test:consumerProvider tests verify that the provider implementation satisfies the contract:
npm run test:providerRun both consumer and provider tests:
npm test- Consumer Tests Generate Pact: Consumer tests create a Pact file describing expected interactions
- Provider Tests Verify Pact: Provider tests validate that the implementation satisfies the contract
- Pact Files: Generated contracts are stored in the
./pactsdirectory
{
"description": "a request for all employees",
"request": {
"method": "GET",
"path": "/employees"
},
"response": {
"status": 200,
"body": {
"_embedded": {
"employeeList": [
{
"id": 1,
"name": "John Doe",
"role": "Developer",
"_links": {
"self": { "href": "http://localhost:8080/employees/1" },
"employees": { "href": "http://localhost:8080/employees" }
}
}
]
},
"_links": {
"self": { "href": "http://localhost:8080/employees" }
}
}
}
}{
"description": "a request to create a new employee",
"request": {
"method": "POST",
"path": "/employees",
"body": {
"name": "Jane Smith",
"role": "Manager"
}
},
"response": {
"status": 200,
"body": {
"id": 1,
"name": "Jane Smith",
"role": "Manager"
}
}
}- 404 Not Found: Employee doesn't exist
- 400 Bad Request: Invalid data (empty fields, invalid roles, duplicate names)
- Validation Errors: Comprehensive validation error messages
Provider tests use state handlers to set up test conditions:
employees exist: Sets up test data with multiple employeesno employees exist: Ensures empty employee listemployee with id X exists: Sets up specific employeeemployee does not exist: Ensures no test data
- Consumer-Driven Contracts: Consumers define what they need
- Provider States: Clear test state management
- Realistic Test Data: Uses actual business domain data
- Comprehensive Coverage: Tests happy paths and error scenarios
- Type Safety: Strong TypeScript typing throughout
- HATEOAS Testing: Validates hypermedia API patterns
The setup supports integration with Pact Broker for sharing contracts across teams:
# Publish contracts to Pact Broker
npm run pact:publish
# Check if safe to deploy
npm run pact:can-i-deployFor Pact Broker integration:
PACT_BROKER_BASE_URL: Pact Broker URLPACT_BROKER_USERNAME: Authentication usernamePACT_BROKER_PASSWORD: Authentication password
- Port Conflicts: Ensure ports 8080 and 9999 are available
- Missing Dependencies: Run
npm installto install all dependencies - TypeScript Errors: Run
npm run buildto check for compilation issues
Pact logs are available in the ./logs directory for debugging test failures.
- Add new test scenarios in the appropriate test files
- Update types if API changes
- Ensure both consumer and provider tests pass
- Update documentation for new features