Loading
Current section: End-to-End 6 exercises
lesson

Intro to End-to-End

Loading lesson

Transcript

00:00 Let's write some end-to-end tests. This exercise is going to introduce you to Playwright, this amazing tool that allows us to write awesome automated tests. So first, I want to talk about manual testing a little bit. No matter what you do, you cannot avoid testing. It is basically impossible. Nobody just writes code and then ships it

00:19 without running some form of test, whether that be an automated test that they wrote a script for or manually running the code and executing the code that they created. So we are testing, period. In fact, even just using TypeScript, that is a form of testing.

00:37 In the testing trophy shape, if you're familiar with that, the base of that is static testing, and that includes TypeScript. So manual testing is definitely what you're going to be doing if you aren't automating. And some people think that testing

00:53 is like this special type of software development. It's not. You're literally writing software to automate human processes. So that's all that it is. And that's what we write software for, is to automate human processes. So manual testing, it becomes untenable after your application grows.

01:12 And every single time you make a change to your app, you have to go and manually test everything to make sure you didn't break anything. It's really a pain. And so we're going to automate all of that, just like we automate other human processes. Whatever you're building your app for, you are automating some other human process

01:31 and testing exactly the same sort of thing. So I like to think of automated testing as thousands of little kints, just like I can just execute, say, go make sure that things work the way that they worked when I wrote the test. And that helps a lot, especially when you're thinking about how you construct the test.

01:50 You think, well, how would I do this if I was doing it manually? So you're going to be thinking about that a lot as you're writing tests. Like, how would I do this if I was doing it manually? Try to do that when you automate it. So you're using a computer program to make certain that a human can use your app. That is what you're doing when you're writing an automated test.

02:10 And the problem with that is that you're kind of stepping back from the way that your app is actually being used, because your app is not going to be used by a computer program. Your app is going to be used by a human. And so it's very important that you keep this in mind. The more your tests resemble the way your software is used,

02:28 the more confidence they can give you. So I tweeted about that years ago. And that is the guiding principle for many a developer's testing pattern, is just to try and make the test resemble the way that the software is used as closely as reasonable.

02:45 And we'll be kind of exploring that a lot in this workshop and seeing how close can we get it without losing reason. So what is a test? Let's give you a quick intro. This is your source code. It's this sum function. And this is a test. You have some expected thing.

03:04 You have an actual thing. And if those things don't match, then you throw. In fact, the basic definition of this is a test is code that throws an error when something is wrong. That's it. That's all that a test is. Now, testing frameworks make doing that a lot easier. But it's important for you to understand

03:24 that all that an automated test is, is just you execute this thing. And if it fails with a non-zero exit code or whatever, then the test fails. And that's it. So it is actually a pretty simple and straightforward thing.

03:40 So end-to-end testing is a special type of testing. It sits at the top of the testing trophy shape. And with end-to-end testing, you want to think about the way that you would manually or you're currently manually testing your app and then use a tool to automate that process.

03:58 Whatever you're doing right now, just use a tool to automate that process. And that specifically just means that you're not mocking out nearly or anything if you can. Of course, in our application, we are mocking out third parties. And that's what I recommend.

04:18 You typically don't want your test to exercise the code of third parties. But there are use cases for that as well. But typically, you're just going to be saying that from the UI piece all the way down to the database that you control,

04:35 you're going to be testing the full stack. And that gives you an enormous amount of confidence. It's no accident that you're going through this exercise first in the workshop, because I think that it's that important that we start with end-to-end tests. That's what I recommend.

04:53 If you have no test at all, write one end-to-end test, and you'll just get an enormous amount of confidence. Like, the application can connect to the database, and the application can actually render on the client. Like, the server starts up. Like, just an enormous amount of confidence that you don't get from other levels of tests.

05:11 So that's why I always recommend end-to-end tests. Now, we're going to be using a tool called PlayWrite. PlayWrite's fantastic, a really, really great automation tool that we're looking at right here. And yeah, this is just what a typical test looks like. And you're going to be writing some of this. So you've got your test function. You have this thing called fixtures.

05:30 You've got a way to control the browser and the page that we're looking at here. And then you have assertions as well. And you also have ways to look at the page and interact with different pieces of the page.

05:47 So really, really awesome set of utilities from PlayWrite. And in this exercise, we're going to be implementing a test on the search page. So you'll go to the home page. You'll type in a search. You'll see the results of that search. And then you'll make sure that you can actually

06:06 get the empty state of no users found. So it is a pretty number of things going on in this test and a number of surprising gotchas that we're going to run into as a part of this exercise too. So I think you're going to have a good time. So yeah, let's get started.