Skip to main content
Glama
mednabouli

MCP Multi-Context Hook Generator

by mednabouli
generateComponentTests.ts1.76 kB
// mcp/generator/generateComponentTests.ts import fs from 'fs'; import path from 'path'; import { ComponentInfo } from '../crawler/components'; export interface TestGeneratorOptions { outputDir: string; overwrite?: boolean; // overwrite existing test files } export function generateComponentTests(components: ComponentInfo[], options: TestGeneratorOptions) { const { outputDir, overwrite = true } = options; if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true }); components.forEach((comp) => { const testFileName = `${comp.name}.test.tsx`; const testFilePath = path.join(outputDir, testFileName); if (!overwrite && fs.existsSync(testFilePath)) return; const usageSnippet = (comp as any).usage || `<${comp.name} />`; const defaultProps = comp.props .filter((p) => p.defaultValue !== undefined && p.defaultValue !== '') .map((p) => `${p.name}={${p.defaultValue}}`) .join(' '); const content = ` // AUTO-GENERATED TEST - DO NOT EDIT import React from 'react'; import { render, screen } from '@testing-library/react'; import '${comp.name}'; ${comp.filePath.startsWith('.') ? `import { ${comp.name} } from '${comp.filePath.replace(/\.tsx?$/, '')}';` : ''} describe('${comp.name} Component', () => { it('renders without crashing', () => { render(<${comp.name} ${defaultProps} />); }); it('matches snapshot', () => { const { asFragment } = render(<${comp.name} ${defaultProps} />); expect(asFragment()).toMatchSnapshot(); }); it('usage snippet renders', () => { render( <> ${usageSnippet} </> ); }); }); `; fs.writeFileSync(testFilePath, content, 'utf-8'); console.log(`✅ Test generated: ${testFilePath}`); }); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mednabouli/MCPV2'

If you have feedback or need assistance with the MCP directory API, please join our Discord server