From what I understand, I don't think transactions can ever work when using dusk, as each browser request in dusk creates a separate instance of your laravel app.
Previously, phpunit would create a new application in memory as part of the process (in the setUp
/ createApplication
method), then test against that testing application, then destroy it and set up the next one. As such, the transactions can be wrapped around (or just inside) the create and destroy parts of that application before it starts up a new database connection for the next test.
With dusk, it's real end-to-end testing (including a browser, faked user interaction, the routing on your local machine, etc.), which means it is not all contained within the environment that your tests are running in, like they usually are in phpunit.
Dusk does the following:
- Copies your
.env.dusk.*
and launches the chromedriver (or whatever selenium-like thing you use)
- Triggers a phpunit shell command (ie. new command, new process)
- The phpunit command runs your dusk tests, which each open a browser window and makes requests (each request starting a new php-fpm and php process (for nginx)) - just as though you were making those requests yourself. Each of them have separate connections to the db, and so cannot interact with each others' transactions.
It's also worth noting that the DatabaseTransactions
trait is in the Foundation package, not the Dusk package, so it's not build / packaged with Dusk in mind.
This also explains why in-memory sqlite doesn't work with dusk, as one process does not have access to another process' memory.