1. 29
    Run Linting, Tests and Prettier in git Hooks with Husky
    5m 41s

Run Linting, Tests and Prettier in git Hooks with Husky

Andy Van Slaars
InstructorAndy Van Slaars

Share this video with your friends

Send Tweet

With the ability to run unit tests, automatic code formatting and linting, we should avoid ever committing code that does not adhere to our project standards or breaks our tests. In this lesson, we'll install and configure the Husky npm package and enforce these checks before a git commit is allowed.

Felipe
Felipe
~ 2 years ago

In the test file from the Transcript says 'if'. if('Fails', () => { expect(true).toBe(false) }) Salute.

Miguel Palomera
Miguel Palomera
~ 2 years ago

Due to I am using google prettier setup eslint was overriding the prettier output hence I was unable to commit. The solution was to use this package

npm i -D eslint-config-prettier

Then, add eslint-config-prettier to the "extends" array in your .eslintrc file. Make sure to put it last, so it gets the chance to override other configs.

{ "extends": [ "some-other-config-you-use", "prettier" ] }

cfsnsalazar
cfsnsalazar
~ 2 years ago

I am getting this error when trying to set up the pre-commit hook :

internal/modules/cjs/loader.js:626
    throw err;
    ^

Error: Cannot find module './v1'
Require stack:
- /Users/cfsnsalazar/node_modules/npm/node_modules/uuid/index.js
- /Users/cfsnsalazar/node_modules/npm/lib/utils/metrics.js
- /Users/cfsnsalazar/node_modules/npm/lib/npm.js
- /Users/cfsnsalazar/node_modules/npm/bin/npm-cli.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:623:15)
    at Function.Module._load (internal/modules/cjs/loader.js:527:27)
    at Module.require (internal/modules/cjs/loader.js:681:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/Users/cfsnsalazar/node_modules/npm/node_modules/uuid/index.js:1:10)
    at Module._compile (internal/modules/cjs/loader.js:774:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at Module.load (internal/modules/cjs/loader.js:641:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:681:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/cfsnsalazar/node_modules/npm/node_modules/uuid/index.js',
    '/Users/cfsnsalazar/node_modules/npm/lib/utils/metrics.js',
    '/Users/cfsnsalazar/node_modules/npm/lib/npm.js',
    '/Users/cfsnsalazar/node_modules/npm/bin/npm-cli.js'
  ]
}
husky > pre-commit hook failed (add --no-verify to bypass)```
cfsnsalazar
cfsnsalazar
~ 2 years ago

If I do not use my script , the pre-commit hook works "pre-commit": "eslint ./"