Summer Sale! All accounts are 50% off this week.

fylzero's avatar
Level 67

Is there an app()->runningUnitTests() for Laravel Dusk?

Curious if it is possible to detect programmatically if Dusk is currently running. Does anyone know?

I've tried if (app()->runningUnitTests()) and if (app()->environment('testing')) but neither seem to do the trick.

0 likes
4 replies
Nakov's avatar
Nakov
Best Answer
Level 73

runningUnitTests won't be true because Dusk tests are e2e feature tests.

And the environment to be testing I believe you should create your own environment file which you would like dusk to use. Otherwise dusk will use your .env file which means the same database and so on. https://laravel.com/docs/8.x/dusk#environment-handling

Take a look at this approach: https://medium.com/@deleugpn/testing-if-dusk-environment-matches-browser-environment-5edfe9d75ff6

fylzero's avatar
Level 67

@nakov Thank you for the reply. I was messing with the .env.dusk.local file but from what I can tell that file swap isn't happening?

Example:

  • I duplicate my .env file to .env.dusk.local
  • Change APP_ENV=testing inside .env.dusk.local
  • Place dd(config('app.env')); in my routes/web.php
  • Run php artisan dusk
  • Still says "local"?

Any idea why this wouldn't be working? I'm just trying to dodge a block of code when running Dusk tests.

fylzero's avatar
Level 67

Update, this check does work in Blade templates though. Going to check controllers now. This may solve my problem.

ehsanashar's avatar

Hi, I am not on dusk but I am facing this issue. I am running unit tests on Laravel 5 and App::runningUnitTests() is returning false during unit tests run. Why is that happening?

What I already tried is that I tried to set the APP_ENV to testing in the base class of unit test but to no avail. What could be the solution here?

My PHP version is 7.1.

Please or to participate in this conversation.