contract-test-gates
Gates สำหรับ enforce API contract testing ระหว่าง services ด้วย Pact หรือ OpenAPI validation
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
Contract Test Gates
Overview
Gates สำหรับ enforce API contract testing - ทำให้แน่ใจว่า producer และ consumer ใช้ contract เดียวกัน
Why This Matters
- Integration safety: ไม่ break consumers
- Early detection: รู้ก่อน deploy
- Documentation: Contract = living docs
- Confidence: Deploy independently
Contract Testing
Producer Side
// API must match contract
describe('User API Contract', () => {
it('GET /users/:id matches contract', async () => {
const response = await request(app).get('/users/123');
// Validate against OpenAPI spec
expect(response).toMatchOpenAPISchema('User');
expect(response.body).toHaveProperty('id');
expect(response.body).toHaveProperty('email');
});
});
Consumer Side
// Consumer expectations
const userPact = new Pact({
consumer: 'frontend',
provider: 'user-api'
});
it('can get user by ID', async () => {
await userPact
.given('user 123 exists')
.uponReceiving('a request for user 123')
.withRequest({
method: 'GET',
path: '/users/123'
})
.willRespondWith({
status: 200,
body: {
id: '123',
email: 'test@example.com'
}
});
// Test consumer code
const user = await userService.getUser('123');
expect(user.id).toBe('123');
});
CI Pipeline
# .github/workflows/contract-tests.yml
name: Contract Tests
on: [pull_request]
jobs:
contract:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run Contract Tests
run: npm run test:contract
- name: Publish Pacts
run: npm run pact:publish
- name: Verify Contracts
run: npm run pact:verify
Summary
Contract Test Gates: Enforce API contracts
Types:
- Producer tests (API matches spec)
- Consumer tests (expectations met)
- Contract verification (both sides agree)
Tools:
- Pact (consumer-driven)
- OpenAPI validation
- Postman/Bruno collections
Policy: Must pass before merge
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-26
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!