generate_tests_for_coverage
Generate intelligent tests to achieve 80%+ code coverage for target files using Jest, Vitest, or Mocha frameworks. Includes options for edge cases and accessibility testing.
Instructions
Generate intelligent tests to achieve 80%+ coverage
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| targetFile | Yes | File to generate tests for | |
| testFramework | No | Test framework to use | |
| coverageTarget | No | Target coverage percentage (default: 80) | |
| includeEdgeCases | No | Include edge case tests | |
| includeAccessibility | No | Include accessibility tests for components |
Implementation Reference
- Main handler function that implements the tool logic: reads and analyzes the target file using Babel AST, generates test code for specified framework, estimates coverage, and provides suggestions to reach target coverage.export async function generateTestsForCoverage( config: TestGenerationConfig ): Promise<TestGenerationResult> { const result: TestGenerationResult = { success: false, testCode: '', coverage: { estimated: 0, functions: 0, branches: 0, lines: 0, }, testCategories: { unit: 0, integration: 0, edgeCases: 0, errorHandling: 0, accessibility: 0, }, suggestions: [], }; try { // Read target file if (!existsSync(config.targetFile)) { throw new Error(`Target file not found: ${config.targetFile}`); } const fileContent = readFileSync(config.targetFile, 'utf-8'); const fileType = detectFileType(fileContent); // Parse the file to understand structure const ast = parseCode(fileContent); const analysis = analyzeCode(ast, fileType); // Generate tests based on analysis const testFramework = config.testFramework || detectTestFramework(); const tests = generateTests(analysis, testFramework, config); // Calculate coverage estimation const coverage = estimateCoverage(analysis, tests); // Generate suggestions for reaching target coverage const suggestions = generateSuggestions( coverage, config.coverageTarget || 80, analysis ); result.success = true; result.testCode = tests.code; result.coverage = coverage; result.testCategories = tests.categories; result.suggestions = suggestions; } catch (error) { result.success = false; result.suggestions = [`Error generating tests: ${error}`]; } return result; }
- Input schema definition for the tool, specifying parameters like targetFile, testFramework, coverageTarget, etc.name: 'generate_tests_for_coverage', description: 'Generate intelligent tests to achieve 80%+ coverage', inputSchema: { type: 'object', properties: { targetFile: { type: 'string', description: 'File to generate tests for', }, testFramework: { type: 'string', enum: ['jest', 'vitest', 'mocha'], description: 'Test framework to use', }, coverageTarget: { type: 'number', description: 'Target coverage percentage (default: 80)', }, includeEdgeCases: { type: 'boolean', description: 'Include edge case tests', }, includeAccessibility: { type: 'boolean', description: 'Include accessibility tests for components', }, }, required: ['targetFile'], }, },
- src/tools/index.ts:165-183 (registration)Tool registration and dispatch in the main switch statement: parses arguments with Zod schema matching the tool schema and calls the handler function.case 'generate_tests_for_coverage': { const params = z.object({ targetFile: z.string(), testFramework: z.enum(['jest', 'vitest', 'mocha']).optional(), coverageTarget: z.number().optional(), includeEdgeCases: z.boolean().optional(), includeAccessibility: z.boolean().optional(), }).parse(args); const result = await generateTestsForCoverage(params); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- src/tools/index.ts:23-23 (registration)Imports the tool definitions array used for listing tools.import { toolDefinitions } from './tool-definitions.js';
- src/tools/index.ts:14-14 (registration)Imports the handler function for use in tool dispatch.import { generateTestsForCoverage } from './testing/test-generator.js';