TDD Guard

nizos/tdd-guard

Blocks non-TDD file writes.

CLAUDE.md

TDD Guard

Project Goal

TDD Guard is a Claude Code hook that enforces Test-Driven Development by intercepting file operations. When Claude Code attempts to edit or write files, TDD Guard:

  1. Captures: Intercepts Edit, MultiEdit, and Write operations
  2. Analyzes: Examines test results, file paths, and code changes
  3. Validates: Checks TDD compliance using an AI model
  4. Blocks: Prevents operations that skip tests or over-implement
  5. Guides: Explains violations and suggests corrections

This automated enforcement maintains code quality without cluttering prompts with TDD reminders.

Development Workflow

Commit Guidelines

  • Atomic commits: Each commit represents one logical change with its tests
  • Test and implementation together: Never separate tests from the code they test
  • Explain why, not what: Commit messages should explain the reason for the change
  • Conventional format: Use prefixes to categorize changes: feat, fix, refactor, test, chore, docs

Example: feat: add network request filtering to reduce noise in captured data (explains why, not just what)

Project Structure

The codebase is organized with core functionality in src/ and language-specific reporters:

reporters/                        # Language-specific test reporters
├── go/                           # tdd-guard-go - Go test reporter
├── jest/                         # tdd-guard-jest - Jest reporter (npm)
├── phpunit/                      # tdd-guard/phpunit - PHPUnit reporter (composer)
├── pytest/                       # tdd-guard-pytest - Pytest reporter (pip)
├── rspec/                        # tdd-guard-rspec - RSpec reporter (gem)
├── rust/                         # tdd-guard-rust - Rust test reporter (crates.io)
├── storybook/                    # tdd-guard-storybook - Storybook test-runner reporter (npm)
├── test/                         # Shared test artifacts and integration tests
└── vitest/                       # tdd-guard-vitest - Vitest reporter (npm)

src/                              # Main CLI application
├── cli/                          # Hook entry point and context builder
├── config/                       # Configuration management
├── contracts/                    # Types and Zod schemas
├── guard/                        # Guard enable/disable management
├── hooks/                        # Claude Code hook parsing and processing
├── linters/                      # ESLint integration for code quality
├── processors/                   # Test result and lint processing
├── providers/                    # Model and linter client factories
├── storage/                      # Storage abstractions
├── validation/                   # TDD principle validation
│   ├── validator.ts              # Sends context to AI model and parses response
│   ├── context/                  # Formats operations for AI validation
│   ├── prompts/                  # TDD validation rules and AI instructions
│   └── models/                   # Claude SDK and Anthropic API clients
└── index.ts                      # Package entry point

test/                             # Main test suite (hooks, integration, utils)
docs/                             # Documentation (ADRs, configuration, etc.)

Architecture

TDD Guard is organized as a TypeScript project with integrated language-specific reporters:

  • src/: Core functionality including contracts, config, storage, and validation
  • reporters/: Language-specific test reporters (go, jest, phpunit, pytest, rspec, rust, storybook, vitest)
  • test/: Comprehensive test suite with integration tests and utilities

Testing

Guidelines

  • Use test helpers: Extract setup logic into helper functions placed at the bottom of test files
  • Use test factories: Always use factories from test/utils/ instead of creating data inline
  • Group tests effectively: Use describe blocks and beforeEach for common setup
  • Keep tests concise: Keep as little logic in the tests themselves as possible

Commands

npm run build             # Build main package and workspace reporters (jest, storybook, vitest)
npm run test              # All unit tests and base integration tests
npm run test:unit         # Fast unit tests only
npm run test:reporters    # Test all reporter implementations
npm run lint              # Check code style and quality
npm run format            # Auto-format code with Prettier
npm run checks            # Run all checks: typecheck, lint, format, and test

Key Design Principles

  • Interface-driven: Core functionality defined by interfaces (Storage, ModelClient)
  • Dependency injection: Components receive dependencies as parameters
  • Single responsibility: Each module has one clear purpose
  • Type safety: Comprehensive TypeScript types with runtime validation
README.md

TDD Guard

npm version npm downloads CI Security License: MIT

Automated Test-Driven Development enforcement for Claude Code.

Overview

TDD Guard ensures Claude Code follows Test-Driven Development principles. When your agent tries to skip tests or over-implement, TDD Guard blocks the action and explains what needs to happen instead.

Features

  • Test-First Enforcement - Blocks implementation without failing tests
  • Minimal Implementation - Prevents code beyond current test requirements
  • Lint Integration - Enforces refactoring using your linting rules
  • Customizable Rules - Adjust validation rules to match your TDD style
  • Flexible Validation - Choose faster or more capable models for your needs
  • Session Control - Toggle on and off mid-session

Getting Started

Requirements

  • Node.js 22+
  • A supported test framework (Vitest, Jest, Storybook, pytest, PHPUnit, Go, Rust, RSpec, Minitest)

Installation

Open Claude Code in your project and run:

  1. /plugin marketplace add nizos/tdd-guard
  2. /plugin install tdd-guard@tdd-guard
  3. /tdd-guard:setup

This adds the marketplace, installs the plugin, and configures the test reporter for your project. You may need to restart your terminal session or IDE extension for the setup skill to appear. For manual installation and configuration, see the installation guide.

Configuration

Security

TDD Guard hooks run with your user permissions. We maintain automated security scanning, dependency audits, and welcome source code review. See Claude Code's security considerations for more on hook safety.

Development

Contributing

Contributions are welcome! See the contributing guidelines to get started.

Contributors:

Roadmap

  • Expand language and test framework support
  • Validate file modifications made through MCPs and shell commands
  • Encourage meaningful refactoring opportunities when tests are green
  • Add support for multiple concurrent sessions per project

Support

  • Discussions - Ask questions and share ideas
  • Issues - Report bugs and request features

License

MIT