How to Clear Session Data When Testing with Laravel Dusk

Each time you run a browser test in Laravel Dusk you will share session and cookie data with other tests in your file. If you’re trying to test a login form or something similar you may run into issues where one test is using session data from a different test.

Here’s how you can clear and delete the session data between tests to ensure that each test is sanitized.

Open up /tests/DuskTestCase.php and create or update the tearDown() method with the following.

This will clear cookie data for every test you create. If you’d rather only do it for a single file or test, you can either move the tearDown() method into the test file you want to update or create a different method and call it inside of individual Laravel Dusk tests.