6. Kiro Hooks & Automation
← Back to Table of Contents | Previous: Kiro Configuration | Next: Terraform Infrastructure →
6.1 What Are Kiro Hooks?
Kiro hooks are automated actions triggered by IDE events. They enforce methodology rules without relying on developer discipline.
6.2 Essential Hooks
Hook 1: Run Tests on File Save
Ensures every code change is immediately validated by tests.
File: .kiro/hooks/test-on-save.hook.json
{
"name": "Run Tests on Save",
"version": "1.0.0",
"description": "Runs unit tests for the modified module when a source file is saved",
"when": {
"type": "fileEdited",
"patterns": ["src/**/*.ts", "src/**/*.js"]
},
"then": {
"type": "runCommand",
"command": "npm run test -- --run --reporter=verbose"
}
}
Hook 2: Lint on File Save
Enforces consistent code style across all Kiro agents.
File: .kiro/hooks/lint-on-save.hook.json
{
"name": "Lint on Save",
"version": "1.0.0",
"description": "Runs linter when source files are saved",
"when": {
"type": "fileEdited",
"patterns": ["src/**/*.ts", "src/**/*.js"]
},
"then": {
"type": "runCommand",
"command": "npx eslint --fix"
}
}
Hook 3: Verify Tests Exist Before Write
Reminds Kiro to generate tests whenever it writes code.
File: .kiro/hooks/verify-tests.hook.json
{
"name": "Verify Tests on Write",
"version": "1.0.0",
"description": "Reminds agent to include unit tests with every code change",
"when": {
"type": "preToolUse",
"toolTypes": ["write"]
},
"then": {
"type": "askAgent",
"prompt": "Before writing this code, confirm that corresponding unit tests will be created or updated. Every function must have tests. If you are writing source code, you must also write or update the test file."
}
}
Hook 4: Module Boundary Check
Prevents Kiro from modifying files outside its assigned module.
File: .kiro/hooks/module-boundary.hook.json
{
"name": "Module Boundary Check",
"version": "1.0.0",
"description": "Verifies write operations respect module ownership boundaries",
"when": {
"type": "preToolUse",
"toolTypes": ["write"]
},
"then": {
"type": "askAgent",
"prompt": "Check that this write operation targets only files within your assigned module. You must NOT modify files in other modules. If the change requires cross-module modification, create an interface in shared/ instead and flag it for human review."
}
}
Hook 5: Run Tests After Task Completion
Validates that all tests pass after a spec task is completed.
File: .kiro/hooks/test-after-task.hook.json
{
"name": "Run Tests After Task",
"version": "1.0.0",
"description": "Runs full test suite after completing a spec task",
"when": {
"type": "postTaskExecution"
},
"then": {
"type": "runCommand",
"command": "npm run test -- --run"
}
}
Hook 6: Terraform Validate on Save
For the infrastructure repo, validates Terraform files on save.
File: .kiro/hooks/terraform-validate.hook.json
{
"name": "Terraform Validate on Save",
"version": "1.0.0",
"description": "Validates Terraform configuration when .tf files are saved",
"when": {
"type": "fileEdited",
"patterns": ["**/*.tf"]
},
"then": {
"type": "runCommand",
"command": "terraform validate"
}
}
6.3 Hooks Summary
| Hook | Trigger | Action | Purpose |
|---|---|---|---|
| Test on Save | File edited (src/**) |
Run tests | Immediate test feedback |
| Lint on Save | File edited (src/**) |
Run linter | Code style consistency |
| Verify Tests | Pre-write tool | Ask agent | Enforce test generation |
| Module Boundary | Pre-write tool | Ask agent | Prevent cross-module edits |
| Test After Task | Post task execution | Run tests | Validate task completion |
| Terraform Validate | File edited (*.tf) |
Validate TF | Catch infra errors early |
← Back to Table of Contents | Previous: Kiro Configuration | Next: Terraform Infrastructure →