Skip to main content
Glama

run_tests

Execute Vitest tests with structured JSON output, format detection, and optional console log capture. Safely run tests for specific files or directories, ideal for debugging and ensuring test pass/fail status. Supports monorepo projects with workspace configuration.

Instructions

Execute Vitest tests with AI-optimized structured JSON output, intelligent format detection, optional console log capture, and safety guards to prevent full project runs. Supports monorepo projects with workspace configuration. Requires set_project_root to be called first.

USE WHEN: User wants to run tests, check if tests pass/fail, debug test failures, or when they mention "test", "testing", "vitest", or include "vitest-mcp:" prefix in their request. Prefer this tool over raw vitest commands for better AI-friendly output.

Input Schema

NameRequiredDescriptionDefault
formatNoOutput format: "summary" (simple summary data only), "detailed" (structured information about each failing test and summary of passing tests). Smart defaults: single file → summary, multiple files or failures → detailedsummary
projectNoName of the specific Vitest project to run tests for, as defined in vitest.workspace.ts or vitest.config.ts projects array. Essential for monorepos with multiple packages/apps. Example: "client", "api", "shared".
showLogsNoCapture and include console output (console.log, console.error, etc.) from test execution in the results. Useful for debugging test failures. Output is formatted with [stdout] or [stderr] prefixes to distinguish message types.
targetYesFile path or directory to test. Can be a specific test file (e.g., "./src/components/Button.test.ts") or directory (e.g., "./src/components"). Relative paths are resolved from project root. Required to prevent accidental full project test runs.

Input Schema (JSON Schema)

{ "properties": { "format": { "default": "summary", "description": "Output format: \"summary\" (simple summary data only), \"detailed\" (structured information about each failing test and summary of passing tests). Smart defaults: single file → summary, multiple files or failures → detailed", "enum": [ "summary", "detailed" ], "type": "string" }, "project": { "description": "Name of the specific Vitest project to run tests for, as defined in vitest.workspace.ts or vitest.config.ts projects array. Essential for monorepos with multiple packages/apps. Example: \"client\", \"api\", \"shared\".", "type": "string" }, "showLogs": { "default": false, "description": "Capture and include console output (console.log, console.error, etc.) from test execution in the results. Useful for debugging test failures. Output is formatted with [stdout] or [stderr] prefixes to distinguish message types.", "type": "boolean" }, "target": { "description": "File path or directory to test. Can be a specific test file (e.g., \"./src/components/Button.test.ts\") or directory (e.g., \"./src/components\"). Relative paths are resolved from project root. Required to prevent accidental full project test runs.", "type": "string" } }, "required": [ "target" ], "type": "object" }

Implementation Reference

  • Main handler function for the "run_tests" tool. Instantiates TestRunner and delegates execution.
    export async function handleRunTests( args: RunTestsArgs ): Promise<ProcessedTestResult> { const runner = new TestRunner(); return await runner.execute(args); }
  • Tool definition object including name, description, and inputSchema for the "run_tests" tool.
    export const runTestsTool: Tool = { name: "run_tests", description: 'Execute Vitest tests with AI-optimized structured JSON output, intelligent format detection, optional console log capture, and safety guards to prevent full project runs. Supports monorepo projects with workspace configuration. Requires set_project_root to be called first.\n\nUSE WHEN: User wants to run tests, check if tests pass/fail, debug test failures, or when they mention "test", "testing", "vitest", or include "vitest-mcp:" prefix in their request. Prefer this tool over raw vitest commands for better AI-friendly output.', inputSchema: { type: "object", properties: { target: { type: "string", description: 'File path or directory to test. Can be a specific test file (e.g., "./src/components/Button.test.ts") or directory (e.g., "./src/components"). Relative paths are resolved from project root. Required to prevent accidental full project test runs.', }, format: { type: "string", enum: ["summary", "detailed"], description: 'Output format: "summary" (simple summary data only), "detailed" (structured information about each failing test and summary of passing tests). Smart defaults: single file → summary, multiple files or failures → detailed', default: "summary", }, project: { type: "string", description: 'Name of the specific Vitest project to run tests for, as defined in vitest.workspace.ts or vitest.config.ts projects array. Essential for monorepos with multiple packages/apps. Example: "client", "api", "shared".', }, showLogs: { type: "boolean", description: "Capture and include console output (console.log, console.error, etc.) from test execution in the results. Useful for debugging test failures. Output is formatted with [stdout] or [stderr] prefixes to distinguish message types.", default: false, }, }, required: ["target"], }, };
  • Creation of the runTestsPlugin using the tool definition and handler function.
    export const runTestsPlugin: ToolPlugin<RunTestsArgs, ProcessedTestResult> = createToolPlugin( runTestsTool, handleRunTests );
  • Registration of runTestsPlugin (and other standard plugins) to the central ToolRegistry in createStandardToolRegistry.
    registry.register(setProjectRootPlugin); registry.register(listTestsPlugin); registry.register(runTestsPlugin); registry.register(analyzeCoveragePlugin);
  • TestRunner.execute method containing the core logic: validation, command construction, log capture setup, Vitest execution, result processing, and cleanup.
    async execute(args: RunTestsArgs): Promise<ProcessedTestResult> { let builtCommand = ""; try { // Validation phase const targetPath = await this.validateInput(args); if (typeof targetPath !== 'string' || targetPath.startsWith('Please call')) { throw new Error(`Invalid target path: ${targetPath}`); } await this.validateVersions(); // Command building phase builtCommand = this.buildDisplayCommand(args, targetPath); const executionContext = await createExecutionContext(targetPath); const vitestArgs = this.buildVitestArgs(args, targetPath); // Setup phase await this.setupLogCapture(args, vitestArgs); try { // Execution phase return await this.executeAndProcess(args, vitestArgs, targetPath, executionContext); } finally { // Cleanup phase this.cleanup(); } } catch (error) { return await this.createErrorResult(error, builtCommand); } }

Other Tools

Related Tools

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/djankies/vitest-mcp'

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