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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With