This course has been updated! We now recommend you take the Introduction to Node.js, v2 course.

Check out a free preview of the full Introduction to Node.js course:
The "Testing Node Libraries" 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 details how Node js has advanced to be extremely versatile over the last few iterations, and gives several use cases for testing in Node js.

Get Unlimited Access Now

Transcript from the "Testing Node Libraries" Lesson

>> Scott Moss: Testing is pretty cool in Node actually, and it's very flexible, and it has evolved from just kinda garbage to amazing. But basically, if you export your modules, you can test them. That's basically testing in Node, but there's a lot more, we're gonna cover that. But at the end of the day, a test is just a file, it's a JavaScript file.

[00:00:23] And if you're trying to test a module that's not exported, then how can you test it? So you still have to export stuff. Even if you have modules that aren't being used in other pieces of code but outside the file that they live in, you still have to export those if you wanna test them.

[00:00:39] Otherwise, the only way you can test them is by testing the code that uses them, and then that gets crazy. So it's better just to export than to write really great unit tests. So like I said, before you can test your code, make sure it is testable. As long as you can export what you want to test, you should be able to test it.

[00:00:55] There are other concerns, different libraries or different frameworks that kinda make this weird, like with Mox and Spies and stuff like that. But in such an edge case that if you run in to it, you will just, I'm sure there's issues for it. You can pretty much test any thing in node.js.

[00:01:15] That includes browser apps that use the DOM. I know I said there is no DOM in Node, and that is true, but you can still test browser apps that use the DOM in Node. You might be thinking, how does that work? Well, there's a thousand ways to do that.

[00:01:28] So if I ask this question four years ago, the way that will work is, you would actually start the browser, like a headless version. People will use something called Karma, if you ever heard of Karma. It's a very old testing framework that was Angular used to use or whatever, and it would basically start up a headless version of a browser.

[00:01:49] A headless version is like a browser with no GUI, just invisible browser that ran in the background, maybe something about PhantomJS. They would start that browser up, and they would inject all your tests into that browser, and then it will talk to it via web sockets. So they would run the tests in the browser, then get the results via web sockets and print them out to you in the terminal.

[00:02:06] So that's how you would test browser. It was like four years ago. Today, you don't actually have to do that for a few reasons. One, most frameworks actually are virtualized now, so they can run on the server. Most framework support service side rendering, so they can render to each string on a server without a DOM, so you don't need to put it in a browsing where to actually test it.

[00:02:25] So View, Angular, Reacts, and pretty much any framework that comes out today can pretty much render on the server now as a string. So you can just test it on the servers. That's one reason. Two, if you didn't wanna do that, you could actually just mock the DOM out with something like JS DOM.

[00:02:41] That's a npm module, you could look it up. JS DOM literally mocks the entire DOM out, so you could use the DOM on the server. It mocks and probably fills the APIs out, care of to where you can test things. So that's another example of how technology's progressing, so there really is no way why you couldn't test everything in node.js on the server.

[00:02:59] In fact, I can't remember the last time I wrote a test in the browser. I remember, when I learned to code, we would use Mocha in the browser, and you would see all of the suites and the checkmark stuff in the browser. I haven't done that in forever.

[00:03:09] You just do everything in Node because it does everything. So you can pretty much test anything. Browser apps, APIs, CLIs, tools, scripts, whatever you want, you could test it, it doesn't matter. It's so flexible, you can mock anything out, you can virtualize anything. There's just no way that you couldn't test stuff.

[00:03:26] You could even test animations in Node. I wrote an animation library for Angular a long time ago, and all the tests were in Node. So I wouldn't test how the animation worked, I would test the map on the animation and see that these coordinates changed to these coordinates, and this happened when this happened.

[00:03:45] So it's more about the unit-level type of stuff and not the visual thing. So Node was able to help test that stuff. Yeah, so again, your test themselves will be executed in Node.js, so the actual text scripts that you write have the ability to do anything as well.

[00:04:04] So not only is the code that you are testing is node, but the code that is testing the code that your node is in Node. So you pretty much do anything in those tests as well, right? So one example is that, we use lambda functions to test our UI from a CLI sometimes.

[00:04:22] So we wanna be able to test UI things unto like, take visual snapshots and photos of our app. So we'll use lambda for that with a headless Chrome, that's invisible in the background. It will load it up in the lambda function with a headless Chrome and take a snapshot of it, and it will send it back to the CLI, so we can do all that in Node with no visual representation.

[00:04:44] No GUI, but it still taking screenshots, you never see them until they get saved back or upload it to S3. So it can get pretty complicated, but you can do whatever you want because it's all just, JavaScript. That's an advanced use case, but yeah, you can do whatever you want there.