技能标识:senior-qa
Generates unit tests, integration tests, and E2E tests for React/Next.js applications. Scans components to create Jest + React Testing Library test stubs, analyzes Istanbul/LCOV coverage reports to surface gaps, scaffolds Playwright test files from Next.js routes, mocks API calls with MSW, creates test fixtures, and configures test runners. Use when the user asks to "generate tests", "write unit tests", "analyze test coverage", "scaffold E2E tests", "set up Playwright", "configure Jest", "implem
针对React和Next.js应用程序的测试自动化、覆盖率分析和质量保证模式。
bash
扫描React/TypeScript组件,生成结构合理的Jest + React Testing Library测试桩。
输入: 包含React组件的源目录
输出: 包含describe块、渲染测试、交互测试的测试文件
用法:
bash
支持的模式:
解析Jest/Istanbul覆盖率报告,识别缺口、未覆盖分支,并提供可操作的建议。
输入: 覆盖率报告(JSON或LCOV格式)
输出: 带建议的覆盖率分析
用法:
bash
扫描Next.js页面/应用目录,生成包含常见交互的Playwright测试文件。
输入: Next.js页面或应用目录
输出: 按路由组织的Playwright测试文件
用法:
bash
在为新或现有React组件设置测试时使用。
步骤1:扫描项目中未测试的组件
bash
python scripts/testsuitegenerator.py src/components/ --scan-only
步骤2:生成测试桩
bash
python scripts/testsuitegenerator.py src/components/ --output tests/
步骤3:审查并自定义生成的测试
typescript
// tests/Button.test.tsx (已生成)
import { render, screen, fireEvent } from @testing-library/react;
import { Button } from ../src/components/Button;
describe(Button, () => {
it(使用标签渲染, () => {
render();
expect(screen.getByRole(button, { name: 点击我 })).toBeInTheDocument();
});
it(点击时调用onClick, () => {
const handleClick = jest.fn();
render();
fireEvent.click(screen.getByRole(button));
expect(handleClick).toHaveBeenCalledTimes(1);
});
// TODO: 添加你的特定测试用例
});
步骤4:运行测试并检查覆盖率
bash
npm test -- --coverage
python scripts/coverage_analyzer.py coverage/coverage-final.json
在改进测试覆盖率或准备发布时使用。
步骤1:生成覆盖率报告
bash
npm test -- --coverage --coverageReporters=json
步骤2:分析覆盖率缺口
bash
python scripts/coverage_analyzer.py coverage/coverage-final.json --threshold 80
步骤3:识别关键路径
bash
python scripts/coverage_analyzer.py coverage/ --critical-paths
步骤4:生成缺失的测试桩
bash
python scripts/testsuitegenerator.py src/ --uncovered-only --output tests/
步骤5:验证改进
bash
npm test -- --coverage
python scripts/coverage_analyzer.py coverage/ --compare previous-coverage.json
在为Next.js项目设置Playwright时使用。
步骤1:初始化Playwright(如果未安装)
bash
npm init playwright@latest
步骤2:从路由搭建E2E测试
bash
python scripts/e2etestscaffolder.py src/app/ --output e2e/
步骤3:配置认证fixture
typescript
// e2e/fixtures/auth.ts (已生成)
import { test as base } from @playwright/test;
export const test = base.extend({
authenticatedPage: async ({ page }, use) => {
await page.goto(/login);
await page.fill([name=email], test@example.com);
await page.fill([name=password], password);
await page.click(button[type=submit]);
await page.waitForURL(/dashboard);
await use(page);
},
});
步骤4:运行E2E测试
bash
npx playwright test
npx playwright show-report
步骤5:添加到CI流水线
yaml
| 文件 | 内容 | 使用时机 |
|---|---|---|
| references/testingstrategies.md | 测试金字塔、测试类型、覆盖率目标、CI/CD集成 | 设计测试策略 |
| references/testautomation_patterns.md |
typescript
// 首选(可访问)
screen.getByRole(button, { name: 提交 });
screen.getByLabelText(/邮箱/i);
screen.getByPlaceholderText(/搜索/i);
// 备选
screen.getByTestId(custom-element);
typescript
// 等待元素
await screen.findByText(/已加载/i);
// 等待移除
await waitForElementToBeRemoved(() => screen.queryByText(/加载中/i));
// 等待条件
await waitFor(() => {
expect(mockFn).toHaveBeenCalled();
});
typescript
import { rest } from msw;
import { setupServer } from msw/node;
const server = setupServer(
rest.get(/api/users, (req, res, ctx) => {
return res(ctx.json([{ id: 1, name: 张三 }]));
})
);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
typescript
// 首选
page.getByRole(button, { name: 提交 });
page.getByLabel(邮箱);
page.getByText(欢迎);
// 链式调用
page.getByRole(listitem).filter({ hasText: 产品 });
javascript
module.exports = {
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
};
bash
以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:
帮我安装 SkillHub 和 senior-qa-1776349703 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 senior-qa-1776349703 技能
skillhub install senior-qa-1776349703
文件大小: 46.42 KB | 发布时间: 2026-4-17 15:56