Check out a free preview of the full Introduction to Node.js course:
The "Anatomy of Tests" Lesson is part of the full, Introduction to Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Scott gives a brief description of test suites, assertion libraries, mocks, and spies.

Get Unlimited Access Now

Transcript from the "Anatomy of Tests" Lesson

[00:00:00]
>> Scott Moss: Cool, so we're gonna talk about the anatomy of tests. There's many tools for the same job, but basically there's a couple things. There's your code to be tested, right, and that's all the code that we wrote today, that's the code that you want to test. There's a test suite, all right.

[00:00:16] A test suite is responsible for helping organize your tests, provide hooks, overall environment. So if we go back to our example, a test suite would be if you go look inside of modules, the one you already did, test.js, all this stuff right here, describe, test, test, all this stuff right here.

[00:00:34]
>> Scott Moss: Where did this come from? We didn't require this anywhere. And it's definitely not a global in Node, cuz we never talked about it, it's not a documentation. So where is it coming from? Well, when you ran the jest command, it makes all these variables available in your environment as a globals.

[00:00:53] So that's where they came from. So if you try to execute this file with just Node, Node will be like, I have no idea what describe is. I have no idea what test is, but if you execute it with the Jest CLI, which uses the Node runtime inside of it, it would know what it is, cuz it injects it for you.

[00:01:08] Cuz it's just a CLI, you built a CLI, so you know how those work, you can pretty much do whatever you want. So they just inject these global variables for you. And that's where they come from. So that's what a test suite is. It's this thing, it helps you organize and describe, and get your test together and gives you the framework to actually write some test.

[00:01:26] It's not gonna actually do the searchings for you by itself, but it will help organize it for you. The next one is an assertion library. An assertion library is a thing that actually does the actual comparisons in your test. So if we go back to here, everything that says like expect, expect, that's the assertions.

[00:01:43] I'm expecting (data.users).toHaveLength(1), that's the assertion part. And there's a thousand libraries for that, so many of them. So many have the same name, there's literally one called expect.js, and it's not the one you're looking at. So there's just so many of them with the same name, it's ridiculous.

[00:02:01] The one we're using is called Jest, and it comes with everything. It's a sweet, it's an assertion library, it's got everything you need, that's what I've been using lately. And then, you have mocks and spies, which gets really advance and honestly, some people might argue that's like, if you have these mocks and spies, then you're writing your code wrong.

[00:02:19] Maybe, but sometimes you import third party code, that's asynchronous, like you're using Stripe on your server to attack people's credit card, and you have function that you wanna test, and that function is using Stripe. You don't wanna test Stripe every time you run your test, you don't wanna hit Stripe API, so may be you wanna mock those out.

[00:02:35] Say hey, don't actually go to Stripe, just mock it out and return this fake promise real quick, cuz I don't wanna hit Stripe. So yeah, that's where you need mocks. And spies are basically like, I have a function that for some reason, I cannot test. It's inside this other function that I can test.

[00:02:50] So I'm gonna spy on that internal function to see if it got invoked, and that's the only way I can test it. So that's what a spy is, or if you wanna test to see if a callback was executed, you would spy on a callback to see if it was executed.

[00:03:01] And then, the testing framework would allow you to know, hey this spy was called four times, five times with these arguments. So that's what a spy is exactly what it sounds like, you're spying on things. But those are like really advance things. If you're really crazy about testing, you'll love these things, if you hate testing, you will hate these things.

[00:03:18] I find myself not using mocks and spies too much, only for mocking out asynchronous third party code, that's almost the only time I ever use them, and that's for mock. Spies, almost never use them, I just don't write my code in a way where ever I need a spy.

[00:03:31] I just export everything so I can test it, so I haven't ran into that problem yet. But that's the basics of an anatomy of a test, of any type of test, specifically with JavaScript.