How to make PestPHP boot Laravel so I can use facades such as Config?

1k views Asked by At

I want to use standard Laravel facades, such as Str and Config, in my PestPHP Feature tests. Is there some concise way I can get PestPHP to do this for all of my tests in my tests/Feature directory?

I thought having uses(Tests\TestCase::class)->in('Feature'); in my Pest.php file would be sufficient, but all I ever get is this error: "A facade root has not been set."

1

There are 1 answers

0
CXJ On

The problem appears to be that Pest does not boot Laravel when it runs your test files, but rather only when you call certain global functions, e.g. the test closure functions test() and it(). So it's not possible to use Pest's "simplified" test script structure to easily initialize class-wide (file wide) values, i.e. attributes. beforeAll() does not work for this. Instead, one has to use beforeEach() in each class where facades will be needed.

See these issues: https://github.com/pestphp/pest/issues/237, https://github.com/pestphp/pest/issues/33#issuecomment-658022624, https://github.com/pestphp/pest/issues/246.