-
Notifications
You must be signed in to change notification settings - Fork 1
Simple Git Hooks with Create React App ESLint and Husky
First step is configuring eslint for you project. Only precondition is having Node installed on your machine.
-
Install eslist globally by running "npm install -g eslint"
-
Navigate to src/ directory, which is directory where package.json file is located.
-
run "eslint --init"
Answer question about your eslint usage, framework you are using, code style etc. Set of questions is changing over time as eslint evolves so i'm not gonna post them here, since in a few months they will probably be outdated. You can play if safe and choose all defaults. -
Install eslint plugin for react "npm install -g eslint-plugin-react"
-
Running init script created eslint config file in your src/ directory called .eslintrc.* ( * depending on config format you choose when answering questions on step 3, i recommend json format ) There are few customization we need to do in .eslintrc.* file
- In "env": section add jest , not adding jest will make eslint error out on jest specific functions
like describe and it:
"env": {
"browser": true,
"es6": true,
"jest": true
}
- In "extends": section add "eslint:recommended" and "plugin:react/recommended" :
"extends": [
"standard",
"eslint:recommended",
"plugin:react/recommended"
],
- Add any custom rules you want/need in "rules": section, here is the list off all
eslint rules, bellow is an example off adding rule that semicolon is mandatory:
"rules": { "semi": [2, "always"] } - In "env": section add jest , not adding jest will make eslint error out on jest specific functions
-
Now you should be set and you can run eslint on your project. Navigate to src/ directory, open terminal and run
eslint src/**/*.js
You should see number of errors and warnings, fix them by using--fixflag:eslint --fix src/**/*.js
This will fix almost all errors and warnings, if there are any left you have to manually fix them.
That should be really easy since eslint gives you exact line and descriptive/googleable error about whats wrong.
One example that you have to manually fix is serviceWorker.js file that create-react-app creates, it will have few console related errors that you can fix by adding/* eslint-disable no-console */on top of the serviceWorker.js file. -
You don't really want to always run linter manually from terminal, add npm scripts that run linter:
Open package.json file, in "scripts": section add new eslint scripts// package.json "scripts": { // ... "lint": "eslint src/**/*.js", "lint:fix": "eslint --fix src/**/*.js" },You can run these scripts with npm by running: npm run lint and npm run lint:fix
You should be all set on ESLint at this point, right now it's really easy to set up Git Hooks using Husky.
-
Install Husky: navigate to src/ directory, open terminal and run "npm install husky --save-dev"
-
Add "husky": section in package.json
// package.json
{
"husky": {
"hooks": {
"pre-commit": "npm run lint",
"pre-push": "npm run test"
}
}
}
-
Now we have Git Hooks for commit and push actions. Every time you run
git commitHusky will run "pre-commit" script so it will run "npm run lint", it will only let you commit your changes if validation passes.
Same goes forgit pushHusky will run "pre-push" script that runs "npm run test" and lets you push only if all tests are passing. -
Optional What if we want to run both test and lint scripts pre commit or pre push?
We can do that by adding new script in package.json:
// package.json
{
"scripts": {
...
"test:all": "set CI=true && react-scripts test && npm run lint",
}
}
Setting CI=true is for making sure jest runs tests only ones and not run them in watch mode, since watch mode does not make sense for git hooks. Syntax above is for windows, on mac it should be something like:
"test:all": "CI=true react-scripts-ts test && npm run lint"
Now all we need to do is pass new script in pre-commit or pre-push hook in package.json
// package.json
{
"husky": {
"hooks": {
"pre-commit": "npm run lint", // only lint on commit
"pre-push": "npm run test:all" // run new script on push
}
}
}