Search code examples
laravelalgolialaravel-dusk

Laravel Dusk test error on Algolia Search: Impossible to connect, please check your Algolia Application Id


In this project we use Algolia Search installed through Composer. When I'm running a dusk test on the login form it fails because of an error. The login on the form itself performs wel, it seems when this test actually logs in and ends up on the homescreen, this is where it fails.

Note: There is an Algolia APP_ID and SECRET defined in the .env file, and all is working fine when using the application.

The actual error output for this test:

1) Tests\Browser\LoginTest::testLogin
Algolia\AlgoliaSearch\Exceptions\UnreachableException: Impossible to connect, please check your Algolia Application Id.

Dusk test:

public function testLogin()
    {
        $user = factory(User::class)->create([
            'email' => 'dusktester@mail.com',
            'password' => '***'
        ]);

        $this->browse(function (Browser $browser) use ($user) {
            $browser->visit('/login')
                    ->type('email', 'dusktester@mail.com')
                    ->type('password', '***!')
                    ->press('.button')
                    ->assertPathIs('/');
        });
    }

Solution

  • Solved it another way. Our login system is a little more complex and links another table depending on the type of user you are. Since this wasn't defined in my user factory within this Dusk test it lacked some crucial information about this user which led to the Algolia Search error.

    The way I solved it:

    No longer creating a user within the Dusk test and use one of my already seeded test users. The credentials for this user are taken from my .env file to ensure a clean / safe dusk test file that can be uploaded to Git:

    public function testLogin()
        {
    
            $this->browse(function (Browser $browser){
                $browser->visit('/login')
                        ->type('email', env('DUSK_USER'))
                        ->type('password', env('DUSK_PASSWORD'))
                        ->press('.button')
                        ->assertPathIs('/');
            });
        }