laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.51k stars 11.02k forks source link

[9.x] Vite directive throws on testing without manifest #44290

Closed DarkGhostHunter closed 2 years ago

DarkGhostHunter commented 2 years ago

Description:

When using a view with @vite, and using that view in unit testing, it returns the error Vite manifest not found at home/projects/laravel/public/build/manifest.json.

While this is indeed true (there is no manifest), it's bonkers when requiring it on unit testing.

Steps To Reproduce:

  1. Create a view with @vite() ponting to any random javascript entrypoint.
  2. Create a test that returns the view.
  3. See it explode.

This is valid too on the feature test:

public function test_vite_why(): void 
{
    Files::put('resources/js/test.js', '');
    Files::put('resources/views/test.blade.php', "@vite('resources/js/test.js')");

    Route::get('test', function () {
        return view('test');
    });

    $this->get('test')->assertOk();
}

Possible solution

Add a simple check inside Vite::manifest() that can returns an empty array if not found on unit testing.

if (! is_file($path)) {
    if (app()->runningUnitTests()) {
        return [];
    }

    throw new Exception("Vite manifest not found at: {$path}");
}
driesvints commented 2 years ago

https://laravel.com/docs/9.x/vite#disabling-vite-in-tests