Latest / CyberSecurity Summary / Simplify Testing with React Testing Library: Create maintainable tests using RTL that do not break with changes
Transcript
- 0:00Speaker 1: All right, everyone, get ready for this. We're diving deep
- 0:02Speaker 1: today into React Testing Library RTL. If you want to
- 0:05Speaker 1: sound cool.
- 0:06Speaker 2: Uh huh.
- 0:07Speaker 1: Yeah.
- 0:07Speaker 2: When we say cool, we really mean developers who are
- 0:10Speaker 2: you know, serious about building React apps that are like
- 0:13Speaker 2: actually good, robust, user friendly.
- 0:17Speaker 1: All that for sure, And we've got an awesome guide
- 0:20Speaker 1: for this deep dive. Simplified Testing with React Testing Library
- 0:24Speaker 1: by Scotty Crump. Really a game changer.
- 0:26Speaker 2: Absolutely. It's not just about you know, going through the
- 0:29Speaker 2: motions with testing. It's about making tests that matter that
- 0:33Speaker 2: focus on how like real people are going to use
- 0:37Speaker 2: the thing you built.
- 0:38Speaker 1: So let's about getting lost in all the code details
- 0:40Speaker 1: and more about thinking like a user would clicking buttons,
- 0:43Speaker 1: using forms, that sort of thing.
- 0:44Speaker 2: Exactly. It's like imagine you're building a car. You wouldn't
- 0:47Speaker 2: just test that the engine turns on right, You'd want
- 0:49Speaker 2: to actually drive it, make sure the brakes work, the
- 0:51Speaker 2: steering wheel.
- 0:51Speaker 1: Does its job, Yeah, the seats are comfy, the whole experience.
- 0:55Speaker 2: Exactly, because that's what matters the driver's experience. And that's
- 0:58Speaker 2: the philosophy behind RTL. Well testing from the user's perspective,
- 1:02Speaker 2: making sure that your React apps aren't just functional but
- 1:05Speaker 2: actually enjoyable to use.
- 1:06Speaker 1: Okay, I'm with you on the philosophy, but what about
- 1:09Speaker 1: the actual tools and techniques What makes RTL stand out.
- 1:13Speaker 2: So one of the key parts is just it's a
- 1:15Speaker 2: JavaScript testing framework that's become like the industry standard for React.
- 1:20Speaker 2: It gives you a really solid environment for running those
- 1:23Speaker 2: tests and it works perfectly with RTL.
- 1:25Speaker 1: And then there's this thing called the screen object right,
- 1:28Speaker 1: which is kind of like a direct link to your
- 1:30Speaker 1: apps UI.
- 1:31Speaker 2: Yeah, think of it like x ray vision into your app.
- 1:33Speaker 2: You can find elements on the page the same way
- 1:36Speaker 2: a user would by their role, the text on them,
- 1:38Speaker 2: stuff like that.
- 1:38Speaker 1: So no more relying on those internal IDs or classes
- 1:42Speaker 1: that could break your tests. That the code changes exactly.
- 1:45Speaker 2: And when it comes to user interactions, RTL takes it
- 1:48Speaker 2: a step further. With the user event module, you can
- 1:51Speaker 2: actually simulate real user actions type being clicking, selecting. Makes
- 1:55Speaker 2: your test super realistic.
- 1:57Speaker 1: So instead of just checking if a button exists, you
- 1:59Speaker 1: can test what happens when a user clicks it, how
- 2:02Speaker 1: the UI changes, how the app reacts, all that. It's
- 2:05Speaker 1: like having a robot.
- 2:06Speaker 2: User huh huh exactly. And remember that car analogy. Well,
- 2:10Speaker 2: when you're testing components that depend on libraries like Redux,
- 2:14Speaker 2: it's like trying to test the engine while it's still
- 2:16Speaker 2: hooked up to the transmission. Custom render methods are how
- 2:19Speaker 2: we get around that. They let us isolate the component
- 2:22Speaker 2: and test it properly.
- 2:23Speaker 1: That's a brilliant analogy. So custom render methods are like
- 2:26Speaker 1: setting up a little test lab where you can control
- 2:28Speaker 1: the environment and see how your components react exactly.
- 2:31Speaker 2: And this brings us to another important concept, the difference
- 2:35Speaker 2: between integration testing and isolation testing.
- 2:38Speaker 1: Now, this is where things get interesting. So integration testing
- 2:42Speaker 1: is like looking at the big picture seeing how all
- 2:44Speaker 1: the parts of your app work together, and isolation testing
- 2:47Speaker 1: is kind of like zooming in on a single piece.
- 2:49Speaker 2: Yeah, exactly. The book Use is a great example a
- 2:52Speaker 2: vote component. It's got like two buttons like and dislike.
- 2:56Speaker 2: Instead of testing each button separately, it shows how to
- 2:59Speaker 2: test the whole component together.
- 3:01Speaker 1: So like simulating a user clicking on like then dislike
- 3:05Speaker 1: and making sure the counts update right.
- 3:07Speaker 2: Right, You're testing the entire voting system from a user's perspective,
- 3:10Speaker 2: making sure it all flows smoothly.
- 3:12Speaker 1: Makes sense, But doesn't that make integration tests way more complex.
- 3:16Speaker 2: It can be. You might have to you know, mock
- 3:19Speaker 2: some dependencies or fake some external services, but in the
- 3:23Speaker 2: end you get a way better understanding of how your
- 3:25Speaker 2: app actually behaves.
- 3:26Speaker 1: So it's about finding the right balance between complexity and
- 3:29Speaker 1: getting a full picture.
- 3:31Speaker 2: Yep, And the right approach depends on what you're testing.
- 3:34Speaker 2: Sometimes testing in isolation is all you need, like if
- 3:37Speaker 2: it's just a simple button or something, But for.
- 3:39Speaker 1: A feature with lots of moving parts, integration testing is
- 3:42Speaker 1: the way to go totally.
- 3:44Speaker 2: And oh, don't forget about legacy code. A lot of
- 3:47Speaker 2: projects have old tests written with tools like enzyme. Switching
- 3:51Speaker 2: these to RTL can seem tough, but it's so worth it.
- 3:54Speaker 1: Kind of like restoring a classic car. You want to
- 3:57Speaker 1: keep its charm, but give it a modern engine.
- 3:59Speaker 2: Exactly. By moving to RTL, you bring all these advantages
- 4:03Speaker 2: of modern testing to your older projects, making your tests
- 4:06Speaker 2: way better, easier to maintain, and in line with like
- 4:10Speaker 2: the best practices today.
- 4:11Speaker 1: Okay, so we've talked philosophy, the basics, even touched on
- 4:16Speaker 1: legacy code, but there's more right. RTL has this whole
- 4:20Speaker 1: ecosystem of advanced tools too.
- 4:22Speaker 2: Oh, yeah, we're just getting started es lint, plugins, testing, playground, WALLABYJS,
- 4:27Speaker 2: things that'll seriously level up your testing.
- 4:29Speaker 1: Well, the dive into those later those sounds super interesting.
- 4:32Speaker 1: I'm especially curious about this time traveling debugging thing you
- 4:35Speaker 1: mentioned before.
- 4:36Speaker 2: Ahah. Yeah, we'll get to that. And then there's Cypress,
- 4:38Speaker 2: which takes UI testing to like a whole other level,
- 4:41Speaker 2: letting you simulate complete user journeys throughout your application.
- 4:44Speaker 1: So Cypress is basically like a robot user that can
- 4:47Speaker 1: browse your entire website, add stuff to a cart, check
- 4:49Speaker 1: out all.
- 4:50Speaker 2: That exactly, and it integrates seamlessly with RTL, so you
- 4:53Speaker 2: get that user centric approach even for those end to
- 4:55Speaker 2: end tests. And it even uses design patterns like page
- 4:58Speaker 2: objects to help or organize and maintain your Cyprus test,
- 5:01Speaker 2: which is super helpful for larger projects.
- 5:04Speaker 1: All right, I'm definitely feeling the excitement building. We barely
- 5:07Speaker 1: scratch the surface, but I can already see how RTL
- 5:10Speaker 1: can completely transform testing for sure.
- 5:12Speaker 2: This is just the beginning. In the next part, we'll
- 5:15Speaker 2: get more into the practical applications of RTL, looking at
- 5:18Speaker 2: examples and strategies for testing different things. Stay tuned, all.
- 5:23Speaker 1: Right, Welcome back to our React testing library deep dive.
- 5:26Speaker 1: We're learning to think like users, not just code monkeys.
- 5:30Speaker 2: Ah huh, right, and this time we're going to get practical,
- 5:33Speaker 2: see how this whole user centric testing thing actually works
- 5:36Speaker 2: in you know, real projects.
- 5:39Speaker 1: Sounds good to me. Ready to get into the code.
- 5:41Speaker 2: Well, before we do that, let's just quickly revisit the
- 5:43Speaker 2: whole RTL philosophy. The book really hammers this point test
- 5:48Speaker 2: from the user's perspective. That's how you get tests that
- 5:51Speaker 2: are actually valuable.
- 5:52Speaker 1: Makes sense. At the end of the day, it's users
- 5:54Speaker 1: who are going to be using our app, so the
- 5:56Speaker 1: tests should reflect.
- 5:57Speaker 2: That right totally. We focus on the results users, not
- 6:00Speaker 2: just the internal code stuff.
- 6:02Speaker 1: Okay, so what does that look like in practice? Can
- 6:05Speaker 1: you give me an example?
- 6:06Speaker 2: Sure, Imagine we're building an online store and we're testing
- 6:10Speaker 2: that ad to cart.
- 6:11Speaker 1: Button, classic e commerce feature. Gotta have that exactly now.
- 6:15Speaker 2: The old way of testing might just be like making
- 6:18Speaker 2: sure the ad to kurt function gets called when you
- 6:20Speaker 2: click it right, checking.
- 6:21Speaker 1: The code behind it is doing its thing.
- 6:22Speaker 2: But with RTL it's different. We don't care as much
- 6:25Speaker 2: about that function call. We care about what the user
- 6:27Speaker 2: actually sees happening.
- 6:29Speaker 1: So we check that the item actually shows up in
- 6:31Speaker 1: their cart after.
- 6:32Speaker 2: They click yep, and that the cart counter updates, maybe
- 6:36Speaker 2: even a success message pops up. Stuff like that.
- 6:38Speaker 1: So it's not just does the button work, it's does
- 6:41Speaker 1: the whole adding to card experience work correctly? Right?
- 6:45Speaker 2: And these tests are way more resistant to code changes, Like,
- 6:49Speaker 2: as long as what the user sees is the same,
- 6:51Speaker 2: your tests should pass even if you totally rewrite the
- 6:54Speaker 2: code underneath.
- 6:55Speaker 1: That's smart future proof testing. Speaking of which, the book
- 7:00Speaker 1: also talked about integration testing versus isolation testing. Can you
- 7:03Speaker 1: break that down for me?
- 7:04Speaker 2: Sure? Integration testing is like seeing how all the different
- 7:08Speaker 2: parts of your app work together, like a well oiled machine,
- 7:11Speaker 2: like putting.
- 7:11Speaker 1: The puzzle pieces together, making sure they all fit exactly.
- 7:15Speaker 2: The book has a great example a vote component. It's
- 7:18Speaker 2: got two buttons like and dislike. Instead of testing each
- 7:21Speaker 2: button on its own, it shows how to test the
- 7:23Speaker 2: whole component as one unit.
- 7:26Speaker 1: So we'd be simulating a user clicking like than dislike
- 7:30Speaker 1: and making sure the counts are right yep.
- 7:32Speaker 2: You're testing the whole voting mechanism from the user's point
- 7:35Speaker 2: of view, ensuring that all the pieces work well together.
- 7:38Speaker 1: That makes sense, But doesn't that make integration tests like
- 7:41Speaker 1: more complicated to set up?
- 7:43Speaker 2: Yeah, sometimes you might need to mock certain things or
- 7:46Speaker 2: simulate external services things like that, but you get a
- 7:50Speaker 2: much better understanding of how the whole app behaves.
- 7:53Speaker 1: So it's a trade off. More complex to set up,
- 7:56Speaker 1: but you get a more complete picture exactly.
- 7:58Speaker 2: And that's why choosing the right testing strategy is so important.
- 8:02Speaker 2: Sometimes isolation testing is enough, like if you're just working
- 8:05Speaker 2: on a button or something small, but if it's.
- 8:07Speaker 1: A big feature with lots of components interacting, integration testing
- 8:11Speaker 1: is probably better, right.
- 8:12Speaker 2: And speaking of complex features, things can get even trickier
- 8:16Speaker 2: when you're using libraries like reducts, which is often used
- 8:19Speaker 2: for managing State and React apps.
- 8:21Speaker 1: Oh yeah, reducs. I've definitely struggled with testing those components.
- 8:25Speaker 1: It's like trying to test an engine while it's still
- 8:27Speaker 1: attached to the car.
- 8:28Speaker 2: Perfect analogy. You need a way to isolate the component
- 8:31Speaker 2: you're testing from all that extra stuff. That's where custom
- 8:34Speaker 2: render methods come in. They let us create a controlled
- 8:37Speaker 2: environment for testing Redux components.
- 8:39Speaker 1: So instead of using the real reduct store, we can
- 8:42Speaker 1: create a fake one with like preset data makes things
- 8:46Speaker 1: more predictable exactly.
- 8:48Speaker 2: And this approach can be used with other libraries too.
- 8:50Speaker 2: It's all about isolating what you're testing, right.
- 8:53Speaker 1: I see in the book also had some good tips
- 8:55Speaker 1: for dealing with legacy code. Right. A lot of projects
- 8:58Speaker 1: have those older tests using things like enzyme and moving
- 9:02Speaker 1: those to RTL seems daunting.
- 9:04Speaker 2: It can be, but the book offers some great guidance.
- 9:07Speaker 2: Think of it like renovating a classic car. You want
- 9:10Speaker 2: to keep the charm, but update it with modern stuff.
- 9:13Speaker 1: So we're not just throwing away all the old tests,
- 9:15Speaker 1: We're updating them to use RTL's approach.
- 9:18Speaker 2: Exactly, and that way you get all the benefits of
- 9:21Speaker 2: RTL even in older projects. Makes your tests more robust,
- 9:25Speaker 2: easier to work with, and align with current best practices.
- 9:28Speaker 1: Okay, that makes sense. Now I'm really curious about those
- 9:31Speaker 1: advanced tools you mentioned earlier. Can we get into those now?
- 9:33Speaker 2: Sure. One of the essentials is es LNT. It's like
- 9:37Speaker 2: a codelinter, but it can be extended with plugins specifically
- 9:40Speaker 2: for RTL.
- 9:40Speaker 1: And JEST, So ESLINT for those who don't know, it's
- 9:43Speaker 1: like a grammar checker for your code helps you catch
- 9:46Speaker 1: errors and write cleaner.
- 9:47Speaker 2: Code exactly, And these plug ins for RTL and JEST
- 9:50Speaker 2: they take it further, giving you rules and tips specifically
- 9:53Speaker 2: for writing good tests. They can help you avoid common
- 9:56Speaker 2: mistakes and generally make your test better.
- 9:58Speaker 1: So like having a testing expert looking over your shoulder
- 10:01Speaker 1: giving you advice as you write code.
- 10:03Speaker 2: Ah yeah. And then there's Testing Playground. If es lint
- 10:07Speaker 2: is the grammar checker, this is like the visual dictionary
- 10:10Speaker 2: for your tests. You can test out different selectors and
- 10:12Speaker 2: see how they work with your HTML.
- 10:14Speaker 1: I love Testing Playground. It's like having X ray vision
- 10:17Speaker 1: for your components, showing you exactly what elements you need
- 10:20Speaker 1: to target in your tests.
- 10:21Speaker 2: Right, no more guessing about which selectors to use. It
- 10:25Speaker 2: helps you write tests that are more accurate and less
- 10:27Speaker 2: likely to break when your code changes. And for folks
- 10:30Speaker 2: who want instant feedback and some really cool debugging features,
- 10:34Speaker 2: there's Wallaby dot js.
- 10:36Speaker 1: Okay, Wallaby dot js. Now things are getting interesting. What's
- 10:39Speaker 1: so special about it?
- 10:40Speaker 2: Well, it's a paid tool, but it's worth it. It
- 10:42Speaker 2: integrates right into your code editor and gives you real
- 10:45Speaker 2: time feedback as you're writing tests. You see errors immediately
- 10:49Speaker 2: you can step through your code and see what's happening.
- 10:51Speaker 1: Time travel do bugging. That's the thing you mentioned.
- 10:53Speaker 2: Earlier, right, Yep, it's super helpful for finding those tricky bugs.
- 10:58Speaker 2: Wallaby can really speed up you're testing and debugging process.
- 11:02Speaker 1: So it's like having a personal testing assistant right there
- 11:04Speaker 1: in your code. Awesome. What about Cypress you mentioned that
- 11:08Speaker 1: earlier too.
- 11:08Speaker 2: Yeah, Cypress is a whole other level. It's for end
- 11:11Speaker 2: to end UI testing. Imagine a robot user that can
- 11:14Speaker 2: actually interact with your app, like browsing through pages, clicking
- 11:18Speaker 2: on things, filling out forms.
- 11:19Speaker 1: So Cypress is like testing the entire user journey from start.
- 11:23Speaker 2: To finish exactly. It's especially powerful for testing complex workflows
- 11:27Speaker 2: and making sure your app behaves correctly in like real
- 11:30Speaker 2: world scenarios. And the best part is it integrates seamlessly with.
- 11:34Speaker 1: RTL, so you get that user centric approach even for
- 11:37Speaker 1: those big end to end tests.
- 11:39Speaker 2: Right. And Cypress has these things called page objects, which
- 11:42Speaker 2: are like blueprints for different parts of your app. They
- 11:45Speaker 2: make it way easier to organize and maintain your tests,
- 11:48Speaker 2: especially for large projects.
- 11:49Speaker 1: So Cypress is like the ultimate test pilot, putting your
- 11:52Speaker 1: app through as paces and making sure it can handle
- 11:55Speaker 1: anything users throw at it.
- 11:56Speaker 2: Ah exactly, and Cypress can do more than just simulate
- 11:59Speaker 2: user apps. You can test APIs directly from your UI, tests,
- 12:03Speaker 2: check network requests, even interact with databases. It's really powerful.
- 12:07Speaker 1: Okay, so the Cypress can basically test every layer of
- 12:10Speaker 1: your application. Pretty impressive. What about those design patterns you
- 12:14Speaker 1: mentioned for writing better subriss tests.
- 12:17Speaker 2: Right, One of the most useful is the page object
- 12:20Speaker 2: model or pom POM.
- 12:22Speaker 1: That's about making tests more organized and less likely to break. Right.
- 12:26Speaker 2: The idea is to create separate pieces of code that
- 12:28Speaker 2: represent different pages or sections of your app. Each page
- 12:32Speaker 2: object like encapsulates all the logic for interacting with that
- 12:35Speaker 2: specific part of the UI.
- 12:36Speaker 1: So instead of having all your Cypress commands scattered throughout
- 12:39Speaker 1: your tests, you group them logically within these page objects.
- 12:42Speaker 1: Makes things way cleaner.
- 12:43Speaker 2: Exactly, and it makes your tests less sensitive to changes
- 12:47Speaker 2: in your HTML. If the layout changes, you just update
- 12:50Speaker 2: the page object, not a bunch of different tests.
- 12:52Speaker 1: Saves a ton of time, less fixing tests, more time
- 12:54Speaker 1: for new features. What about those custom Cypress commands you mentioned, Oh,
- 12:58Speaker 1: those are great.
- 12:59Speaker 2: They let you if you extend Cypress's functionality and create
- 13:02Speaker 2: reusable pieces of code for your tests. Think about a
- 13:05Speaker 2: common task like logging in or creating a new user.
- 13:08Speaker 1: Stuff we do all the time and testing.
- 13:10Speaker 2: Instead of writing the same Cypress commands over and over,
- 13:13Speaker 2: you can create a custom command that does that whole
- 13:15Speaker 2: thing for you.
- 13:16Speaker 1: So it's like creating your own little library of testing
- 13:18Speaker 1: actions exactly.
- 13:20Speaker 2: Makes your tests shorter, easier to read, and less prone
- 13:24Speaker 2: to errors. And Cypress can even test APIs directly from
- 13:27Speaker 2: your UI tests. You can make sure your front end
- 13:30Speaker 2: and back end are talking to each other properly without
- 13:33Speaker 2: writing separate tests.
- 13:34Speaker 1: Wow, so I can test my UI and API in
- 13:36Speaker 1: the same Cypress test suite. That's amazing. Saves so much
- 13:40Speaker 1: time and effort.
- 13:41Speaker 2: Right and by testing your API from the user's perspective,
- 13:44Speaker 2: you're simulating real world usage more accurately. You can verify
- 13:48Speaker 2: that data is flowing correctly and your app is responding
- 13:51Speaker 2: as it should.
- 13:52Speaker 1: So Cypress really is like the ultimate testing tool, can
- 13:55Speaker 1: handle anything you throw at it.
- 13:57Speaker 2: It's really powerful. Now let's talk about something that can
- 14:00Speaker 2: be really helpful when you're working with like non technical
- 14:03Speaker 2: folks writing tests in a way that anyone can understand.
- 14:07Speaker 1: Okay, interesting, how do we do that?
- 14:10Speaker 2: Well? Cypress works great with something called Cucumber, which uses
- 14:13Speaker 2: this language called Girkin. It's a simple, plain English way
- 14:17Speaker 2: to write tests. Uses keywords like given, when.
- 14:21Speaker 1: And then, so it's like writing out the steps of
- 14:24Speaker 1: a test as if you were explaining it to someone.
- 14:27Speaker 1: Makes it clear what's being tested and what should happen exactly.
- 14:30Speaker 2: It bridges the gap between developers and everyone else. Cypers
- 14:34Speaker 2: can run these Gurkin tests automatically, so everyone's on the
- 14:36Speaker 2: same page about what's being tested and what success looks like.
- 14:39Speaker 1: That's awesome. A shared language for testing. I like that.
- 14:42Speaker 1: So we've got Cypress for end to end testing. But
- 14:44Speaker 1: what about testing those less than ideal situations, like when
- 14:48Speaker 1: things go wrong? How do we make sure our app
- 14:51Speaker 1: handles those gracefully?
- 14:52Speaker 2: Ah, you're talking about edge cases, those tricky little bugs
- 14:56Speaker 2: that pop up when you least expect them, and that's
- 14:59Speaker 2: where error handling and boundary testing come in. The book
- 15:02Speaker 2: has some great advice on using RTL for those situations,
- 15:06Speaker 2: making sure your app stays stable and user friendly even
- 15:09Speaker 2: when things go sideways.
- 15:11Speaker 1: So how do we do that with RTL?
- 15:13Speaker 2: One way? Is to use RTL's fire event method, you
- 15:16Speaker 2: can simulate all sorts of events that might trigger errors
- 15:19Speaker 2: like invalid input, network problems, unexpected data from an API.
- 15:24Speaker 1: So we're basically trying to break our app on purpose
- 15:27Speaker 1: in a safe environment to see how it reacts.
- 15:29Speaker 2: Uh huh yeah, pretty much. And then using RTL's querying methods,
- 15:32Speaker 2: we can check that the right error messages are shown
- 15:35Speaker 2: or that the app re covers properly.
- 15:36Speaker 1: So instead of crashing, the app can handle errors gracefully
- 15:40Speaker 1: and guide the user back on track.
- 15:42Speaker 2: Exactly. It's about making the user experience as robust and
- 15:45Speaker 2: resilient as possible, and the book gives some really good
- 15:47Speaker 2: examples of how to do that with URTL.
- 15:49Speaker 1: I like that thinking like a user, but also thinking
- 15:52Speaker 1: like a user who might make mistakes or run into problems.
- 15:54Speaker 2: Exactly. It's all part of building a truly user centric application.
- 16:00Speaker 2: Well have you ever had to make changes to your
- 16:01Speaker 2: app but you were worried about accidentally breaking something else
- 16:05Speaker 2: in the process.
- 16:05Speaker 1: Oh yeah, regressions every developer's worst nightmare. You fix one
- 16:10Speaker 1: thing and break two other things.
- 16:11Speaker 2: Uh huh yep. That's where regression testing comes in, and
- 16:15Speaker 2: RTL has us covered there too. How So RTL gives
- 16:18Speaker 2: you the tools to write tests that cover all the
- 16:20Speaker 2: important parts of your app. With a good set of tests,
- 16:23Speaker 2: you can make changes confidently knowing that your tests will
- 16:26Speaker 2: catch any regressions before they go live.
- 16:29Speaker 1: So it's like having a safety net making sure you
- 16:30Speaker 1: don't accidentally introduce new bugs when you're fixing old ones.
- 16:33Speaker 2: Exactly. And the book even shows you how to refactor
- 16:36Speaker 2: old tests written with enzyme so you can bring those
- 16:38Speaker 2: up to speed with RTL's approach.
- 16:40Speaker 1: Awesome. So RTL is great for catching regressions in general.
- 16:44Speaker 1: But the book also had a specific example about a
- 16:46Speaker 1: budget app, right, something that helps users track their spending.
- 16:49Speaker 2: Yeah, it shows how to write integration tests for that app,
- 16:52Speaker 2: verifying that all the components work together correctly.
- 16:55Speaker 1: So we're testing the whole budget management flow, not just
- 16:58Speaker 1: individual pieces.
- 17:00Speaker 2: Make sense, right, And to show regression testing in action,
- 17:03Speaker 2: the book goes through updating the material UI library that
- 17:07Speaker 2: the app uses. That's something that can often introduce regressions
- 17:11Speaker 2: if you're not careful.
- 17:12Speaker 1: Oh, I've been there, updating a library and suddenly half
- 17:15Speaker 1: your app is broken.
- 17:15Speaker 2: By having a good set of tests like the ones
- 17:18Speaker 2: shown in the book, you can catch those regressions early
- 17:21Speaker 2: and fix them before they affect your users.
- 17:23Speaker 1: That's so important. So URTL helps us catch regressions, refacture
- 17:28Speaker 1: old tests, and even test complex workflows. Is there anything
- 17:32Speaker 1: it can't do?
- 17:33Speaker 2: Uh huh. Well, it's not magic, but it's pretty close.
- 17:36Speaker 2: It definitely makes testing way easier and more effective for sure.
- 17:40Speaker 1: And the book also talks about best practices for writing
- 17:43Speaker 1: tests right, things like how to name them, how to
- 17:46Speaker 1: organize them.
- 17:46Speaker 2: Yep, no one wants to deal with a messy, confusing
- 17:50Speaker 2: test suite. It's like trying to find a needle in
- 17:52Speaker 2: a haystack.
- 17:53Speaker 1: Right. So it's not just about writing tests, it's about
- 17:55Speaker 1: writing tests that are easy to understand and maintain exactly.
- 17:59Speaker 2: We should tests with the same care we treat our
- 18:02Speaker 2: production code. Now there's one more challenge. I want to
- 18:05Speaker 2: mention something. A lot of developers struggle with choosing the
- 18:09Speaker 2: right query selectors to target specific elements in their components.
- 18:13Speaker 2: It can be a real pain, especially with complex HTML structures.
- 18:18Speaker 1: Oh tell me about it. I've spent way too much
- 18:20Speaker 1: time trying to figure out the perfect selector.
- 18:22Speaker 2: Well, thankfully, there's this awesome tool called testing Playground. It's interactive.
- 18:27Speaker 2: You can paste in your components HTML and visually see
- 18:30Speaker 2: how different selectors work.
- 18:32Speaker 1: Like a treasure map that leads you right to the
- 18:33Speaker 1: element you're looking for.
- 18:34Speaker 2: Uh huh, yeah, exactly. It takes all the guesswork out
- 18:37Speaker 2: of it, and Testing Playground doesn't just find any selector.
- 18:40Speaker 2: It tries to find the best one, following RTL's best practices.
- 18:44Speaker 1: So it's not just about finding a selector that works,
- 18:46Speaker 1: it's about finding the one that's going to be the
- 18:48Speaker 1: most robust and reliable in the long run. I love that.
- 18:51Speaker 2: Now, for those who really want to take their testing
- 18:53Speaker 2: workflow to the next level, there's Wallaby dot js gobytjs.
- 18:57Speaker 1: That's one with the time traveling debugging right. Yeah, yep.
- 19:00Speaker 2: It's a paid tool, but it's amazing. It integrates directly
- 19:03Speaker 2: into your code editor and gives you instant feedback as
- 19:06Speaker 2: you write tests. You see errors pop up right away.
- 19:08Speaker 2: You can step through your code execution and even rewind
- 19:11Speaker 2: time to see exactly when something went wrong.
- 19:14Speaker 1: That's incredible, like having a testing superpower.
- 19:16Speaker 2: Uh huh. It really does feel that way. Sometimes. It
- 19:19Speaker 2: can make writing and debugging tests so much faster and
- 19:23Speaker 2: more enjoyable.
- 19:24Speaker 1: So Wallaby dad js is definitely something to check out
- 19:26Speaker 1: if you're serious about testing.
- 19:28Speaker 2: Absolutely. Now, let's shift gears and talk about end to
- 19:31Speaker 2: end testing with Cypress.
- 19:33Speaker 1: Cypress, that's the framework that simulates real user interactions.
- 19:37Speaker 2: Right exactly. Imagine a robot user that can browse your website,
- 19:42Speaker 2: click buttons, fill out forms, and do everything a real
- 19:45Speaker 2: user would do.
- 19:46Speaker 1: So it's like testing the entire user journey from start
- 19:49Speaker 1: to finish.
- 19:49Speaker 2: Right, And Cypress makes it surprisingly easy to write these
- 19:52Speaker 2: kinds of tests and has a really nice visual test
- 19:55Speaker 2: runner that shows you exactly what's happening as your tests
- 19:58Speaker 2: are running, so you.
- 19:58Speaker 1: Can actually watch the about user interact with your app.
- 20:01Speaker 1: That's awesome.
- 20:02Speaker 2: Yeah, it's really cool and it's super helpful for finding
- 20:05Speaker 2: and fixing problems. But Cypress can do more than just
- 20:08Speaker 2: simulate user interactions. You can also use it to test APIs,
- 20:13Speaker 2: verify network requests, and even interact with databases. It's a
- 20:17Speaker 2: really comprehensive testing solution.
- 20:19Speaker 1: Sounds like Cypress has it all.
- 20:20Speaker 2: It's pretty amazing. But even with a tool as powerful
- 20:23Speaker 2: as Cypress, it's important to write your tests in a
- 20:26Speaker 2: way that's organized and easy to maintain. That's where design
- 20:30Speaker 2: patterns come in.
- 20:31Speaker 1: Design patterns those are like looprints for structuring your code
- 20:33Speaker 1: right exactly.
- 20:35Speaker 2: One of the most useful patterns for Cypress tests is
- 20:38Speaker 2: the page object model or POM for short.
- 20:41Speaker 1: POM that sound similiar. It's about making your tests less
- 20:44Speaker 1: likely to break when your code changes.
- 20:46Speaker 2: Right, you got it. The idea is to create separate
- 20:49Speaker 2: classes or modules that represent each page or section of
- 20:52Speaker 2: your app. These page objects contain all the logic for
- 20:56Speaker 2: interacting with that specific part of the UI.
- 20:58Speaker 1: Well. Instead of having your Cypress command and scattered all
- 21:00Speaker 1: over the place, you group them logically within these page objects.
- 21:04Speaker 2: Exactly, and by abstracting away the UI interactions, your tests
- 21:08Speaker 2: become less brittle. If the layout of a page changes,
- 21:11Speaker 2: you only need to update the page object, not a
- 21:14Speaker 2: ton of different tests.
- 21:15Speaker 1: Makes sense, less maintenance, more time for building new stuff right.
- 21:19Speaker 2: The book also mentions the power of using custom Cypress commands.
- 21:23Speaker 1: Custom commands what are those?
- 21:25Speaker 2: They let you create reusable chunks of code for common
- 21:28Speaker 2: actions in your tests. For example, imagine you have a
- 21:31Speaker 2: common workflow in your app, like logging in or creating
- 21:34Speaker 2: a new.
- 21:34Speaker 1: Account, stuff we do all the time in testing exactly.
- 21:38Speaker 2: Instead of writing those same Cypress commands over and over
- 21:41Speaker 2: in different tests, you can create a custom command that
- 21:43Speaker 2: encapsulates that entire workflow.
- 21:45Speaker 1: So it's like building your own library of testing actions.
- 21:48Speaker 1: Makes your test more concise and less repetitive.
- 21:51Speaker 2: Exactly, and less repetition means fewer errors and inconsistencies in
- 21:55Speaker 2: your tests.
- 21:56Speaker 1: I could see how that would be super helpful.
- 21:57Speaker 2: Cypress also lets you test your apid directly from your
- 22:00Speaker 2: UI tests, so you can make sure the front end
- 22:03Speaker 2: and back end are talking to each other correctly without
- 22:06Speaker 2: writing separate API tests.
- 22:07Speaker 1: Wow. So I can test both my UI and API
- 22:11Speaker 1: from the same Cypress test suite. That's pretty amazing. It
- 22:14Speaker 1: saves a ton of time.
- 22:15Speaker 2: It really does, and it's a more realistic way to
- 22:17Speaker 2: test because in the real world, your front end and
- 22:19Speaker 2: back end are always interacting.
- 22:21Speaker 1: Okay, I'm sold on Cypress. It sounds like an incredibly
- 22:24Speaker 1: powerful and versatile tool.
- 22:26Speaker 2: It really is. And you know, even though Cypress is
- 22:28Speaker 2: great for technical folks, sometimes we need to involve people
- 22:32Speaker 2: who aren't as familiar with code. That's where Cucumber comes in.
- 22:35Speaker 1: Cucumber that's one that uses plain English to write tests.
- 22:38Speaker 2: Right exactly. It uses a language called Girkin, which is
- 22:41Speaker 2: super readable and easy to understand even for non technical folks.
- 22:46Speaker 2: You write your tests using keywords like given, when, and then,
- 22:50Speaker 2: so it almost reads like a story.
- 22:52Speaker 1: So instead of writing code, you're basically writing out the
- 22:54Speaker 1: steps of the test in plain English. Right.
- 22:57Speaker 2: It's a great way to bridge the communication gap between
- 23:00Speaker 2: developers and stakeholders and to make sure everyone's on the
- 23:03Speaker 2: same page about what's being tested and what the expected
- 23:06Speaker 2: outcome should be.
- 23:07Speaker 1: So Cucumber is like a universal language for testing that
- 23:10Speaker 1: everyone can understand exactly.
- 23:12Speaker 2: And the best part is Cypress has built in support
- 23:14Speaker 2: for Cucumber, so you can write your tests in this
- 23:17Speaker 2: plain English format and Cypress will run them automatically.
- 23:20Speaker 1: That's awesome. It makes testing more collaborative and accessible to everyone.
- 23:24Speaker 2: Now, while all these testing tools and techniques are incredibly valuable,
- 23:28Speaker 2: it's important to remember that testing is ultimately about creating
- 23:31Speaker 2: software that meets the needs of our users.
- 23:34Speaker 1: Right. That's the core of the user centric.
- 23:36Speaker 2: Approach, and a big part of that is making sure
- 23:39Speaker 2: our apps are accessible to everyone, regardless of their abilities.
- 23:42Speaker 1: Accessibility is so important. I have a friend who uses
- 23:45Speaker 1: a screen reader and she's constantly running into websites and
- 23:48Speaker 1: apps that are just impossible to use.
- 23:51Speaker 2: It's a real shame, it is, But luckily RTL can
- 23:54Speaker 2: help us address these accessibility challenges by encouraging us to
- 23:58Speaker 2: test from the user's perspective. RTL naturally leads us to
- 24:01Speaker 2: consider how people with different needs and abilities will experience
- 24:05Speaker 2: our applications.
- 24:06Speaker 1: So if we're testing a form, for example, we might
- 24:09Speaker 1: make sure it can be navigated using only the keyboard,
- 24:12Speaker 1: or that it provides clear instructions for screen.
- 24:15Speaker 2: Readers exactly, And there are tools that can help us
- 24:18Speaker 2: with this. The book mentions just Acts, a library that
- 24:22Speaker 2: can run automated accessibility audits on your React components.
- 24:25Speaker 1: So it's like having an accessibility expert built right into
- 24:28Speaker 1: your testing process exactly.
- 24:30Speaker 2: By integrating just Acts into our workflow, we can catch
- 24:34Speaker 2: and fix accessibility issues early on before they impact our users.
- 24:38Speaker 1: This is all great stuff. It really feels like RTL
- 24:40Speaker 1: is a game changer for testing, especially for building user
- 24:43Speaker 1: centric and accessible applications.
- 24:45Speaker 2: I agree. It's a powerful approach that can really improve
- 24:48Speaker 2: the quality of the software we build.
- 24:50Speaker 1: Right. So we've covered a lot of ground in this
- 24:52Speaker 1: deep dies as we wrap things up, What are some
- 24:54Speaker 1: of the key takeaways you'd like our listeners to remember.
- 24:57Speaker 2: I think the biggest one is that RTL is more
- 25:00Speaker 2: than just a collection of tools and techniques. It's a
- 25:02Speaker 2: mindset shift towards user centric testing. It's about building software
- 25:07Speaker 2: that's not just functional, but also delightful, accessible, and truly
- 25:12Speaker 2: valuable to the people who use it. And remember, the
- 25:15Speaker 2: tools we talked about today are just the starting point.
- 25:19Speaker 2: The real power of RTL lies in its ability to
- 25:22Speaker 2: help us think differently about testing and build a culture
- 25:25Speaker 2: of quality in our development process.
- 25:27Speaker 1: I love that it's not just about the code, it's
- 25:29Speaker 1: about the people exactly.
- 25:31Speaker 2: So as you continue your journey with React Testing Library,
- 25:34Speaker 2: always remember to keep the user at the center of
- 25:36Speaker 2: your thinking. Ask yourself, how can my tests reflect the
- 25:40Speaker 2: real world experiences of my users and strive to create
- 25:44Speaker 2: software that not only works, but also makes a positive
- 25:47Speaker 2: impact on the world.
- 25:48Speaker 1: That's a fantastic message. Thanks for sharing your expertise with
- 25:51Speaker 1: us today. It's been a really eye opening conversation.
- 25:54Speaker 2: The pleasure was online. It's been great chatting about testing
- 25:56Speaker 2: all right.
- 25:57Speaker 1: Welcome back to our React Testing Library deep dive. I
- 25:59Speaker 1: feel like we've covered so much already, but I'm sure
- 26:01Speaker 1: there's even more to uncover.
- 26:03Speaker 2: Oh absolutely, We've gone deep on the tools and the
- 26:06Speaker 2: how to, But now I think it's time to like
- 26:09Speaker 2: zoom out a bit see the bigger picture of what
- 26:12Speaker 2: we've learned.
- 26:12Speaker 1: Okay, I like that. Yeah, So beyond just you know,
- 26:16Speaker 1: writing better tests, what's the bigger takeaway here?
- 26:19Speaker 2: Well, I think this whole shift toward user centric testing
- 26:23Speaker 2: it really changes how we think about software quality. It's
- 26:27Speaker 2: not just does the code work anymore? It's more about, like,
- 26:31Speaker 2: does this actually serve the people using it?
- 26:33Speaker 1: Right? Because at the end of the day, we're building
- 26:35Speaker 1: stuff for humans, not for computers exactly.
- 26:37Speaker 2: And this user centric idea it goes way beyond just software.
- 26:40Speaker 2: Think about any product you use, your car, your phone, website.
- 26:44Speaker 1: Yeah, they've all been designed with the user in mind,
- 26:46Speaker 1: haven't they.
- 26:47Speaker 2: For sure, the best products are the ones that just
- 26:49Speaker 2: like fit seamlessly into your life. They anticipate what you need,
- 26:52Speaker 2: they make the experience enjoyable.
- 26:54Speaker 1: And that's what RTL helps us do with software. By
- 26:57Speaker 1: focusing on the user, we can build apps that are
- 26:59Speaker 1: not just functional, but also intuitive and fun to use,
- 27:02Speaker 1: things that people actually want.
- 27:04Speaker 2: To use You nailed it, And there's another piece to
- 27:07Speaker 2: this user centric approach accessibility, making sure our apps work
- 27:12Speaker 2: for everyone, regardless of their abilities. That's not just about
- 27:17Speaker 2: being ethically responsible, it's about creating a truly user friendly
- 27:21Speaker 2: experience for everyone.
- 27:23Speaker 1: Yeah, that's a really good point. I've got a friend
- 27:25Speaker 1: who uses a screen reader and she's always running into
- 27:28Speaker 1: websites and apps that are just impossible to navigate. It's
- 27:31Speaker 1: a huge problem.
- 27:32Speaker 2: It really is, and thankfully URTL can help a lot
- 27:35Speaker 2: with that. When we're testing from the user's point of view,
- 27:38Speaker 2: we naturally start thinking about how people with different abilities
- 27:41Speaker 2: might use our app.
- 27:42Speaker 1: So like if we're testing a form, we might make
- 27:44Speaker 1: sure it works well with just the keyboard, or that
- 27:47Speaker 1: it gives clear instructions for screen reader users exactly.
- 27:50Speaker 2: And there are even tools to help with this. The
- 27:52Speaker 2: book talked about jest acts, which is a library that
- 27:55Speaker 2: can run automated accessibility checks on your React components.
- 27:58Speaker 1: So it's like having an accessibility the expert built right
- 28:01Speaker 1: into your testing process.
- 28:02Speaker 2: Pretty much, and by using just x you can catch
- 28:05Speaker 2: those accessibility issues early on before they become a problem
- 28:09Speaker 2: for your users.
- 28:10Speaker 1: That's awesome. Okay, So as we wrap up our deep
- 28:12Speaker 1: dive into React Testing Library, What are some of the
- 28:15Speaker 1: key takeaways you want our listeners to walk away with.
- 28:17Speaker 2: Well, first off, I want to emphasize again that RTL
- 28:20Speaker 2: isn't just a testing library. It's a whole philosophy, a
- 28:24Speaker 2: way of thinking about quality from the user's point of view.
- 28:27Speaker 1: It's about building software that's not just functional, but delightful
- 28:30Speaker 1: to use. Yeah, accessible to everyone, and something that genuinely
- 28:34Speaker 1: brings value to people exactly.
- 28:36Speaker 2: And all these tools and techniques we talked about, they're
- 28:38Speaker 2: just the beginning. The real magic of RTL is how
- 28:41Speaker 2: it changes our mindset about testing and helps us build
- 28:44Speaker 2: a culture of quality in our teams.
- 28:47Speaker 1: Yeah, it's not just about the code, it's about the
- 28:48Speaker 1: people for sure.
- 28:50Speaker 2: So to everyone listening as you start exploring React Testing Library,
- 28:54Speaker 2: keep the user at the heart of everything you do.
- 28:56Speaker 2: Think about how your tests can reflect real world use cases,
- 29:00Speaker 2: and always try to build software that not only works,
- 29:02Speaker 2: but also makes a positive difference in the world.
- 29:05Speaker 1: That's a great message to end on. Thanks so much
- 29:07Speaker 1: for joining us on this deep dive into a React
- 29:09Speaker 1: testing library. It's been an awesome and inspiring conversation.
- 29:12Speaker 2: My pleasure I always love talking about testing.
- 29:15Speaker 1: And to our listeners, thanks for tuning in to this
- 29:17Speaker 1: deep dive. We hope you learned a lot about React testing,
- 29:21Speaker 1: Keep learning, keep exploring, and keep building amazing software.