api-contract-testing-guideline
Skill: api-contract-testing-guideline
Purpose
Ensure the system is tested against the approved API contract so that:
- backend implementation matches OpenAPI
- frontend assumptions match OpenAPI
- response wrapper and pagination contracts are enforced
- auth/security contract behavior is verified
- contract drift is detected early
Scope
Use this skill when testing or validating:
- endpoint existence
- HTTP method correctness
- request schema correctness
- response schema correctness
- success/error wrapper consistency
- pagination response structure
- auth/security requirements
- frontend response consumption behavior
Source of Truth Rule
openapi.yaml is the single API source of truth.
Testing must validate implementation and consumption against OpenAPI, not against assumptions or accidental implementation quirks.
Contract test logic must not silently “accept” undocumented backend/frontend behavior.
Contract Coverage Areas
1. Path and method correctness
Verify:
- endpoint path exists as documented
- HTTP method matches documented method
- required path params/query params/body usage matches the contract
2. Request contract correctness
Verify:
- required fields are required
- request body shape matches schema
- query/path params are used correctly
- invalid input paths behave appropriately
3. Response contract correctness
Verify:
- success wrapper is present
- error responses follow expected semantics
- response payload fields match documented structure
- no extra dangerous undocumented fields are exposed casually
4. Pagination contract correctness
Verify:
- paginated endpoints return pagination payload inside
data content,page,size, andtotalexist as expected- frontend consumes pagination from wrapped
data
5. Security contract correctness
Verify:
- public endpoints remain public
- protected endpoints require auth where documented
- bearer auth behavior matches documented security expectations
- frontend respects protected/public distinctions
Standard Success Wrapper Rule
All successful responses must be tested against:
{
"code": 0,
"message": "success",
"data": {}
}
Do not accept raw top-level business payloads when the contract defines a wrapper.
Standard Pagination Wrapper Rule
Paginated responses must be tested against:
{
"code": 0,
"message": "success",
"data": {
"content": [],
"page": 1,
"size": 10,
"total": 100
}
}
Both backend and frontend tests must reflect this exact wrapper logic.
Auth Contract Rule
When auth is involved, contract tests should verify as applicable:
- canonical auth endpoints exist and behave correctly
- public auth endpoints remain unauthenticated
- protected auth/business endpoints require bearer auth
/api/auth/mereturns current authenticated user from auth context- auth request/response shapes remain documented
- password hash and internal token-persistence fields are not exposed
- bearer token is sent/expected through
Authorizationheader for protected APIs
For stateless JWT systems:
- do not normalize session-first contract assumptions
Backend Contract Test Rule
Backend-side contract tests should verify:
- endpoint availability
- request validation
- wrapper correctness
- pagination correctness
- security/auth behavior
- field presence/absence according to OpenAPI
Prefer high-value coverage over blanket noise.
Frontend Contract Test Rule
Frontend-side contract tests should verify:
- module API functions target correct endpoints
- stores/composables/pages unwrap
ApiResponse<T>correctly - paginated flows consume wrapped page payload correctly
- auth flows align with documented auth endpoints and security model
- route guards are consistent with protected/public API assumptions when relevant
Drift Detection Rule
Flag contract drift whenever:
- backend exposes fields not expected by OpenAPI
- frontend expects fields not guaranteed by OpenAPI
- pagination shape differs
- auth/public/protected assumptions differ
- HTTP method/path differs
- status/error semantics materially drift
Do not “adapt” tests to broken drift unless the contract is explicitly updated and approved.
Naming Rule
Contract test naming should remain clear.
Examples:
AuthApiContractTestUserPaginationContractTestauth.api.contract.test.ts
Avoid vague naming.
Forbidden Patterns
- treating OpenAPI as optional
- writing tests against accidental implementation behavior
- ignoring wrapper contract mismatches
- ignoring frontend/backend drift
- assuming auth behavior not documented in contract
- silently accepting raw top-level payloads
Final Goal
Contract tests should make the system:
- OpenAPI-consistent
- frontend/backend-aligned
- wrapper-safe
- pagination-safe
- auth-contract-safe
- harder to drift over time
