Automated TDD enforcement for Claude Code.
TDD Guard monitors file operations in real-time and blocks any changes that violate TDD principles. By analyzing test results, todos, and code changes, it ensures Claude Code follows the red-green-refactor cycle without manual reminders.
Click to watch TDD Guard in action
- Focus on solving problems - TDD Guard enforces the rules while you design solutions
- Save context for what matters - No more TDD instructions cluttering your CLAUDE.md
- Works with your stack - TypeScript, JavaScript, Python, and PHP today. More languages coming soon
- Control without context switches - Toggle with
tdd-guard on/offmid-session - Flexible validation - Use local Claude or configure Anthropic API
- Node.js 18+
- Test Runner:
- JavaScript/TypeScript: Vitest or Jest
- Python: pytest
- PHP: PHPUnit 9.x, 10.x, 11.x, or 12.x
npm install -g tdd-guardTDD Guard needs to capture test results from your test runner. Choose your language below:
JavaScript/TypeScript
Choose your test runner:
Install the tdd-guard-vitest reporter in your project:
npm install --save-dev tdd-guard-vitestAdd to your vitest.config.ts:
import { defineConfig } from 'vitest/config'
import { VitestReporter } from 'tdd-guard-vitest'
export default defineConfig({
test: {
reporters: [
'default',
new VitestReporter('/Users/username/projects/my-app'),
],
},
})Install the tdd-guard-jest reporter in your project:
npm install --save-dev tdd-guard-jestAdd to your jest.config.ts:
import type { Config } from 'jest'
const config: Config = {
reporters: [
'default',
[
'tdd-guard-jest',
{
projectRoot: '/Users/username/projects/my-app',
},
],
],
}
export default configNote: For both Vitest and Jest, specify the project root path when your test config is not at the project root (e.g., in workspaces or monorepos). This ensures TDD Guard can find the test results. See the reporter configuration docs for more details:
Python (pytest)
Install the tdd-guard-pytest reporter:
pip install tdd-guard-pytestConfigure the project root in your pyproject.toml:
[tool.pytest.ini_options]
tdd_guard_project_root = "/Users/username/projects/my-app"Note: Specify the project root path when your tests run from a subdirectory or in a monorepo setup. This ensures TDD Guard can find the test results. See the pytest reporter configuration for alternative configuration methods (pytest.ini, setup.cfg).
PHP (PHPUnit)
Install the tdd-guard/phpunit reporter in your project:
composer require --dev tdd-guard/phpunitFor PHPUnit 9.x, add to your phpunit.xml:
<listeners>
<listener class="TddGuard\PHPUnit\TddGuardListener">
<arguments>
<string>/Users/username/projects/my-app</string>
</arguments>
</listener>
</listeners>For PHPUnit 10.x/11.x/12.x, add to your phpunit.xml:
<extensions>
<bootstrap class="TddGuard\PHPUnit\TddGuardExtension">
<parameter name="projectRoot" value="/Users/username/projects/my-app"/>
</bootstrap>
</extensions>Note: Specify the project root path when your phpunit.xml is not at the project root (e.g., in subdirectories or monorepos). This ensures TDD Guard can find the test results. The reporter saves results to .claude/tdd-guard/data/test.json.
Use the /hooks command in Claude Code:
- Type
/hooksin Claude Code - Select
PreToolUse - Before tool execution - Choose
+ Add new matcher... - Enter:
Write|Edit|MultiEdit|TodoWrite - Select
+ Add new hook... - Enter command:
tdd-guard - Choose where to save (Project settings recommended)
Tip: Also configure:
- Quick commands for
tdd-guard on/off - Session clearing for automatic cleanup on new sessions
- ESLint integration for automated refactoring support
TDD Guard supports two AI models for validation:
Uses your existing Claude Code authentication. No additional setup required.
Direct API access provides faster validation.
Setup:
- Get an Anthropic API key from console.anthropic.com
- Note: This is separate from Claude Code and requires Anthropic API credits
- Uses token-based billing instead of Claude Code subscription
- Set environment variables:
export TDD_GUARD_ANTHROPIC_API_KEY=your_api_key_here
export MODEL_TYPE=anthropic_apiNote: TDD Guard uses the TDD_GUARD_ANTHROPIC_API_KEY environment variable (not the standard ANTHROPIC_API_KEY) to avoid conflicts with other Anthropic SDK usage in your project.
As stated in the Claude Code Hooks documentation:
Hooks execute shell commands with your full user permissions without confirmation. You are responsible for ensuring your hooks are safe and secure. Anthropic is not liable for any data loss or system damage resulting from hook usage.
We share this information for transparency. Please read the full security considerations before using hooks.
TDD Guard runs with your user permissions and has access to your file system. We follow security best practices including automated security scanning, dependency audits, and test-driven development. Review the source code if you have security concerns.
- Not tested with multiple concurrent sessions in the same project
- Add support for more testing frameworks (Mocha, unittest, etc.)
- Add support for additional programming languages (Go, Rust, Java, etc.)
- Encourage meaningful refactoring opportunities when tests are green
- Add support for multiple concurrent subagents per project
See DEVELOPMENT.md for setup instructions and development guidelines.
Contributions are welcome! Feel free to submit issues and pull requests.
Contributors:
- Configuration Guide - Environment variables, model options, and troubleshooting
- Architecture Decision Records - Technical design decisions and rationale