1 00:00:00,200 --> 00:00:04,070 Different frameworks require us to set up our file structure differently. 2 00:00:04,070 --> 00:00:06,390 They want us to name our test files differently. 3 00:00:06,390 --> 00:00:08,180 Or put them in different places. 4 00:00:08,180 --> 00:00:11,920 The main issue is that we don't want to track down our test files manually 5 00:00:11,920 --> 00:00:13,290 every time we run them. 6 00:00:13,290 --> 00:00:16,430 The whole point of automated testing is that our tests are easier and 7 00:00:16,430 --> 00:00:19,130 better to use than our old manual testing style. 8 00:00:20,180 --> 00:00:23,720 If our tests are annoying to use then we might go through all the trouble of 9 00:00:23,720 --> 00:00:27,500 writing them only to never actually run them while we're developing. 10 00:00:27,500 --> 00:00:29,360 That would be a huge waste. 11 00:00:29,360 --> 00:00:31,320 So, we want our test to be easy to run and 12 00:00:31,320 --> 00:00:35,210 write that way we're more likely to actually test our code. 13 00:00:36,385 --> 00:00:38,660 >> Mocha has a really convenient feature. 14 00:00:38,660 --> 00:00:41,440 Since we've already installed Mocha as a dependency for 15 00:00:41,440 --> 00:00:46,820 our project, we can have NPM use it to run all of our tests automatically. 16 00:00:46,820 --> 00:00:51,880 So first, in the console, we'll run npm init. 17 00:00:51,880 --> 00:00:54,690 This sets up our project to be portable, and 18 00:00:54,690 --> 00:00:57,990 it takes advantage of some convenient npm features. 19 00:00:57,990 --> 00:01:03,340 Now, in the console, npm asks us a bunch of questions about our project setup. 20 00:01:03,340 --> 00:01:06,460 But for now, I'm just going to press Enter for all of these and 21 00:01:06,460 --> 00:01:10,320 accept the defaults, since I don't plan to publish this project anywhere. 22 00:01:12,070 --> 00:01:16,280 The result is that we have a package.json file for our project. 23 00:01:17,400 --> 00:01:19,470 Now it doesn't have much in it right now. 24 00:01:19,470 --> 00:01:24,060 But notice that the scripts field has a test about that already. 25 00:01:24,060 --> 00:01:29,186 By default, the test method will normally just give us a warning in our terminal or 26 00:01:29,186 --> 00:01:31,882 consul that says error no test specified. 27 00:01:31,882 --> 00:01:39,310 But in our package.json file npm has already set the test command to use Mocha. 28 00:01:39,310 --> 00:01:44,298 And although we've been explicitly telling Mocha which files to run, we can actually 29 00:01:44,298 --> 00:01:49,670 save even more time by keeping all of our test files in a directory called test. 30 00:01:49,670 --> 00:01:55,470 It's important that the directory is specifically named test, not tests. 31 00:01:55,470 --> 00:01:59,170 Or Test with a capital t, and it has to be located 32 00:01:59,170 --> 00:02:04,120 at the same level of our project structure as the package.JSON file. 33 00:02:04,120 --> 00:02:08,310 It makes sense to have all our tests in one directory at the top of our project. 34 00:02:08,310 --> 00:02:11,590 It makes it easy to pull in code from other files in our project 35 00:02:11,590 --> 00:02:14,880 because the follow path will always be from the same directory. 36 00:02:14,880 --> 00:02:17,560 That also makes it easy to organize our tests. 37 00:02:17,560 --> 00:02:20,540 And for ourselves to find the right test file after we see it 38 00:02:20,540 --> 00:02:22,410 output in our console. 39 00:02:22,410 --> 00:02:25,990 Now if we forget to install Mocha before running npm init, 40 00:02:25,990 --> 00:02:30,830 we can always just go change this part of our package.json file by hand. 41 00:02:30,830 --> 00:02:34,055 And you can check the teacher's notes to learn more about customizing your 42 00:02:34,055 --> 00:02:39,060 package.json file to run task for us, like starting our automated test group. 43 00:02:40,170 --> 00:02:45,652 So if we follow these guidelines then we just get to run npm test, and 44 00:02:45,652 --> 00:02:50,660 Mocha will automatically run every single test in the test directory for us. 45 00:02:52,010 --> 00:02:57,810 So let's create a new directory called test here at the root of our project. 46 00:02:59,440 --> 00:03:03,790 And I'll move the main test.js file inside the new test folder. 47 00:03:05,840 --> 00:03:11,150 So now when I run npm test in a console, I get the same output as running Mocha. 48 00:03:11,150 --> 00:03:11,650 Great. 49 00:03:12,680 --> 00:03:17,420 Earlier I mentioned that it's important that the directory is specifically named 50 00:03:17,420 --> 00:03:19,650 test in all lowercase letters. 51 00:03:19,650 --> 00:03:23,200 Naming the test directory badly is a common mistake developers make 52 00:03:23,200 --> 00:03:24,720 when setting up suites. 53 00:03:24,720 --> 00:03:29,400 So let's see what Mocha's output is when we use a directory name other than test. 54 00:03:30,520 --> 00:03:34,550 For instance, let's change the name to tests. 55 00:03:34,550 --> 00:03:37,413 Then run npm test in the console. 56 00:03:40,410 --> 00:03:43,734 So now Mocha can't find what it's looking for, so 57 00:03:43,734 --> 00:03:47,610 we get this test failed error in the console output. 58 00:03:47,610 --> 00:03:51,140 And the same would happen if the name is test with a capital T. 59 00:03:53,930 --> 00:03:57,378 So I'll change the name back to test. 60 00:03:57,378 --> 00:04:00,280 Then run npm test in the console, and 61 00:04:00,280 --> 00:04:04,740 now Mocha once again automatically runs every single test In the test directory. 62 00:04:06,110 --> 00:04:09,900 Okay, so in the next video we'll really start building our first test suite.