Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run tests before commit in create react app?

How to run jest in create react app before commit? I have found couple articles but they are too complicated , without explaining what to do step by step. Could you please explain me how to make it work

like image 683
Yerlan Yeszhanov Avatar asked Nov 26 '19 09:11

Yerlan Yeszhanov


People also ask

How do you run a test before a commit?

How to use: Invoke the Commit feature using Ctrl + K on Windows/Linux and ⌘ + K on macOS, then select the Commit options and check the Run Tests option, then select which test configuration you wish to run.

What is setupTests js in react?

setupTests. js file is needed. Like Jest and React Testing Library, Jest-DOM is installed with create-react-app. The setupTests. js file is importing the library into our app and giving us access to the matchers. The matcher that was used in our example test was the toBeInTheDocument() method.

What is husky used for in react?

That's where Husky comes in. Husky is a tool for managing Git hooks in JavaScript projects. Git hooks are scripts that Git runs before or after certain commands, such as commit or push . In the olden days, developers had to install a project's Git hooks on their machine manually.


1 Answers

You need to use husky package. Here is basic configuration (put it in package.json). It adds pre-commit hook to your git configuration.

"husky": {
  "hooks": {
    "pre-commit": "CI=true npm run test",
  }
}

You can also consider using lint-staged to lint files which you commit. You can see full configuration here.

like image 150
Dima Vishnyakov Avatar answered Oct 13 '22 14:10

Dima Vishnyakov