DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Utilizing Database Hooks Like a Pro in Node.js
  • Stop Writing Dialect-Specific SQL: A Unified Query Builder for Node.js
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • Integrating Node.js Applications With MCP Servers

Trending

  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  • How Retry Storms Crash API-Led Systems: Bounded Reliability Patterns for Distributed Architectures
  • Designing AI-Assisted Integration Pipelines for Enterprise SaaS
  • Stop Using the ATM-Didn’t-Kill-Jobs Story to Reassure Developers About AI
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Bitbucket Pre-Commit Hooks With Husky for Better Code Quality

Bitbucket Pre-Commit Hooks With Husky for Better Code Quality

BitBucket is Git Management solution from Atlassian. It is free for small teams up to 5 active team members and support all Git repository features.

By 
Tomasz J-ski user avatar
Tomasz J-ski
·
Aug. 20, 20 · Analysis
Likes (1)
Comment
Save
Tweet
Share
17.6K Views

Join the DZone community and get the full member experience.

Join For Free

BitBucket is Git Management solution from Atlassian. It is free for small teams up to 5 active team members and support all Git repository features. More about Bitbucket could be fount at their website https://bitbucket.org/ .

Pre-commit hooks are powerful functionality to execute own scripts after staging git changes but before commit them to the local repository. Thanks to this solution developers are able to execute code quality checks and code tests before the code officially lands at the repository. Pre-commit hook is not the only hook supported by Git. Other common hooks are:

  • applypatch-msg

  • pre-applypatch

  • post-applypatch

  • pre-commit

  • prepare-commit-msg

  • commit-msg

  • post-commit

  • pre-rebase

  • post-checkout

  • post-merge

  • pre-receive

  • update

  • post-receive

  • post-update

  • pre-auto-gc

  • post-rewrite

  • pre-push

But the most important from code quality and test perspective are pre-commit and pre-push ones.

Husky is node js tool written by Typicode and supporters as open source support for bad git commit prevention.Project homepage is https://github.com/typicode/husky and of course https://www.npmjs.com/package/husky.

Putting all together. The goal is to perform code quality checks and test before committing them to local code repository to ensure best possible code stability.

First step like always for npm libraries and tools is installation.

npm install husky –save-dev or npm i husky 

depends on your need.

Next step is adding configuration to package.json

JSON
 




x
15


 
1
// package.json
2
{
3

          
4
 "husky": {
5

          
6
 "hooks": {
7

          
8
 "pre-commit": "npm test",
9

          
10
"pre-push": "npm test"
11

          
12
}
13

          
14
  }
15
}



General approach is “name of the hook” : “script / command to be executed”.
And that’s all!

You have full support to pre-commit hook and can execute any bash script like npm test.

Of course husky is not the only tool that I’m using but this article focus only on husky. Typically for node js (and general JavaScript) development it is worth to add eslint and Prettier for checking the code and auto formatting tools.

Husky supports out of the box git event system – hooks and do not force developer to manually create them in Git folder structure. It is awesome supporting tool which keeps all needed configuration is standard package.json file which is always first step to investigate what node js application does.

Hook Node.js

Published at DZone with permission of Tomasz J-ski. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Utilizing Database Hooks Like a Pro in Node.js
  • Stop Writing Dialect-Specific SQL: A Unified Query Builder for Node.js
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • Integrating Node.js Applications With MCP Servers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook