Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
In this video, we’ll go over the specific way Mocha expects us to organize our files in order to run properly.
- Using npm as a Task Runner – Learn how to customize your package.json file
- It’s important that the test directory is specifically named "test" (not "tests" or "Test"), and it has to be located at the same level of your project as the package.json file
- With your test directory in place, you simply run
npm test, and Mocha will automatically run every test in the test directory
Different frameworks require us to set up our file structure differently. 0:00 They want us to name our test files differently. 0:04 Or put them in different places. 0:06 The main issue is that we don't want to track down our test files manually 0:08 every time we run them. 0:11 The whole point of automated testing is that our tests are easier and 0:13 better to use than our old manual testing style. 0:16 If our tests are annoying to use then we might go through all the trouble of 0:20 writing them only to never actually run them while we're developing. 0:23 That would be a huge waste. 0:27 So, we want our test to be easy to run and 0:29 write that way we're more likely to actually test our code. 0:31 >> Mocha has a really convenient feature. 0:36 Since we've already installed Mocha as a dependency for 0:38 our project, we can have NPM use it to run all of our tests automatically. 0:41 So first, in the console, we'll run npm init. 0:46 This sets up our project to be portable, and 0:51 it takes advantage of some convenient npm features. 0:54 Now, in the console, npm asks us a bunch of questions about our project setup. 0:57 But for now, I'm just going to press Enter for all of these and 1:03 accept the defaults, since I don't plan to publish this project anywhere. 1:06 The result is that we have a package.json file for our project. 1:12 Now it doesn't have much in it right now. 1:17 But notice that the scripts field has a test about that already. 1:19 By default, the test method will normally just give us a warning in our terminal or 1:24 consul that says error no test specified. 1:29 But in our package.json file npm has already set the test command to use Mocha. 1:31 And although we've been explicitly telling Mocha which files to run, we can actually 1:39 save even more time by keeping all of our test files in a directory called test. 1:44 It's important that the directory is specifically named test, not tests. 1:49 Or Test with a capital t, and it has to be located 1:55 at the same level of our project structure as the package.JSON file. 1:59 It makes sense to have all our tests in one directory at the top of our project. 2:04 It makes it easy to pull in code from other files in our project 2:08 because the follow path will always be from the same directory. 2:11 That also makes it easy to organize our tests. 2:14 And for ourselves to find the right test file after we see it 2:17 output in our console. 2:20 Now if we forget to install Mocha before running npm init, 2:22 we can always just go change this part of our package.json file by hand. 2:25 And you can check the teacher's notes to learn more about customizing your 2:30 package.json file to run task for us, like starting our automated test group. 2:34 So if we follow these guidelines then we just get to run npm test, and 2:40 Mocha will automatically run every single test in the test directory for us. 2:45 So let's create a new directory called test here at the root of our project. 2:52 And I'll move the main test.js file inside the new test folder. 2:59 So now when I run npm test in a console, I get the same output as running Mocha. 3:05 Great. 3:11 Earlier I mentioned that it's important that the directory is specifically named 3:12 test in all lowercase letters. 3:17 Naming the test directory badly is a common mistake developers make 3:19 when setting up suites. 3:23 So let's see what Mocha's output is when we use a directory name other than test. 3:24 For instance, let's change the name to tests. 3:30 Then run npm test in the console. 3:34 So now Mocha can't find what it's looking for, so 3:40 we get this test failed error in the console output. 3:43 And the same would happen if the name is test with a capital T. 3:47 So I'll change the name back to test. 3:53 Then run npm test in the console, and 3:57 now Mocha once again automatically runs every single test In the test directory. 4:00 Okay, so in the next video we'll really start building our first test suite. 4:06
You need to sign up for Treehouse in order to download course files.Sign up