Vite and Test Setup0:00
Laravel's Vite integration will attempt to resolve your assets when you run your tests. So this means that you essentially either need to run the Vite development server or build your assets for production when you run your tests. There's a third option, but we'll look at that here in a moment. So I am running the Vite development server. Let's run some tests. So let's start by going to the ExampleTest for our unit, and I'm just going to comment this out so that we just have one test to work with. So inside of the feature folder for ExampleTest, we're going to change this so that we will see Laracasts in Welcome.vue.
So inside of the feature folder for example test, we're going to change this so that we will see Laracasts in Welcome.vue. So the test itself is just going to call the Vue method, and we want to assert that we see Laracasts. Pretty simple. So let's run this test with php artisan test, and we should see that it passed. Great. That makes perfect sense because we are running the development server. So let's stop that. Let's go back.
Failing Without Built Assets1:07
So let's stop that. Let's go back. Let's run the test again, and that passed as well. But that also makes perfect sense because if we take a look at the public folder, we have that build folder where our assets were built for production. So let's delete that. Let's run the test again, and it is going to fail. And if we take a look at the reason why, it says that the Vite manifest not found at path. So as I mentioned, in order for our tests to pass, our assets have to be resolved, which means that the Vite development server has to be running or our assets have to be built.
Disabling Vite per Test1:31
So as I mentioned, in order for our tests to pass, our assets have to be resolved, which means that the Vite development server has to be running or our assets have to be built for production. Or we could just disable Vite, and it's easy enough to do. Instead of our test case, we just need to call the withoutVite method, and that's it. That is going to essentially bypass the need for resolving our assets, and we see that our test passed. That's great. That's awesome. Except that what if we have 100 tests?
Disabling Vite Globally2:04
That's awesome. Except that what if we have 100 tests? Then we would have to add this without vite for each one of those tests, and that's a tedious thing. We don't want to do that. Instead, what we could do is just disable vite globally for all of our tests. Inside of our TestCase, we will override the protected method called setUp. And the first thing we need to do is call the parent class's setUp method, so that then we could call without vite here. And this is going to disable vite for all of our tests.
we could call without Vite here. And this is going to disable Vite for all of our tests. So let me make sure that this is commented out in our test case. It is. We have globally disabled Vite for our tests. So if we run the test again, we can see that it passed. If I comment out that line, then we will see it will fail. So how you go about testing your application is really up to you and what makes sense to your project. Does it make sense to use the development server to build your assets for production?
Choosing a Testing Strategy2:59
your project. Does it make sense to use the development server to build your assets for production or to just bypass Vite altogether? The choice is up to you.
