You need to enable JavaScript to run this app.

How Husky Git Hooks Enhance Code Quality and Fault Prevention?

Summary:

Learn to enhance code quality with Husky git hooks, customized for workflow optimization and fostering excellence in development teams. The "pre-commit" hook for formatting code, the "commit-msg" hook to enforce clear commit messages, and the "pre-push" hook to verify deployable code.

Category
Git
Time to read
10 min.
Published at
2023-7
How Husky Git Hooks Enhance Code Quality and Fault Prevention?

Introduction

Git hooks play a crucial role in maintaining code quality and preventing faults throughout the development process. One powerful tool for implementing these hooks is Husky, which enables developers to automate various quality checks before and after committing code. In this blog post, we'll explore how to use Husky to implement for essential git hooks: pre-commit, commit-msg, prepare-commit-msg, and pre-push to enhance your codebase quality and boost confidence in the shipping process.

Pre-Commit Hook

The pre-commit hook acts as the first line of defense, ensuring that every commit meets certain quality standards before it's added to the repository. In the .husky folder, we've implemented a pre-commit hook that leverages pretty-quick to format staged files automatically.

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

node_modules/.bin/pretty-quick --staged

This ensures that code formatting is consistent, contributing to a clean and maintainable codebase.

Commit Message Hook

Once the pre-commit hook succeeds, the commit message hook, powered by @commitlint/cli and @commitlint/config-conventional, checks the commit message itself against a predefined convention.

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

yarn commitlint --edit "$1"

Standardizing commit messages is crucial for clear communication within the development team and maintaining a meaningful version history. Here we have a basic example but for full assistance in establishing a commit message convention, refer to the Commit Message Convention blog.

Pre-Push Hook

The pre-push hook runs before the git push command, performing a series of checks to ensure the code is in a deployable state. This includes:

  • Test: Verifying that all tests pass successfully.
  • Lint: Ensuring the codebase adheres to clean code rules using tools like ESLint. Numerous ESLint plugins, such as eslint-config-airbnb and typescript-eslint, contribute to elevating code quality and maintaining consistency.
  • Build: Confirming the successful build of the application.
  • Docs: Generating automated documents, such as exported APIs using tools like Typedoc.
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
. "$(dirname -- "$0")/common.sh"

yarn test && yarn lint && yarn build && yarn docs

Running these checks before pushing code helps catch issues early, preventing faulty code from reaching the remote repository.

Prepare-Commit-Message Hook

In many cases, you might find the prepare-commit-msg hook unnecessary, but certain tools, like commitizen and @commitlint/prompt, rely on its functionality. We'll explore Commitizen in more detail in the Commit Message Convention blog.

Bypassing Hooks

While Husky provides a --no-verify git flag for bypassing verification, it should be used sparingly and only in critical situations. Common scenarios include addressing a severe production bug or when you are confident that your changes won't impact the checked commands, such as when making documentation-only changes.

Custom Enhancements

Husky is a flexible tool, allowing developers to tailor hooks to their specific needs. Consider integrating custom checks that align with your project's unique requirements. For instance, you might incorporate security scans, dependency audits, or other domain-specific validations.

Conclusion

By implementing Husky git hooks efficiently, developers can optimize their workflows and elevate the quality of their codebase. From enforcing formatting consistency to standardizing commit messages and running comprehensive tests and builds, Husky empowers teams to ship confidently and minimize the risk of faults. This will not only contribute in an improved development process but also foster a culture of excellence within your team.

Favourite Books
BooksPoems
Favourite Songs
PlaylistsArtists
Favourite Shows
AnimationsSeriesAnime