Skip to content

hookTesting

Remind Run Tests

A Claude Code hook that holds back the end of a turn once, asking the agent to run the test suite when source files changed since the last commit.

npx workspacetemplates add remind-run-tests --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file15 lines · 1.2 KBFile
{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{let j={};try{j=JSON.parse(d)||{}}catch(e){process.exit(0)}const i=j.tool_input||{};const q=String.fromCharCode(34),sq=String.fromCharCode(39),bs=String.fromCharCode(92),nl=String.fromCharCode(10);if(j.stop_hook_active)process.exit(0);const r=require('child_process').spawnSync('git',['status','--porcelain','-uall'],{cwd:j.cwd||process.cwd(),encoding:'utf8'});if(r.status!==0||!r.stdout)process.exit(0);const fl=r.stdout.split(nl).filter(Boolean).map(l=>l.slice(3).trim().split(' -> ').pop().split(q).join(''));const src=fl.filter(f=>/[.](js|jsx|ts|tsx|mjs|cjs|py|go|rs|java|kt|rb|php|cs|swift|c|cpp|h|vue|svelte)$/.test(f)&&!/([.](test|spec)[.]|__tests__|_test[.]|(^|[/])tests?[/])/.test(f));if(src.length===0)process.exit(0);console.error('Before you finish: '+src.length+' source file(s) changed since the last commit ('+src.slice(0,5).join(', ')+'). Run the project test suite, report the result, then stop.');process.exit(2)})\"",
            "timeout": 15
          }
        ]
      }
    ]
  }
}

What it does

Agents sometimes finish a change and declare victory without running anything. This Stop hook asks git which files differ from the last commit, ignores tests and non-code files, and when real source files remain it sends the agent back to run the suite.

It speaks only once per stop, because it respects the stop_hook_active flag, so there is no endless loop. Outside a git repository, or with a clean tree, it stays silent.

Example

you › Rename the price helper and update its callers.

agent › When the agent tries to stop with four modified .ts files, the hook asks it to run the tests first. It does, reports the result, then finishes.

Questions

How does it avoid looping forever?
When the agent has already been kept going by this hook, Claude Code sets stop_hook_active and the script exits without a message.
Will it notice that tests already ran?
No, it only compares files with the last commit. The reminder costs one extra turn and the agent can answer it in a line.
  • Warn Skipped Tests

    Hookjavascript

    Free
  • Contract Testing

    SkillTesting

    Free
  • E2E Test Design

    SkillTesting

    Free
  • Flaky Test Diagnosis

    SkillTesting

    Free