Version control checks

Introduction#

This article focuses on the crucial role of running tests on every commit to catch bugs as early as possible. We'll pay particular attention to Husky 2, a tool that simplifies the process of enabling Git hooks right from your package.json file.

Husky allows developers to add automated actions, such as running tests or linters, before every commit (pre-commit) or push (pre-push). This helps ensure the codebase remains stable, enhancing the overall quality of the software. Let's delve into how to set up and utilize Husky for your projects.

Setting Up Husky#

To implement Husky in our project, we first need to add it as a dependency. This can be done by running the following command in the terminal:

With Husky added as a dependency, we then need to configure it in the package.json file by adding configurations for Husky. An example of a basic Husky configuration in package.json is shown below:

This lesson preview is part of the Pain Free Mocking with Jest course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Pain Free Mocking with Jest, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Pain Free Mocking with Jest
  • [00:00 - 00:10] Keeping tests running on commits is important to catch bugs early. The Husky 2 makes this easy by enabling Git hooks right from your packet.json file.

    [00:11 - 00:18] To get started, let's add Husky as a dependency to our project. Open your terminal and run this command.

    [00:19 - 00:37] We added Husky as a def dependency to our project. Next, we need to configure Husky by running some other commands.

    [00:38 - 03:06] Having run this command, open your packet.json file and in there we need to add some configurations for Husky. We added configuration for Husky, which accepts an object and in the hooks we added a pre-commit which will run our test. We can also add a pre-push which will run every time we push our local changes to the upstream branch. With pre-commit, it will run npm test every time we try to make a git commit. While helpful for quality, we don't want hooks slowing down developer workflow too much. As a tip, only include unit tests in the pre-commit hook since they run very fast and provide instant feedback. Other practices I've seen is to run lintas in the pre-commit hook and run unit tests in the pre-push hook. Let's open our code editor and make a few changes and see how Husky works. I added an assertion that I expect to fail. Expect true to be false, which definitely will fail. I will try to add this to git and make a commit. And we have a failing test which is expected true to be false. When we try to make our commit, the pre-commit hook run our test and if the test fails, the commit will not be successful. While helpful for quality, we don't want hooks slowing down developer workflow too much. As a tip, I advise only including unit tests in pre-commit hook since they run very fast and provide instant feedback. Other practices I've seen is to run lintas in the pre-commit hook and run unit tests in the pre-push hook. Developers have the flexibility to bypass the commit hooks for urgent changes using dash dash no-verify if needed as well. And as you can see, we were able to bypass the it-hook using dash dash no-verify. The CI pipeline that is continuous integration and handle running the lengthier integration and it-to-eat test so commits aren 't blocked or necessary.

    [03:07 - 03:22] In our next lesson, I'll demonstrate setting up a basic CI pipeline using GitHub actions. Combining local Husky hooks with a robust CI pipeline will ensure only stable high quality code, makes it into our repo and production environment.