Vitest 3.2.4
Testing Library 16.3.0
Playwright 1.55.0
{
"scripts": {
"test": "vitest",
"test:coverage": "vitest --coverage",
"test:e2e": "playwright test",
"test:e2e:chromium": "playwright test --project=chromium",
"test:e2e:headed": "playwright test --headed"
}
}
测试范围
src/common/utils.ts)src/common/hooks.ts)src/manager/)测试位置
src/
├── common/
│ └── __tests__/
│ └── utils.test.ts
├── manager/
│ └── __tests__/
│ ├── rtc.test.ts
│ ├── rtm.test.ts
│ └── stt.test.ts
└── components/
└── __tests__/
└── *.test.tsx
测试范围
测试位置
tests/
├── integration/
│ ├── store-integration.test.ts
│ ├── manager-integration.test.ts
│ └── api-integration.test.ts
└── __mocks__/
└── agora-mocks.ts
测试范围
测试位置
e2e/
├── specs/
│ ├── login.spec.ts
│ ├── home.spec.ts
│ ├── stt-functionality.spec.ts
│ └── error-handling.spec.ts
├── pages/
│ ├── login.page.ts
│ └── home.page.ts
└── fixtures/
└── test-data.ts
// tests/__mocks__/agora-mocks.ts
export const mockRtcClient = {
join: vi.fn(),
publish: vi.fn(),
leave: vi.fn(),
}
export const mockSttResponse = {
taskId: "test-task-123",
status: "started",
}
// tests/fixtures/test-data.ts
export const testUserInfo = {
userId: 123456,
userName: "Test User",
}
export const testChannelConfig = {
channel: "test-channel",
appId: "test-app-id",
}
// 描述性测试名称
describe("SttManager", () => {
it("should start transcription when valid languages provided", () => {
// 测试实现
})
it("should throw error when initialization is missing", () => {
// 测试实现
})
})
AAA模式 (Arrange-Act-Assert)
it("should handle user join correctly", () => {
// Arrange
const manager = new RtcManager()
const joinConfig = { channel: "test", userId: 123 }
// Act
await manager.join(joinConfig)
// Assert
expect(manager.client.join).toHaveBeenCalledWith(
expect.any(String),
"test",
expect.any(String),
123,
)
})
it("should handle async operations correctly", async () => {
// 使用async/await处理异步操作
const result = await manager.startTranscription()
expect(result).toBeDefined()
expect(result.status).toBe("started")
})
name: Test Suite
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm ci
- run: npm run test:coverage
- run: npm run test:e2e
// 增加超时时间
it("should complete within timeout", async () => {
await expect(manager.longOperation()).resolves.toBeDefined()
}, 10000) // 10秒超时
// 正确设置模拟
beforeEach(() => {
vi.mock("agora-rtc-sdk-ng", () => ({
createClient: () => mockRtcClient,
}))
})
--headed 模式运行E2E测试