Introduction
A common way to build your Laravel application's frontend assets is with Vite (by running a command such as npm run dev or npm run build). You can then use Laravel's handy @vite Blade directive to include those built assets in your views.
On page load, Laravel will then look for a manifest.json file in your public/build directory to determine which CSS and JavaScript assets to load.
But sometimes, you might want to run your PHPUnit or Pest tests without needing to build your assets with Vite first. For example, let's take this test, which checks that the welcome page can be returned:
declare(strict_types=1);
namespace Tests\Feature;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
final class WelcomeTest extends TestCase
{
#[Test]
public function welcome_page_can_be_returned(): void
{
$this->get('/')
->assertOk()
->assertViewIs('welcome');
}
}
In the test, we're just asserting that when we visit the / route, the welcome page is returned and that it uses the correct view. We're not inspecting anything in the view itself, so it's not necessary to build our assets with Vite.
However, if our page is using the @vite directive, and we haven't built our assets, then we'll get an error like this when running the above test:
Expected response status code [200] but received 500.
Failed asserting that 500 is identical to 200.
The following exception occurred during the last request:
Illuminate\Foundation\ViteManifestNotFoundException: Vite manifest not found at: /Users/ashallen/www/laravel-app/public/build/manifest.json
Let's quickly explore how to avoid this error and run your PHPUnit and Pest tests for your Laravel application without needing to build your assets with Vite. We'll also explain the benefits of this approach.
Run PHPUnit Tests without Vite
If you're using PHPUnit for your tests, you can disable Vite for individual test classes by using the withoutVite method. You can add this to the setUp method of your test class like so:
declare(strict_types=1);
namespace Tests\Feature;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
final class WelcomeTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->withoutVite();
}
#[Test]
public function welcome_page_can_be_returned(): void
{
$this->get('/')
->assertOk()
->assertViewIs('welcome');
}
}
Typically, you wouldn't want to add this to every test class, though. Instead, you might want to add it to your base test case class so that it applies to all your tests:
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->withoutVite();
}
}
This now means that all your tests will run without needing to build your assets with Vite.
Run Pest Tests without Vite
Alternatively, let's say you're using Pest and your test looks like this:
test('welcome page can be returned', function () {
$this->get('/')
->assertOk()
->assertViewIs('welcome');
});
You can disable Vite for all the tests in the Feature directory/suite by using the withoutVite method in your tests/Pest.php file:
pest()->extend(Tests\TestCase::class)
->in('Feature')
->beforeEach(function () {
$this->withoutVite();
});
You'll now be able to run your Pest tests without needing to build your assets with Vite first.
Benefits of Running Tests Without Vite
There may be times when you actually require your assets to have been built for your tests to run correctly. For example, if you're using something like Laravel Dusk, Playwright, or Cypress to run browser tests that interact with your frontend, then you'll likely need to build your assets with Vite first.
But unless you're running these types of tests, it's unlikely you'll need to do so.
As a result of not building your assets with Vite, it can speed up your testing process. This is especially true if you're running your tests in a continuous integration (CI) environment, such as GitHub Actions. You can save time by skipping the Node dependency installation and asset build steps. As a result, your CI pipeline can then run faster, giving you quicker feedback on your code changes.
Conclusion
Hopefully, this Quickfire article has shown you how to run your PHPUnit and Pest tests in Laravel without needing to build your assets with Vite first.
If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter.
Keep on building awesome stuff! 🚀
Top comments (1)
Hey! thanks this is really useful... I think that I had this issue before and fix it the same way... but only for phpUnit thanks