Implement Pre-Commit Hook to check code coverage greater than 85 percent Nest.js

Implement check code coverage greater than 85 percent Pre-Commit Hook and then commit

🙋‍♂️ Shubham Verma    🗓 December 7, 2023


Implement Pre-Commit Hook to check code coverage greater than 85 percent Nest.js


Implement Pre-Commit Hook to check code coverage greater than 85 percent Nest.js

Ensuring code quality and test coverage is a vital aspect of software development.
Pre-commit hooks provide an excellent way to enforce certain checks before allowing code to be committed. In this article, we'll guide you through the process of implementing a pre-commit hook to check code coverage greater than 80% in a Nest.js project using Husky and Jest.

Step 1: Create a Repository and Clone

Start by creating a new repository on your preferred version control platform (e.g., GitHub, GitLab, or Bitbucket). Clone the repository to your local machine:

Step 2: Install Required Packages

Install the necessary packages for testing and code coverage:

Let's briefly explain each package:

Husky: A tool for managing Git hooks.
Jest: A JavaScript testing framework widely used for testing Node.js applications.
@nestjs/testing: The testing module for Nest.js applications.

Step 3: Set Up Husky with npm Package Scripts

Initialize Husky in your project by running the following command:
This command sets the `prepare` script in your `package.json` to run `npx husky install` during the preparation phase.

Step 4: Run npm Prepare

Execute the following command to trigger the prepare script:
This command installs Husky and sets up the necessary hooks in your Git repository.

Step 5: Add a Pre-Commit Hook for Code Coverage

Now, add a pre-commit hook that checks code coverage before allowing a commit. Run the following command:
This command adds a pre-commit hook that executes the test:cov npm script before every commit. The test:cov script runs Jest with coverage reporting.

Step 6: Update Package.json Scripts

In your `package.json` file, update the scripts section to include the `test:cov` script and the `prepare` scripts:
Add below code to check code coverage in package.json file:
Make sure your Jest configuration includes the appropriate settings for your project.

Here are the sample package.json:

Step 7: Try to Commit Your Changes

With the pre-commit hook in place, make changes to your code and attempt to commit them.

Let's check with 85%
The commit will be success if the code coverage is greater than 85%. Right now my code coverage is not 85%. So the commit will be failed.
For this experiment, change the value to 85 as:

Output:
Implement Pre-Commit Hook to check code coverage greater than 85 percent Nest.js

Implement Pre-Commit Hook to check code coverage greater than 85 percent Nest.js


You can see not allowing to commit, because the code coverage is less than 85%.
Let's check with 75%

For this experiment, change the value to 75 as:

Output:
Implement Pre-Commit Hook to check code coverage greater than 85 percent Nest.js

Implement Pre-Commit Hook to check code coverage greater than 85 percent Nest.js

You can see its allowing to commit, because the code coverage is greater than 75%.
The pre-commit hook will run the tests and check code coverage. If the coverage is below the specified threshold, the commit will be rejected.

Advantages of Pre-Commit Hooks for Code Coverage:


Early Detection of Issues:Pre-commit hooks catch issues and low test coverage early in the development process.
Automated Quality Assurance:Automated checks ensure that only code meeting quality and coverage standards is committed.
Consistency:Enforcing coverage thresholds ensures a consistent level of code quality across the project.

Conclusion:

Implementing a pre-commit hook for code coverage in Nest.js using Husky and Jest is a proactive approach to maintaining high-quality code.

By following the steps outlined in this article, you can seamlessly integrate this practice into your development workflow, ensuring that every commit meets the specified code coverage standards.

Related Keywords:

Implement Pre-Commit Hook to check code coverage

Pre-Commit Hook to check code coverage greater than 85 percent

Implement Pre-Commit Hook to check code coverage greater than 85 percent Nest.js

Pre-Commit Hook to check code coverage




Support our IDKBlogs team

Creating quality content takes time and resources, and we are committed to providing value to our readers. If you find my articles helpful or informative, please consider supporting us financially.

Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce high-quality content.

Thank you for your support!




Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.