Lesson 7 of 10
Husky + lint-staged - Pre-Commit Quality Gates
Husky runs ESLint and Prettier automatically before every commit. lint-staged ensures only the changed files are checked - making the check fast (seconds, not minutes) even in large codebases.
Husky + lint-staged Setup
npm install --save-dev husky lint-staged
# Initialize Husky
npx husky init
# .husky/pre-commit
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx lint-staged
# package.json - lint-staged config
{
"lint-staged": {
"*.{ts,tsx}": [
"eslint --fix", // auto-fix what can be fixed
"prettier --write", // format
"jest --bail --findRelatedTests --passWithNoTests" // run tests for changed files only
],
"*.{json,md}": [
"prettier --write"
]
}
}
# Result: every commit is guaranteed to:
# - Have no ESLint errors
# - Be formatted by Prettier
# - Not break tests for changed files
# Developer doesn't have to remember to run these manuallyTip
Tip
Practice Husky lintstaged PreCommit Quality Gates in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Preventing broken code commits with pre-commit type checking, linting, and tests
Practice Task
Note
Practice Task - (1) Write a working example of Husky lintstaged PreCommit Quality Gates from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Husky lintstaged PreCommit Quality Gates is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?