> For the complete documentation index, see [llms.txt](https://gitdeveloperguide.solomonmarvel.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitdeveloperguide.solomonmarvel.com/advanced-git-features/advanced-git-features-practice-scenarios/using-git-hooks-for-automated-testing.md).

# Using Git Hooks for Automated Testing

#### <mark style="color:blue;">Scenario</mark>

In this scenario, we will explore how to set up a pre-commit hook to automatically run test scripts, ensuring that code changes meet the required quality standards.

#### **Addressing the scenario**

Imagine you are working on a project where multiple developers collaborate, and you want to enforce a rule that requires running automated tests before committing code. By incorporating a pre-commit hook, you can automate this process and ensure that code changes are thoroughly tested.

#### **Setting up the pre-commit hook**

To begin, navigate to the root directory of your Git repository and locate the `.git` directory. Inside the `.git` directory, you will find a subdirectory called `hooks`. This directory contains sample hook scripts with the `.sample` extension. We will utilize the `pre-commit` hook for our scenario.

First, create a file named `pre-commit` (without any extension) in the `hooks` directory. You can use the command-line interface or a text editor to create the file. Make sure the file is executable by running `chmod +x .git/hooks/pre-commit`.

#### **Writing the pre-commit hook script**

Open the `pre-commit` file in a text editor and start writing the script. The pre-commit hook should contain the necessary commands to run your automated test scripts. For example, if you are using a testing framework like Jest for a JavaScript project, the script might look like this:

```bash
#!/bin/sh
echo "Running automated tests..."
npm test
exit $?
```

The script begins with a shebang (`#!/bin/sh`), which specifies the interpreter to use. The subsequent lines execute the desired commands. In this case, it displays a message and runs the `npm test` command, which triggers the automated tests. Finally, `exit $?` captures the exit code of the test command and exits the script with the same code.

#### Testing the pre-commit hook:&#x20;

Save the `pre-commit` file and test the hook by making changes to your code and attempting to commit. When you run `git commit`, the pre-commit hook script will be executed automatically before the commit is created. If the tests fail, the hook will prevent the commit from proceeding, allowing you to fix the issues and rerun the tests.

It is essential to ensure that your test scripts have appropriate exit codes. If the tests pass, the hook script will exit with a code of 0, allowing the commit to proceed. If the tests fail or encounter an error, the hook script should exit with a non-zero code, preventing the commit.

#### Benefits of using Git hooks for code quality

*Incorporating Git hooks into your workflow offers several advantages:*

* **Code quality enforcement**: By running tests or other code quality checks automatically, Git hooks provide an additional layer of quality control, preventing code with issues from being committed.
* **Early bug detection**: Running tests before committing code helps catch bugs early in the development process, making it easier to identify and fix issues.
* **Consistent workflows**: Git hooks ensure that all team members follow the same processes and standards, fostering consistency and reducing the chances of overlooking critical steps.
* **Time and effort savings**: Automated testing through Git hooks saves developers time and effort by eliminating the need for manual testing before each commit.

{% hint style="success" %}
By leveraging Git hooks for automated testing, you can significantly improve code quality and streamline the development process.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gitdeveloperguide.solomonmarvel.com/advanced-git-features/advanced-git-features-practice-scenarios/using-git-hooks-for-automated-testing.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
