Hanabi Logo

Discover Hana AI

Hana, Google Chat integrated versatile AI assistant developed by Hanabi Technologies
Hanabi Logo

Kasavanahalli, Bengaluru, India, 560035

sales@hanabitech.com

HELLO

BehanceGmailInstagramLinkedInMedium

Mastering Code Quality: A Guide to Setting Up Husky Pre-CommitHooks

A picture relating to husky precommit hook

Maintaining high code quality and consistency is essential for any software development project. Manual code reviews and tests can be time-consuming and error-prone, leading to potential bugs and issues slipping into the codebase. To address these challenges, developers leverage Husky pre-commit hooks—a powerful automation tool that ensures code quality by running checks and tests before each commit.

In this blog, we will explore the step-by-step process of setting up Husky pre-commit hooks in our project and show the benefits through relevant code examples.

What are Pre-Commit Hooks and Why Use Husky?

Pre-commit hooks are scripts or commands that run before a commit is finalized. They enable developers to perform various checks, such as code linting, formatting, and running tests, to ensure that the committed code meets specific quality standards. Using pre-commit hooks is a best practice as it helps catch issues early, keeps the codebase consistent, and promotes collaboration.

Husky is a popular tool that simplifies the setup and management of pre-commit hooks. It is used in the JavaScript ecosystem and integrates seamlessly with Git repositories. Husky empowers developers to automate pre-commit checks, enhancing code quality and productivity.

Step-by-Step Guide: Setting up Husky Pre-Commit Hooks

Step 1: Initialize Your Project

Assuming you have a Git repository for your project, start by navigating to the root directory of your project in the terminal.

Step 2: Install Husky

To begin using Husky, you need to install it as a development dependency in your project. Use npm or yarn to add Husky to your project:

Step 3: Configure the Pre-Commit Hook

After installing Husky, you need to configure the pre-commit hook in your project's package.json file. The pre-commit hook will define the tasks you want to run before each commit.

Let's consider two essential pre-commit tasks: code linting using ESLint and running tests with Jest.

Task 1: Code Linting with ESLint

ESLint is a popular JavaScript linter that helps identify and fix common coding issues. To set up ESLint and Husky, follow these steps:

  • Install ESLint as a development dependency:
  • Create an ESLint configuration file in your project's root directory:
  • Now, let's configure Husky to run ESLint as a pre-commit task:

Task 2: Running Tests with Jest

Jest is a popular testing framework for JavaScript projects. To integrate Jest and Husky, follow these steps:

  • Install Jest as a development dependency:
  • Create your test files using the .test.js extension. For example, if you have a module named calculator.js, create a test file named calculator.test.js.

  • Now, let's configure Husky to run Jest tests as a pre-commit task:

Step 4: Commit and Test

With the Husky pre-commit hooks set up, it's time to test the process. Make some changes to your code, add the changes to the staging area using git add, and then try to commit using git commit. Husky will trigger the pre-commit hooks, running the defined tasks (in this case, ESLint and Jest tests).

If any issues are found, Husky will prevent the commit from happening, allowing you to fix the problems before proceeding.

Importance of using Husky Pre-Commit Hooks

1. Early Bug Detection

Husky pre-commit hooks enable developers to catch bugs and errors before they become part of the codebase. By running automated tests and checks before committing code, potential issues can be identified and addressed promptly, reducing the chances of introducing bugs into the main branch.

2. Code Consistency

Consistent code formatting and styling contribute to better readability and maintainability of the codebase. Husky pre-commit hooks can enforce coding standards, ensuring that all team members follow the same guidelines, leading to more consistent code and a smoother collaborative development process.

3. Improved Productivity

Manually running code checks and tests before each commit can be time-consuming and prone to human error. With Husky pre-commit hooks, these tasks are automated, saving developer's time and effort. Automation boosts productivity by allowing developers to focus on writing code while knowing that quality checks are automatically taken care of.

4. Immediate Feedback

Immediate feedback is essential in agile development. When a developer commits code, Husky triggers the pre-commit hooks, providing instant feedback on any issues found. This immediate feedback loop allows developers to address problems promptly, making it easier to fix mistakes and keep the codebase clean and functional.

Conclusion

Husky pre-commit hooks are a game-changer for improving code quality and ensuring consistency in our software development projects. By automating code checks and tests before each commit, Husky helps us catch issues early, maintain coding standards, and collaborate effectively with our team. By embracing Husky in our workflow, we get the benefits of automated code quality checks in our projects.