xBnLEg6Fo's avatar

Random Dusk failures, caching issue?

Hello,

I am using Laravel 6.18.14 and I'm currently facing big issues with Dusk. Sometimes the tests are not running at all, sometimes it helps to do

php artisan config:clear
php artisan cache:clear

I cannot really find a pattern in it, sometimes it works, sometimes not. I am using a Sqlite database in the database directory, the .env.dusk.local points to it and is copied before the actual test run starts as it should be. When the tests are not running, I am seeing all kinds of DB exceptions like

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: users 

in the logfile, so I assume it's a problem related to the database. Also I have the problem from time to time, that Dusk just uses my local dev DB for its testing instead of the configured Sqlite (also not nice because all data is gone then obviously). All in all it looks like some weird caching effect to me, because of its unpredictable behavior.

Has anyone seen this before and can point me to the right direction maybe?

Thanks.

0 likes
10 replies
bugsysha's avatar

I did have that issue with wrong database usage, but I do not recall how I've solved it. I'm leaving this post as a reminder to myself to check that if I can remember.

xBnLEg6Fo's avatar

Thanks, had to smile about your post. Maybe a little more information: I am running the application in a local Homestead Vagrant box. I am not using

php artisan serve

as from my understanding it is not necessary (correct me if I am wrong). It was initially a Laravel 4.x application which I upgraded to Laravel 6, means the configuration files in the config directory look a little different, but I assume they did all changes in a downward compatible way. The application itself is running without problems. The database connection looks like this:

        'dusk' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('dusk.sqlite')),
            'prefix' => '',
        ],

In the .env.dusk.local I am referring to it via

DB_CONNECTION=dusk
bugsysha's avatar

There is nothing wrong with laughing ;)

You can add listeners in your PHPUnit.

    <listeners>
        <listener class="Tests\BackupRestoreEnvironmentListener" file="./BackupRestoreEnvironmentListener.php"></listener>
    </listeners>
<?php

namespace Tests;

use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\TestSuite;

class BackupRestoreEnvironmentListener implements \PHPUnit\Framework\TestListener
{
    /**
     * A test suite started.
     */
    public function startTestSuite(TestSuite $suite): void
    {
	// delete screenshots
	// copy env files
    }

    /**
     * A test suite ended.
     */
    public function endTestSuite(TestSuite $suite): void
    {
	// restore env files
    }
}
xBnLEg6Fo's avatar

Hm I am not exactly sure how this TestListener helps me with the above problem?

xBnLEg6Fo's avatar

Alright, I will. But where can I find the phpunit.xml that is used by Dusk? I understood that it somehow uses an internal one.

bugsysha's avatar

I think it should be the same phpunit.xml file. At least it was the same file when I've used it last time.

xBnLEg6Fo's avatar

Nope, when I add this to phpunit.xml, it doesn't seem to be executed.

bugsysha's avatar

It worked for me. Who knows what have changed since then. Have you tried it on a clean install?

mtbossa's avatar

I'm having the same problem. Sometimes if fails, sometimes it doesn't. When it fails, it's because it tries to connect to the wrong database. Meaning: all other environment variables in my .env.dusk.local are correct, for example APP_URL, APP_DOMAIN, etc., but DB_DATABASE is wrong, it's still using the one from .env. It's like this is the only environment variable that is "cached". I tried running PHP artisan config:clear, config:cache... nothing works. Then, all of a sudden, it starts working again.

Please or to participate in this conversation.