Tips
Web Development
Test
Pest
Tips and Tricks

How to run Pint and Pest on Git hooks with Laravel Sail

by Antoni Putra, Fullstack Web Developer

Developers use Git hooks to run scripts automatically when a specific Git event occurs, such as committing code changes. Pint and Pest are popular testing frameworks for PHP applications. Pint is a PHP code style checker, and Pest is a PHP testing framework. This tutorial shows you how to set up Git hooks for Pint and Pest while using Laravel Sail.

Laravel Sail is a lightweight command-line interface (CLI) for running Laravel applications in a Docker container. It simplifies the development environment setup process by providing preconfigured Docker images that work out of the box.

Assuming you already have Pint and Pest installed, follow these steps to set up Git hooks with Laravel Sail:

1. Create pre-commit

Create a Git hook file Navigate to your Laravel application's root directory and create a new file called pre-commit in the .git/hooks/ directory:

touch .git/hooks/pre-commit

2. Make it executeable

Make the Git hook file executable Make the pre-commit file executable by running the following command:

chmod +x .git/hooks/pre-commit

3. Write the script

Open the Git hook file and add the following code Open the pre-commit file in a text editor and add the following code:

#!/bin/sh
echo "Running Pint ..."
./vendor/bin/pint

echo "Running Pest ..."
[ -f sail ] && bash sail pest || bash vendor/bin/sail pest

This code runs Pint and Pest before a Git commit is executed. It first runs Pint and then runs Pest using Laravel Sail if the sail script exists; otherwise, it runs Pest using the local vendor/bin/sail script.

Thats it guys! Now test the Git hook Save the changes to the pre-commit file and make a change to your code. Attempt to commit your changes, and you should see output similar to the following:

Running Pint ...
[OK] No syntax error found
Running Pest ...
PASS Tests: 2, Assertions: 2, Passed: 2, Skipped: 0, Incomplete: 0, Risky: 0, Error: 0, Failed: 0

If you encounter any issues, check that Pint and Pest are installed and that the paths in the pre-commit file are correct.

In conclusion, setting up Git hooks for Pint and Pest with Laravel Sail is a straightforward process that can save you time by automatically running tests before committing code changes. By following these steps, you can ensure that your code adheres to the coding standards and that your tests pass before pushing code changes to your repository.

About the Author:

Antoni, a full-stack web developer, finds joy in working with Laravel and Javascript.

Disclaimer:

The information provided in this article is based on the author's knowledge and experience as of the publication date. Technology and tools may have evolved since then. Always refer to the official documentation for the latest instructions and updates.