Check out a free preview of the full API Design in Node.js, v3 course:
The "Testing with Jest Overview" Lesson is part of the full, API Design in Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott gives a summary of what Jest is, and what is happening with the tests so that students can write their own.

Get Unlimited Access Now

Transcript from the "Testing with Jest Overview" Lesson

>> Scott Moss: Who here writes tests for their code? [LAUGH] Nobody writes tests? You know what? I'm not a fan of-
>> Scott Moss: Yeah. I'm not a fan of writing tests for like front end stuff. Cuz it's so much stuff, but for back end like I literally write test for everything.

[00:00:21] In fact I got to the point now where on the back end I write all my tests first. Which I know it's like who does that? But I do. I write all my tests first on the back end. Because it's so easy to like think about on the back end.

[00:00:32] Then I write my code and it helps me figure out what to do. So I love writing tests on the back-end because it's so simple. So what I wanted to talk about is we've been looking at tests and you've been fixing tests all day. I just wanted to show you what was actually happening in this testing setup, so when you go out and do your own testing setup, you'd know what's actually happening because I had to do a lot to get this to work.

[00:00:52] One is I'm using Jest to write test. Jest is basically a piggy back off of Mocha which is basically a piggy back off of Jasmine. But Jest is definitely the best one in my opinion. Because it's very much like Jasmine. And where it comes with every single thing you need like an assertion library and mocks but it's more modern and not as bad as Jasmine.

[00:01:16] It just does more stuff. It has built-in checking of, what do they call it. I can't even think of the name of it. Percentage checking, or coverage, built-in coverage. It's got built-in mocks. All the way to mocking modules out completely, it's got everything. It's made by Facebook so it's pretty well-backed.

[00:01:36] So we're using Jest. And then Jest by default runs all tests. And when I say test I mean anything that has a describe block, a top level describe block. It runs all of them in parallel, at the same time. That's how it runs it really fast. But that creates issues.

[00:01:55] One, when you log inside of a test, sometimes you don't see those logs. If you are ever logged in one of these tests and you didn't see it pop up, that's because it runs it in parallel and it may or may not show up. It runs them all in different chopped processes at the same time.

[00:02:09] It spawns them. So there's a master process from where you ran that every described block is ran in a child process. So you're logging down here, but you're looking right here, so you might not see those logs. Maybe, maybe not, and that's because it runs into parallel by default.

[00:02:23] You can run it in serially, but it's passing a flag to prevent that, but it helps, speeds it up. But it introduces problems, because we write tests stateless, as part of our database. So I had to introduce a script that connects to a different database every single time for every single describe block, and then delete that database.

[00:02:45] So if you go look at test-db.setup, you'll see where I'm doing that, but that's basically what's happening here, is I'm connecting a database before every single describe block. That's happening in parallel and then I completely delete that database after it's done. So for every describe blocks you have that's how many databases are being created.

[00:03:04] That allows you to run in parallel without having to have stateful tests which is basically a test knowing about a test before it, which is really bad. You don't want to do that. It's really hard to write tests like that.
>> Scott Moss: And then other thing is everything in a __tests__ folder that has a spec or a .test on it will be tested by default by Jest.

[00:03:27] So you don't have to do anything. So that's all the stuff I had to do to get it set up. And there's obviously some options. In the packets.JSON there'll be pass suggest to help us ignore certain files like the disk folder. Point to that setup file that I talked about and make sure that we're running a node, and that's about it.

[00:03:46] Everything else is just boiler plate. So I had to do all that to get set up. Other than that, tests are pretty simple. You don't really have to do anything else.