Laravel Dusk ignores .env.dusk and .env.dusk.local (Using Valet and Laravel 5.5)

1.4k views Asked by At

I'm trying to run Laravel Dusk and I need to use a test database. When I check the screenshot it says that the database doesn't exist. Problem is that is the database defined on .env and not the one on .env.dusk ... I've tried to rename the file to .env.dusk.local and still no luck. What am I doing wrong?

4

There are 4 answers

2
Andre Madarang On

Try clearing your cache? I remember having similar issues when I first started using dusk.

php artisan cache:clear
0
free2idol1 On

Same issue... The problem is you forgot to create config cache file for your testing env so dusk will use your current local config cache file instead. Here is the fix:

1/ duplicate .env to .env.dusk.testing and edit the following:

APP_ENV=testing

DB_DATABASE=halfwish_test #I use different MYSQL database for testing.

2/ run this cmd:

$ php artisan config:cache --env=dusk.testing && php artisan clear //clear compiled services and packages files and cache config file.
$ php artisan migrate:fresh --env=dusk.testing && php artisan seed --env=dusk.testing //this is migrating and seeding demo data to DB.

Now you can run php artisan dusk.

0
Precastic On

I was able to get this to work by following these steps:

  1. Remove .env.testing
  2. Run php artisan dusk (This will create phpunit.dusk.xml).
  3. Add the following to the end of this newly created file
<phpunit>
    ...

    <php>
        <server name="APP_ENV" value="local"/>
    </php>
</phpunit>
  1. You can now restore .env.testing which can still be used for phpunit testing.

ORIGINAL ANSWER

I found that if you remove .env.testing it then uses .env.dusk.local. It seems that .env.testing always take precedence.

This does not solve the problem of using a separate .env for unit tests & dusk; which is useful when running unit tests in memory and dusk tests using sqlite.

0
Lam Kwok Shing On

I got the same problem. Lately I figured out that the .env file is being cached before dusk start.

I resolved it by adding

$this->artisan('config:cache');

into the createApplication() function in CreateApplication.php. It works :)

But then, if I finished the dusk test, I must run

php artisan config:cache

to get the original server running.

Edit:

later I tried simply call the

php artisan config:clear

also prevent the application caching the env variable. (Much simpler and faster)