Skip to content
Latchkey

Laravel "A facade root has not been set" in CI

A Laravel facade proxies to a service resolved from the application container. If the app was never bootstrapped, the facade has no root to resolve against and throws "A facade root has not been set." In tests this means the case did not boot the framework.

What this error means

A test using a facade (Config, DB, Cache, Log) fails with "RuntimeException: A facade root has not been set." The test extends PHPUnit\Framework\TestCase directly instead of the Laravel base TestCase.

phpunit
  RuntimeException: A facade root has not been set.

  /vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:358

Common causes

The test does not bootstrap the application

A pure unit test extending PHPUnit\Framework\TestCase never calls createApplication(), so no container exists for facades to resolve against.

A facade is used in setup before boot

Code called during test construction touches a facade before the framework has booted, so the root is unset.

How to fix it

Extend the Laravel base TestCase

  1. Extend Tests\TestCase, which uses CreatesApplication.
  2. Let it boot the app so facades resolve.
  3. Re-run the test with the framework bootstrapped.
tests/Feature/ReportTest.php
namespace Tests\Feature;

use Tests\TestCase;

class ReportTest extends TestCase { /* facades work here */ }

Avoid facades in true unit tests

For tests that intentionally skip the framework, inject dependencies instead of calling facades.

tests/Unit/ReportServiceTest.php
$service = new ReportService($repository);

How to prevent it

  • Extend Tests\TestCase for anything that touches the framework.
  • Keep facade usage out of pure unit tests.
  • Inject dependencies so unit tests need no container.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →