Skip to content

Implement pre-commit hooks with Husky to run lint and tests #122

Description

@vlavrynovych

Problem

Currently, linting and test failures are only caught in CircleCI/GitHub Actions after code is pushed. This leads to:

  • Failed CI/CD builds that could have been prevented locally
  • Wasted time waiting for CI feedback
  • Additional commits to fix simple linting errors
  • Broken master/release branches if checks are bypassed

Recent example: Issue #80 had ESLint errors that passed locally but failed in CircleCI due to unused imports and variables.

Solution

Implement Option 2: Husky with Full Test Suite - run comprehensive checks before every commit.

Pre-Commit Hook Configuration

.husky/pre-commit:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

echo "🔍 Running pre-commit checks..."

# Run linting
echo "📝 Running ESLint..."
npm run lint || exit 1

# Run tests with coverage
echo "🧪 Running tests with coverage..."
npm run test:coverage || exit 1

# Check coverage threshold
echo "📊 Verifying 100% coverage..."
npx nyc check-coverage --lines 100 --branches 100 --functions 100 --statements 100 || exit 1

echo "✅ All pre-commit checks passed!"

Installation Steps

# Install Husky
npm install --save-dev husky

# Initialize Husky
npx husky init

# Create pre-commit hook (content above)

package.json update:

{
  "scripts": {
    "prepare": "husky install"
  }
}

Benefits

Catch errors immediately - Before they reach CI/CD
Maintain 100% coverage - Enforced on every commit
Consistent code quality - ESLint rules applied automatically
Faster CI/CD - Fewer failed builds
Team alignment - Everyone runs the same checks

Considerations

⚠️ Slower commits - May take 30-60 seconds per commit (acceptable for quality)
⚠️ Can be bypassed - Developers can use git commit --no-verify (document when this is acceptable)
⚠️ Local setup required - Team must run npm install after pulling

Acceptance Criteria

  • Husky installed and configured
  • Pre-commit hook runs ESLint
  • Pre-commit hook runs full test suite
  • Pre-commit hook verifies 100% coverage
  • Documentation updated with bypass instructions (--no-verify for WIP)
  • Team notified of new workflow
  • Tested on all platforms (macOS, Linux, Windows)

Related

Implementation Notes

  • Consider adding commitlint in future for commit message standards
  • Could add lint-staged later for faster staged-file-only checks
  • May want to add TypeScript type checking (tsc --noEmit) to hook

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions