inertiaのテストを書いていて、component
ファイルが正しいパスをかいているのにInertia page component file [*] does not exist.
とでてきて困ったときのメモ
- inertia 1.x
- laravel 11.x
最初に結論
config/ineatia.php
のpage_path
を更新すること
config/ineatia.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Server Side Rendering
|--------------------------------------------------------------------------
|
| These options configures if and how Inertia uses Server Side Rendering
| to pre-render the initial visits made to your application's pages.
|
| You can specify a custom SSR bundle path, or omit it to let Inertia
| try and automatically detect it for you.
|
| Do note that enabling these options will NOT automatically make SSR work,
| as a separate rendering service needs to be available. To learn more,
| please visit https://inertiajs.com/server-side-rendering
|
*/
'ssr' => [
'enabled' => true,
'url' => 'http://127.0.0.1:13714',
// 'bundle' => base_path('bootstrap/ssr/ssr.mjs'),
],
/*
|--------------------------------------------------------------------------
| Testing
|--------------------------------------------------------------------------
|
| The values described here are used to locate Inertia components on the
| filesystem. For instance, when using `assertInertia`, the assertion
| attempts to locate the component as a file relative to any of the
| paths AND with any of the extensions specified here.
|
*/
'testing' => [
'ensure_pages_exist' => true,
'page_paths' => [
resource_path('js/Pages'), // ここを正しいパスに変更
],
'page_extensions' => [
'js',
'jsx',
'svelte',
'ts',
'tsx',
'vue',
],
],
];
以下経緯
動かないテスト
tests\Feature\Controllers\User\IndexTest.php
<?php
namespace Tests\Feature\Controllers;
use App\Models\User;
class IndexTest extends TestCase
{
/**
* @test
*/
public function ユーザー一覧画面を表示する()
{
$url = route('users.index');
$response = $this->withoutMiddleware()->get($url);
$response->assertInertia(function (Assert $page) {
$page->component('User/Index');
}
}
動かないテストを実行してみる
$ php artisan test tests/Feature/Controllers/User/IndexTest.php
エラー
FAIL Tests\Feature\Controllers\User\IndexTest
⨯ ユーザー一覧画面を表示する
0.15s
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\Feature\Controllers\A\User\IndexTest > ユーザー一覧画面を表示する AssertionFailedError
Inertia page component file [User/Index] does not exist.
at vendor/inertiajs/inertia-laravel/src/Testing/AssertableInertia.php:53
49▕ if ($shouldExist || (is_null($shouldExist) && config('inertia.testing.ensure_pages_exist', true))) {
50▕ try {
51▕ app('inertia.testing.view-finder')->find($value);
52▕ } catch (InvalidArgumentException $exception) {
➜ 53▕ PHPUnit::fail(sprintf('Inertia page component file [%s] does not exist.', $value));
54▕ }
55▕ }
56▕
57▕ return $this;
+1 vendor frames
2 tests/Feature/Controllers/User/IndexTest.php:31
+2 vendor frames
5 tests/Feature/Controllers/User/IndexTest.php:29
Tests: 1 failed (8 assertions)
Duration: 0.17s
どうみても正しいとこにあるんだけどな~(検索)
ん!?
設定ファイルでテストするときのリソースファイルがあるディレクトリを指定しないといけないのね!?
React+typescriptでかいているからjsからtsにディレクトリ名を変更したからでした。