8.1 Testing Setup - Video Tutorials & Practice Problems
Video duration:
6m
Play a video:
Video transcript
<v Instructor>In this section,</v> we're going to set up our system for testing. The tool we're going to use is called Mocha, which is a powerful testing framework for Node.js. The way to install it is with a command called npm, which as mentioned in the introduction stands for Node Package Manager. You can see it's already installed, and indeed, it should be installed automatically as part of Node. So if you have Node.js on your system, you should have npm. Now, to install Mocha, what I'm going to do is use npm install and then in this case, we just want the Mocha executable. And in that case, when it doesn't have to be part of a particular project, the way to install the package is to use the global flag so that it's available wherever you are. There we go, we can do which mocha. And there it is. In order to use Mocha, we need to make a new Node package that incorporates our palindrome code. So right now, I'm in my main js_tutorial directory. Like that. But because this is going to be a self-contained package, we actually want a new directory for it. So I'm gonna make one called palindrome. And then I'm gonna copy the palindrome code from the js_tutorial directory while renaming it index.js, which is the standard name for the code in a Node package. As usual, it's a good idea to initialize the directory as a Git repository. And to this point, I recommend creating a new remote repository at your repository host of choice. GitHub, Bitbucket and GitLab are all good choices. And then push your repository up. As you might guess, I already have such a repository because I've been through this tutorial once before at a URL that I'll include in the next step when I initialize the npm package. Let's see how to do that. The command is npm init. Kind of like git init. But you can see here there's an interactive dialog that walks us through the creation of our Node package. Now, the name, by default, is just the name of the directory. Here palindrome. But I suggest using a name that is unique to you. This is important because we're actually gonna be publishing this module and modules have to have unique names. So in my case, I used this. Just mhartl-, like that. There's still always a chance of collision but chances are that your standard username -palindrome will be available. The next choice we have to make is to define a version. Now, when deciding on a version number, we're going to use a convention called semantic versioning, also called SemVer. In this system we're versioning, 1.0.0 indicates that a package is fairly mature and is ready for outside use. But we're just getting started here, so we're going to use a version 0.1.0. By using a leading zero here, we're signaling to people that this software is new, and could change unpredictably. The text goes into some more detail and has some links to even more detail about semantic versioning but this is good enough to get us started. So a description, whatever you want. I'm gonna put something like Palindrome detector. Entry point, remember I said that index.js was the default, so we've already done that. The test command, what are we gonna use for our test command? Well, we're gonna use mocha. Like that. Git repository. This is the kind of thing you can just paste it in. Mine is here. Like that. Keywords. Do palindrome. Like that. Author is me. You should put in your name. License. This is the ISC license, which is a permissive free software license. ISC stands for Internet Systems Consortium. In these certifications, the default is almost always fine. So we're gonna go with this for now. We can always change it later. But I've noticed by the way that any time there's something in parentheses, that's the default. If you just hit Return, it selects that automatically. Is this ok? Yes. There's one final step. Let's take a look at our code. Remember, index.js is where our code is now but this is just our palindrome code from before. So the final step is to export the object that we defined. In this case, the Phrase object. The way to do that is to use the special Node object module. Module.exports equals and then the name of the object. So all this stuff is not necessarily obvious. So I pulled most of it from the npm documentation. This is just the kind of thing that might seem mysterious but a little technical sophistication, googling something like how to set up an npm module is usually sufficient to get you on the right track. All right, and that's it. This is just an example of the kind of commit you might wanna make. I'm not gonna bother showing the exact commits and pushes that I make. But I do recommend making regular commits and pushes as you proceed throughout the rest of the chapter. All right, now that we're all set up, we're ready to get testing.