Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

simnom's avatar

Dusk: CSRF mismatch error

Hey,

Just working through some more Dusk tests and I'm hitting the 'TokenMismatchException' error when a form is submitted.

The overall blade template includes the CSRF meta tag and I'm even specifying to not use Middleware within the test within the class as follows:

use Tests\DuskTestCase;

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class SubmitEntryTest extends DuskTestCase
{
    use WithoutMiddleware, DatabaseMigrations;

    ...
}

Anyone else having this issue or am I missing something simple?

Thanks

0 likes
3 replies
simnom's avatar

Thanks, but already checked that, the CSRF token is added using <meta name="csrf-token" content="{{ csrf_token() }}">.

Currently wondering if it's something to do with session storage with Dusk in the testing environment.

scuttlebyte's avatar

Having the same issue. For now, ~disabling middleware~ did the trick. Interested to see what the "real" solution is.

False alarm. It didn't seem to help disabling middleware.

Okay I am just an all-around moron. Conflated this issue with something else.

My Dusk test was failing because my input wasn't validating, but the field on which the validation failed wasn't displaying the message (a bug). So I attempted to re-create the issue in the browser and got the token mismatch exception and figured it was that. However, after reloading the page it looks like my session expired.

Hopefully your issue is as silly as mine, simnom. Good luck!

Please or to participate in this conversation.