Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ tdd-guard Public
forked from nizos/tdd-guard

A Claude Code hook that enforces Test-Driven Development principles by monitoring agent actions in real-time and blocking violations before they occur

License

Notifications You must be signed in to change notification settings

lytv/tdd-guard

 
 

Repository files navigation

TDD Guard

npm version CI Security License: MIT

TDD enforcement for Claude Code.

Overview

TDD Guard monitors file operations in real-time and blocks changes that violate TDD principles. It provides immediate feedback when violations occur, helping maintain disciplined development practices without manual oversight.

The tool intercepts Write, Edit, and MultiEdit operations before execution, examining them against the current development state. Each validation considers the file path, intended modifications, current todo list, and latest test results to determine whether changes follow proper TDD practices.

Core Benefits

  • Eliminates manual TDD enforcement: No need to remind agents about writing tests first or following TDD cycles
  • Preserves context window: Removes lengthy TDD instructions from CLAUDE.md, freeing space for domain-specific guidance
  • Maintains development flow: Works silently until a violation occurs, then provides clear corrective feedback

Technical Features

  • Multi-model Support: Choose between Claude Code CLI or Anthropic API
  • Context Aggregation: Combines file modifications, todo states, and test results for accurate validation
  • Dynamic Instructions: Tailored prompts for Write, Edit, and MultiEdit operations
  • Task-Based Enforcement: Automatically adjusts TDD rules based on task complexity and type

Installation

npm install --save-dev tdd-guard

Quick Start

1. Environment

If you do not have Claude installed locally, create a .env file:

USE_SYSTEM_CLAUDE=true

This tells TDD Guard to the System Claude from PATH instead of ~/.claude/local/claude.

2. Hook Setup

Configure TDD Guard using the /hooks command:

  1. Type /hooks in Claude Code
  2. Select PreToolUse - Before tool execution
  3. Choose + Add new matcher...
  4. Enter: Write|Edit|MultiEdit|TodoWrite
  5. Select + Add new hook...
  6. Enter command: tdd-guard
  7. Choose where to save (Project settings recommended)

3. Test Reporter

Add to your vitest.config.ts:

import { VitestReporter } from 'tdd-guard'

export default defineConfig({
  test: {
    reporters: ['default', new VitestReporter()],
  },
})

For advanced configuration options, see the Configuration Guide.

Security Notice

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.

Known Limitations

  • Not tested with multiple subagents working simultaneously
  • Test output context only available for Vitest

Roadmap

  • Encourage meaningful refactoring opportunities when tests are green
  • Add support for other testing frameworks (Jest, Mocha, etc.)
  • Improve handling of concurrent subagents

Contributing

Contributions are welcome. Feel free to submit issues and pull requests.

Documentation

License

MIT

About

A Claude Code hook that enforces Test-Driven Development principles by monitoring agent actions in real-time and blocking violations before they occur

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 95.7%
  • JavaScript 4.3%