This course is no longer being maintained. We recommend the testing section of the Intermediate React course instead. We now recommend you take the Intermediate React, v4 course.
Transcript from the "Testing Fundamentals" Lesson
>> Kent C. Dodds: So we're gonna talk about different forms of testing really briefly. This is one form of testing, Static Code Analysis. This is happening through a tool called ESLint. If you have your editor configured to work with ESLint, you might see some ESLint warnings as you're going through. ESLint can be really helpful at identifying very simple bugs.
[00:00:23] There's also static type checkers like Flow that can catch also, pretty simple bugs. But, in general, the nice thing about these tools is that they work statically and so they don't have to run your code. So you don't have to really write these tests. With Flow, you're sort of writing tests in the source with the typing.
[00:00:41] But yeah, in general, there's not a whole lot of effort around writing these kinds of tests or configuring these kinds of tools. So I highly recommend that you use these tools. We will not be using these tools today. I thought it was enough just to teach you testing React.
[00:01:00] It'd be a whole another workshop to teach you a static type checker. But in your applications, I definitely in React recommend using flow-typed or TypeScript, and then obviously ESLint is kind of a given. One thing I should say about ESLint is please don't enforce style rules with ESLint like code style, code formating.
[00:01:23] It's silly, because we have Prettier, an amazing tool that will automatically format your code for you. Use that instead, and then us ESLint just to catch actually problems like. Variables aren't defined, stuff like that. Because the last thing I wanna do is see a red squiggly because I have an extra space after the function keyword.
[00:01:45] My code's still gonna work, and then Prettier's gonna fix that when I hit the save key anyway. So here's just a quick example of a unit test, don't look to closely because this is you first exercise. Just kidding, feel free to look as closely as you like. So we have React and ReactDOM.
[00:02:02] And when you wanna render an application, you ReactDOM.render. You could do the same thing in your test, there's nothing wrong with doing that. And I'll show you with Jest, you can configure it to simulate the browser environment in Node. And it's using jsdom, so doing document.creatElement totally works.
[00:02:22] And yeah, you just use the component the way that you would use it in a source code elsewhere. And then you can make assertions based on what is rendered. So we'll look deeper into that, but unit tests are generally pretty straightforward. And from a purist standpoint, a unit test will mock out all of the dependencies.
[00:02:45] I care less about that. So for me, the unit versus integration test is a little bit fluid. But in general, unit test is a kind of a smaller test, testing something in particular. Integration tests with regard to React is a little bit kind of a bigger experience. And so here we're rendering the entire application with the router.
[00:03:10] And we can specify, hey, route to this page and I'll show you how to do all this stuff. And so it takes a little bit more work. You have to mock out API calls and stuff. But with this, you're rendering more of the many components working together. Yeah, so that's the idea of an integration test.
[00:03:36] You get a lot more coverage with an integration test than you do with the unit test. But in general, they can be a little bit slower and also can be a little harder to set up, because of the network requesting things that you have to mock out. End-to-end tests is a totally different thing.
[00:03:55] It's not Jest. This is with Cypress. And you're kind of treating the application as a black box that user, from a user's perspective clicking on things and typing in some text. And then verifying the state of the application sort of in a way the user would. And so that is an example of an end-to-end, and we'll look into all of these forms of tests.