emulate-seed
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEmulate Seed Configs
Emulate 种子配置
Generate and manage seed configs for emulate (Apache-2.0) — Vercel Labs' stateful API emulation tool. Each category has individual rule files in loaded on-demand.
rules/Not mocks. Emulate provides full state machines with cascading deletes, cursor pagination, webhook delivery, and HMAC signature verification. Create a PR via the API and it appears in . Delete a repo and its issues, PRs, and webhooks cascade-delete.
GET /repos/:owner/:repo/pulls为emulate(Apache-2.0协议)——Vercel Labs推出的有状态API模拟工具——生成并管理种子配置。每个分类在目录下都有独立的规则文件,会按需加载。
rules/并非Mock工具。Emulate提供具备级联删除、游标分页、Webhook投递和HMAC签名验证功能的完整状态机。通过API创建PR后,该PR会出现在接口的返回列表中。删除仓库时,其关联的议题、PR和Webhook会被级联删除。
GET /repos/:owner/:repo/pullsQuick Reference
快速参考
| Category | Rules | Impact | When to Use |
|---|---|---|---|
| Seed Config | 1 | HIGH | Setting up emulate.config.yaml for test environments |
| Service Selection | 1 | MEDIUM | Choosing GitHub/Vercel/Google for your tests |
| Webhook Setup | 1 | MEDIUM | Testing webhook delivery with HMAC verification |
| Parallel CI | 1 | HIGH | Running tests in parallel without port collisions |
| Auth Tokens | 1 | MEDIUM | Seeding tokens mapped to emulated users |
Total: 5 rules across 5 categories
Quick Start
快速入门
bash
undefinedbash
undefinedInstall
安装
npm install --save-dev emulate
npm install --save-dev emulate
Start all services (GitHub :4001, Vercel :4000, Google :4002)
启动所有服务(GitHub :4001,Vercel :4000,Google :4002)
npx emulate
npx emulate
Start specific services with seed data
使用种子数据启动特定服务
npx emulate --service github --seed ./emulate.config.yaml
npx emulate --service github --seed ./emulate.config.yaml
Generate a starter config
生成初始配置
npx emulate init --service github
undefinednpx emulate init --service github
undefinedServices
支持服务
| Service | Default Port | Coverage |
|---|---|---|
| GitHub | | Repos, PRs, issues, comments, reviews, Actions, webhooks, orgs, teams |
| Vercel | | Projects, deployments, domains, env vars, teams |
| Google OAuth | | OAuth 2.0 authorize, token exchange, userinfo |
See for full endpoint lists.
references/api-coverage.md| 服务 | 默认端口 | 覆盖范围 |
|---|---|---|
| GitHub | | 仓库、PR、议题、评论、评审、Actions、Webhook、组织、团队 |
| Vercel | | 项目、部署、域名、环境变量、团队 |
| Google OAuth | | OAuth 2.0授权、令牌交换、用户信息 |
完整的端点列表请查看。
references/api-coverage.mdSeed Config Structure
种子配置结构
A seed config pre-populates the emulator with tokens, users, repos, and projects so tests start from a known state.
yaml
undefined种子配置会预先在模拟器中填充令牌、用户、仓库和项目,让测试从已知状态开始。
yaml
undefinedemulate.config.yaml
emulate.config.yaml
tokens:
dev_token:
login: yonatangross
scopes: [repo, workflow, admin:org]
ci_token:
login: ci-bot
scopes: [repo]
github:
users:
- login: yonatangross
name: Yonatan Gross
- login: ci-bot
name: CI Bot
repos:
- owner: yonatangross
name: my-project
private: false
default_branch: main
topics: [typescript, testing]
vercel:
users:
- username: yonatangross
email: yonaigross@gmail.com
projects:
- name: my-docs
framework: next
See `rules/seed-config.md` for full schema and best practices.tokens:
dev_token:
login: yonatangross
scopes: [repo, workflow, admin:org]
ci_token:
login: ci-bot
scopes: [repo]
github:
users:
- login: yonatangross
name: Yonatan Gross
- login: ci-bot
name: CI Bot
repos:
- owner: yonatangross
name: my-project
private: false
default_branch: main
topics: [typescript, testing]
vercel:
users:
- username: yonatangross
email: yonaigross@gmail.com
projects:
- name: my-docs
framework: next
完整的Schema和最佳实践请查看`rules/seed-config.md`。Programmatic SDK
程序化SDK
typescript
import { createEmulator } from 'emulate'
const github = await createEmulator({ service: 'github', port: 4001 })
// github.url -> 'http://localhost:4001'
// State is real — create a PR and it appears in the list
const res = await fetch(`${github.url}/repos/org/repo/pulls`, {
method: 'POST',
headers: { Authorization: 'Bearer dev_token' },
body: JSON.stringify({ title: 'Test PR', head: 'feature', base: 'main' })
})
const prs = await fetch(`${github.url}/repos/org/repo/pulls`)
// -> includes the PR we just created
// Cleanup
github.reset() // Synchronous state wipe
await github.close() // Shut down serverSee for advanced patterns (multi-service, lifecycle hooks).
references/sdk-patterns.mdtypescript
import { createEmulator } from 'emulate'
const github = await createEmulator({ service: 'github', port: 4001 })
// github.url -> 'http://localhost:4001'
// 状态是真实的——创建PR后,它会出现在列表中
const res = await fetch(`${github.url}/repos/org/repo/pulls`, {
method: 'POST',
headers: { Authorization: 'Bearer dev_token' },
body: JSON.stringify({ title: 'Test PR', head: 'feature', base: 'main' })
})
const prs = await fetch(`${github.url}/repos/org/repo/pulls`)
// -> 包含我们刚刚创建的PR
// 清理
github.reset() // 同步清空状态
await github.close() // 关闭服务器高级模式(多服务、生命周期钩子)请查看。
references/sdk-patterns.mdWebhook Delivery
Webhook投递
Emulate delivers real webhooks with HMAC-SHA256 signatures when state changes:
typescript
import crypto from 'crypto'
function verifyWebhook(payload: string, signature: string, secret: string): boolean {
const expected = 'sha256=' + crypto
.createHmac('sha256', secret)
.update(payload)
.digest('hex')
return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected))
}See for webhook receiver patterns.
rules/webhook-setup.md当状态发生变化时,Emulate会发送带有HMAC-SHA256签名的真实Webhook:
typescript
import crypto from 'crypto'
function verifyWebhook(payload: string, signature: string, secret: string): boolean {
const expected = 'sha256=' + crypto
.createHmac('sha256', secret)
.update(payload)
.digest('hex')
return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected))
}Webhook接收器模式请查看。
rules/webhook-setup.mdCI Integration
CI集成
yaml
undefinedyaml
undefined.github/workflows/test.yml
.github/workflows/test.yml
jobs:
test:
steps:
- uses: actions/checkout@v4
- name: Start emulate
run: npx emulate --service github --seed .emulate/ci.yaml &
- name: Wait for emulate
run: sleep 2
- name: Run tests
run: npm test
env:
GITHUB_API_BASE: http://localhost:4001
VERCEL_API_BASE: http://localhost:4000
undefinedjobs:
test:
steps:
- uses: actions/checkout@v4
- name: 启动emulate
run: npx emulate --service github --seed .emulate/ci.yaml &
- name: 等待emulate启动
run: sleep 2
- name: 运行测试
run: npm test
env:
GITHUB_API_BASE: http://localhost:4001
VERCEL_API_BASE: http://localhost:4000
undefinedParallel Test Execution
并行测试执行
Each test worker gets its own port to avoid race conditions:
typescript
// vitest.config.ts
const workerPort = 4001 + parseInt(process.env.VITEST_WORKER_ID || '0')See for full parallel isolation patterns.
rules/parallel-ci.md每个测试工作进程会分配独立的端口,避免竞争条件:
typescript
// vitest.config.ts
const workerPort = 4001 + parseInt(process.env.VITEST_WORKER_ID || '0')完整的并行隔离模式请查看。
rules/parallel-ci.mdDecision Matrix
工具选择矩阵
| Tool | When to Use | Stateful? | Platforms |
|---|---|---|---|
| emulate (FIRST CHOICE) | GitHub/Vercel/Google API testing | YES | GitHub, Vercel, Google |
| Pact | Contract verification between services | No | Any |
| MSW | In-browser/Node HTTP mocking | No | Any |
| Nock | Node.js HTTP intercept | No | Any |
| WireMock | HTTP stub server | Partial | Any |
Use emulate when:
- Testing code that calls GitHub, Vercel, or Google APIs
- You need state persistence across multiple API calls in a test
- You want webhook delivery with real HMAC signatures
- You need cascading side-effects (delete repo -> PRs cascade-delete)
Use MSW/Nock when:
- Mocking arbitrary HTTP APIs not covered by emulate
- You need in-browser interception (MSW)
- Tests only need single request/response pairs
| 工具 | 使用场景 | 支持状态持久化? | 支持平台 |
|---|---|---|---|
| emulate(首选) | GitHub/Vercel/Google API测试 | 是 | GitHub、Vercel、Google |
| Pact | 服务间契约验证 | 否 | 任意 |
| MSW | 浏览器/Node.js HTTP Mock | 否 | 任意 |
| Nock | Node.js HTTP拦截 | 否 | 任意 |
| WireMock | HTTP stub服务器 | 部分支持 | 任意 |
推荐使用emulate的场景:
- 测试调用GitHub、Vercel或Google API的代码
- 测试中需要跨多个API调用持久化状态
- 需要带有真实HMAC签名的Webhook投递
- 需要级联副作用(删除仓库→PR级联删除)
推荐使用MSW/Nock的场景:
- Mock emulate未覆盖的任意HTTP API
- 需要浏览器端拦截(MSW)
- 测试仅需单请求/响应对
Related Skills
相关技能
- — Integration test patterns (emulate as first choice for API tests)
testing-integration - — End-to-end test patterns with emulated backends
testing-e2e - — Unit test patterns (use emulate for API-dependent units)
testing-unit - — Auth token patterns (emulate token seeding)
security-patterns
- —— 集成测试模式(API测试首选emulate)
testing-integration - —— 基于模拟后端的端到端测试模式
testing-e2e - —— 单元测试模式(依赖API的单元测试可使用emulate)
testing-unit - —— 认证令牌模式(emulate令牌种子配置)
security-patterns
CLI Reference
CLI参考
See for all CLI flags and commands.
references/cli-reference.md所有CLI标志和命令请查看。
references/cli-reference.mdSDK Patterns
SDK模式
See for programmatic usage.
references/sdk-patterns.mdcreateEmulator()程序化的用法请查看。
createEmulator()references/sdk-patterns.md