Creating a Pest test0:04
Testing in live wear four has literally never been easier, especially with how good pest has gotten. So let me show you how we can unit test, uh, this little post create component that we have here. So it's a form with two fields and a save button. Uh, let's test it. So we're gonna go to tests feature and let's create a new test. For this feature we'll use Laravel Command Artisan May Test and we'll say Create post test.
For this feature we'll use Laravel Command Artisan May Test and we'll say Create post test. Okay? And now we get a new create post test file right here. It's using PE by default. 'cause when I did typed Laravel new for this project, I decided to use Pest. Pest is super powerful and especially with its new browsing browser testing stuff we're gonna get to, but I'm all in on pest. I think you should be too. So here's a pest test
we're gonna get to, but I'm all in on pest. I think you should be too. So here's a pest test and it's just like a smoke test. It hits an endpoint and says Is it a 200 status code? And it passes. I have a test runner for my editor. Um, but you can always just run the test command pest and then it'll run all of your pest tests. Okay, so let's test this live wire component. I'm gonna open it over here so we can see it while we're testing it.
Unit testing Livewire component1:06
I'm gonna open it over here so we can see it while we're testing it. And the first thing is live wire, colon, colon test. Let's import live wire test. And then you just pass the name of the component. So pages post create, and let's see what happens if I run this right now. Okay, didn't perform any assertions but it didn't fail. First thing we're gonna do is set this property. So we're unit testing a live wire component.
First thing we're gonna do is set this property. So we're unit testing a live wire component. We can't click or type. We can't say type this into that 'cause there's no browser. That's browser testing and we're gonna get to that. But this is unit testing where we're setting the property of a component. We're calling a method and we're asserting that all the plumbing works. So we're gonna set the title property to test title,
that all the plumbing works. So we're gonna set the title property to test title, and then we're gonna set the content property to test content. And then we're gonna call the save method. So call save. And the last bit is we're gonna assert redirect to the root endpoint. 'cause at the end of the save method, if it's exceeded, it's gonna redirect. And we want to assert that there's lots of assertions
if it's exceeded, it's gonna redirect. And we want to assert that there's lots of assertions and affordances with live wires testing utilities. So go to the documentation and look through those for anything you might need. But this is kind of a way to just get us going here. So let's run the test and it failed. So what's the failure? Ah, it's a database failure. So the live wear stuff's working. It just says there's no such table posts
Fixing SQLite test database2:23
So the live wear stuff's working. It just says there's no such table posts for the SQL light connection. Okay, so this tells me it works in the browser, but it's not working in the test suite. So there must be some custom database set up for the test suite. And there is, so if you look at PHP unit xml, this is the main kind of 'cause pest uses PHP unit under the hood.
this is the main kind of 'cause pest uses PHP unit under the hood. So it's gonna like defer to all of PHP units config type stuff. And if you look in the PHP unit config file, you'll see all these custom ENV variables that get engaged when you run a pest test. And here's the one that we're interested in, database DB database memory. So it's saying use the SQL light connection
DB database memory. So it's saying use the SQL light connection but use an in-memory database, not the actual database of our application. So when we're running this test, it has a database, it's just totally blank and it says, Hey, there's no post table even found. So how do we make sure that when we run a test, it is actually running our migrations into this ephemeral quick little database.
it is actually running our migrations into this ephemeral quick little database. All of that stuff lives inside of pest PHP. This is pest configuration file inside the test folders. So tests pest PHP. And then here's the bit in question right here. It's Pest Extend test case for the feature tests and it says use refresh database trait. So this is Laravel refresh database trait that makes sure that every time you run any test, it's going to build up
So this is Laravel refresh database trait that makes sure that every time you run any test, it's going to build up and tear down your whole database for every test. And there's lots of different things. There's refresh database, there's, I'm blanking on the other ones. There's a bunch of other ones and every app is different, but this is a good kind of happy path default. So let's un comment that out. So now we're actually refreshing our database
Adding database assertions4:03
So let's un comment that out. So now we're actually refreshing our database and now if I go over back to our test and I try to run it, we get a successful run. Perfect. However, this test is not really making any assertions on the database contents themselves. And I, I really wouldn't be able to, you know, sleep well with this test until there was something saying there was nothing in the database.
with this test until there was something saying there was nothing in the database. We hit the live wire component and now there's something in the database. So let's do that. There's a really nice helper function from pest called assert database has an assert database missing. So let's import those assert database missing and then we gotta import post. We're just gonna say for this model, assert that the database does not have an entry with this title
We're just gonna say for this model, assert that the database does not have an entry with this title and this content and this should pass and it does. And now we're gonna do the inverse where we say, instead of assert database missing, we want to assert database has this entry for this post, uh, model. So we rerun it and it passes. Perfect. So this is basically live wear unit testing. In a nutshell, it's super powerful, it's really fast and uh, it's a great way
Wrap-up and next steps5:06
In a nutshell, it's super powerful, it's really fast and uh, it's a great way to get more confidence about your components and pest makes it so easy and so nice with little helpers like that. Um, so there you go. We're gonna get deeper and do some browser testing where we really unlock some real power. Uh, so see you there.
where we really unlock some real power. Uh, so see you there.
