MCP生成单元测试服务器
一个智能模型上下文协议(MCP)服务器,使用Jest、Karma/Jasmine或其他测试框架自动为Angular、React和Next.js项目生成全面的单元测试。
特性
- 🔍 自动项目检测 -检测Angular、React或Next.js项目及其版本
- 📦 框架管理 -检查测试框架并提供自动安装
- 📚 实时文档获取 -在生成测试之前检索最新的Jest和Jasmine文档
- 🤖 LLM发电 -使用Claude Sonnet 4.5+进行智能测试生成
- 🎯 高覆盖目标 -生成针对超过85%代码覆盖率的测试
- ✅ 所有公共方法 -为所有导出的函数和公共类方法创建测试
- 🔄 并排验证 -显示源代码和测试代码,便于查看
- 🛠️ 多个框架 -支持Jest、Karma/Jasmine和Vitest
安装
npm install
npm run build用法
作为MCP服务器
添加到您的MCP客户端配置中(例如,Claude Desktop、带MCP的VS Code):
{
"mcpServers": {
"generate-unit-testing": {
"command": "node",
"args": ["/path/to/GenerateUnitTesting/build/index.js"]
}
}
}可用工具
1. detect_project
检测项目类型和测试框架。
{
"filePath": "/path/to/your/project/src/component.ts"
}2. install_test_framework
使用用户权限安装缺少的测试框架。
{
"projectPath": "/path/to/your/project",
"framework": "jest",
"autoInstall": false
}3. generate_unit_test
为源文件生成全面的单元测试。
{
"filePath": "/path/to/your/project/src/component.ts",
"framework": "jest",
"coverageTarget": 85,
"generateForAllPublicMethods": true,
"includeEdgeCases": true
}4. validate_test
运行生成的测试并计算覆盖率。
{
"testFilePath": "/path/to/your/project/src/component.test.ts",
"framework": "jest"
}5. display_side_by_side
创建源代码和测试代码的并排比较。
{
"sourceFilePath": "/path/to/your/project/src/component.ts",
"testFilePath": "/path/to/your/project/src/component.test.ts"
}可用资源
docs://jest/latest-最新Jest文档docs://jasmine/latest-最新Jasmine文档
可用提示
generate-unit-test-综合单元测试生成提示generate-edge-cases-边缘用例测试生成提示generate-mocks-模拟生成提示
建筑
src/
├── index.ts # Main MCP server entry point
├── types.ts # TypeScript type definitions
├── tools/
│ ├── detectProject.ts # Project and framework detection
│ └── testGenerator.ts # Test generation logic
├── resources/
│ └── documentationFetcher.ts # Live doc fetching from Jest/Jasmine
├── templates/
│ └── testTemplates.ts # Framework-specific test templates
├── prompts/
│ └── testPrompts.ts # LLM prompts for test generation
└── utils/
├── fileUtils.ts # File system utilities
└── codeAnalyzer.ts # AST-based code analysis工作流程
- 项目检测 -分析
package.json确定项目类型和现有测试设置 - 框架检查 -验证测试框架安装,如果缺少,则提示安装
- 文档提取 -从官方来源检索最新的框架文档
- 代码分析 -使用Babel解析源代码以提取函数、类和方法
- 测试生成 -使用LLM和上下文(代码+文档+模板)生成全面的测试
- 验证 -运行测试并计算覆盖率
- 显示 -显示并排比较以供开发人员查看
支持的框架
玩笑
- React和Next.js项目
- Types/JavaScript
- 现代异步/等待模式
- 模拟功能和间谍
卡玛/茉莉
- Angular项目
- 使用TestBed进行组件测试
- 服务和管道测试
- 传统的describe/it语法
覆盖目标
服务器目标 >85%的代码覆盖率 通过生成:
- 所有公共功能测试
- 所有公共类方法的测试
- 边缘案例场景
- 错误处理测试
- 异步操作测试
- 依赖关系的模拟实现
示例输出
对于React组件:
// Original: Button.tsx
export const Button = ({ onClick, label }) => {
return {label};
};生成的测试:
// Generated: Button.test.tsx
import { render, fireEvent } from '@testing-library/react';
import { Button } from './Button';
describe('Button', () => {
it('should render with label', () => {
const { getByText } = render( {}} />);
expect(getByText('Click me')).toBeInTheDocument();
});
it('should call onClick when clicked', () => {
const handleClick = jest.fn();
const { getByText } = render();
fireEvent.click(getByText('Click me'));
expect(handleClick).toHaveBeenCalledTimes(1);
});
// ... more tests for edge cases;需求
- Node.js 18+
- TypeScript 5+
- MCP客户端(Claude Desktop、带MCP扩展的VS Code等)
发展
# Install dependencies
npm install
# Build
npm run build
# Watch mode
npm run watch
# Start server
npm start配置
服务器会自动检测:
- 包管理器(npm、yarn、pnpm)
- 项目类型(Angular、React、Next.js)
- 现有测试框架
- Types/JavaScript
无需手动配置!
许可证
麻省理工学院
贡献
欢迎投稿!请确保:
- 代码遵循现有模式
- 包括对新功能的测试
- 文档已更新
支持
- 对于问题或疑问,请在存储库上打开问题。
- 作者:Ahmadreza Shamimi(https://github.com/shentia)
