I’m using Laravel 10 with Livewire 2. I was writing a simple test for a component, which is just an upload form. I ran into a cryptic message error:
FAILED TestsFeatureLivewireUploadAssetTest > book can be saved correctly
Type is not supported
at vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php:88
84▕ default => json_encode($data, $this->encodingOptions),
85▕ };
86▕
87▕ if (! $this->hasValidJson(json_last_error())) {
➜ 88▕ throw new InvalidArgumentException(json_last_error_msg());
89▕ }
90▕
91▕ return $this->update();
92▕ }
The test is:
/** @test */
public function book_can_be_saved_correctly()
{
$admin = User::factory()->create();
$this->actingAs($admin);
Storage::fake('private');
$testBook = UploadedFile::fake()->create('private/books/test_book.pdf');
$testCover = UploadedFile::fake()->image('private/covers/test_image.png');
$emptyForm = (new UploadBookService())->buildForm();
$book = array_merge($emptyForm,[
'title' => 'Title Test',
'author' => 'Author Test',
'description' => 'Description Test',
'publishedAt' => '2022',
'cover' => $testCover,
'book' => $testBook,
]);
$component = Livewire::test(UploadAsset::class)
->set('assetMeta', $book)
->call('save');
Storage::assertExists('books/test_book.pdf');
Storage::assertExists('covers/test_cover.png');
}
For what I’ve debugged so far, the error is triggered once i call set()
in the test.
I can confirm that the component is working while using it.
2
Answers
Ok, I guess I found an answer: probably the objects inside the array are not parsed correctly, so if I modify my test in:
by separately calling the
set()
forassetMeta.book
andassetMeta.cover
the Type error disappears.The error is that you are passing a value that is not compatible with the json_encode() function, I suggest the following: