How to check for leftover debug code with Pest

How to check for leftover debug code with Pest
Photo by Sigmund / Unsplash

We've all done it, committed to Git with a rouge dd() in our code, or perhaps some other function that's used for debugging. Fortunately, using PestPHP, there's a nice way of being able to catch these, so if you run tests as part of a CI/CD pipeline, then your test suite should fail if your codebase contains any. Let's have a look at the code.

So this is using the awesome Architecture Testing that comes with PestPHP, it makes writing tests like this an absolute breeze. In my app, I simply have the following test within a tests/ArchitectureTest.php file:

test('the codebase does not contain debug statements')    
->expect(['dd', 'dump', 'var_dump', 'ddd', 'print_r', 'die', 'print'])
->not->toBeUsed();

So this scans my codebase and looks for any of those functions. If it finds any, the test fails.