Make Laravel Dusk tests more reliable by disabling animations

Published on

Animations can cause Dusk tests to fail randomly. For example, interacting with an element that is inside a modal might not work if the modal is still playing a slide-in animation.

A nice solution for flaky tests caused by animations is to disable animations while running Dusk tests. Put this in the <head> or your Blade template:

@if(is_running_dusk_tests())
    <style>
        * {
            animation: none !important;
        }
    </style>
@endif

For Dusk specific code I like using a is_running_dusk_tests() helper function. This function lets me quickly find all places where I make an exception when running Dusk.

function is_running_dusk_tests(): bool
{
    return app()->environment('testing-dusk');
}

Make sure to set the correct environment in your .env.dusk file:

APP_ENV="testing-dusk"