1 00:00:00,390 --> 00:00:05,500 Let's open up our maintest.js file and start really using Mocha. 2 00:00:05,500 --> 00:00:08,200 The first thing to do is set up a Test suite. 3 00:00:09,250 --> 00:00:13,890 Remember, a Test suite is a block of unit tests that are all closely related 4 00:00:13,890 --> 00:00:18,980 because they test the same function or they test similar parts of our code base. 5 00:00:18,980 --> 00:00:23,754 We introduce a test suite in Mocha using describe. 6 00:00:28,314 --> 00:00:31,980 Describe is a function that takes two arguments. 7 00:00:31,980 --> 00:00:33,760 A string and another function. 8 00:00:35,340 --> 00:00:38,600 We used a string to describe what the suite will cover. 9 00:00:38,600 --> 00:00:40,470 So, for example, let's write a test for 10 00:00:40,470 --> 00:00:43,830 ourselves to prove that Mocha is working properly. 11 00:00:43,830 --> 00:00:46,860 This kind of trivial check is called a sanity check 12 00:00:46,860 --> 00:00:48,170 because it's easy to write and 13 00:00:48,170 --> 00:00:52,710 it verifies that all our code structure is in proper order before we get started. 14 00:00:52,710 --> 00:00:57,650 Doing this now will help us confirm that hings at least worked at some point, 15 00:00:57,650 --> 00:01:00,040 even if we experience bugs later down the road. 16 00:01:01,100 --> 00:01:03,400 Since we're testing our mocha framework, 17 00:01:03,400 --> 00:01:06,300 I'll just write mocha as the first argument. 18 00:01:06,300 --> 00:01:09,550 Mocha is the thing we're testing, so that's a good title for this suite. 19 00:01:10,600 --> 00:01:13,670 Now the second argument is an anonymous function. 20 00:01:13,670 --> 00:01:16,810 It doesn't require any arguments it's just a wrapper for 21 00:01:16,810 --> 00:01:21,910 all of the individual unit tests that we're going to include within the suite. 22 00:01:21,910 --> 00:01:27,010 So now with our suite in place we can start adding some simple unit tests. 23 00:01:27,010 --> 00:01:30,890 Each individual unit test is sometimes called a spec. 24 00:01:35,420 --> 00:01:36,850 And just like our suites, 25 00:01:36,850 --> 00:01:42,540 Moca makes it natural to write our spec by containing them in a function called it. 26 00:01:44,800 --> 00:01:47,600 It is similar to describe. 27 00:01:47,600 --> 00:01:49,830 The first argument is a string. 28 00:01:49,830 --> 00:01:55,491 That describes the particular behavior this unit test is responsible for. 29 00:01:55,491 --> 00:01:58,327 And again, just as a sanity check for 30 00:01:58,327 --> 00:02:02,090 myself I'll say should run our tests using npm. 31 00:02:02,090 --> 00:02:04,330 The second argument is a function. 32 00:02:05,380 --> 00:02:08,950 This time the function should contain all of our expectations for 33 00:02:08,950 --> 00:02:13,360 this unit, remember an expectation gives a specific state condition for 34 00:02:13,360 --> 00:02:15,580 a test to count as passing. 35 00:02:15,580 --> 00:02:17,150 Otherwise it throws an error. 36 00:02:17,150 --> 00:02:20,036 So let's import chai at the top of our file so 37 00:02:20,036 --> 00:02:22,775 that we can use it as an expectation here. 38 00:02:30,896 --> 00:02:32,850 So once we have the expect method. 39 00:02:32,850 --> 00:02:37,250 We can write an expectation inside of our lonely unit tests. 40 00:02:37,250 --> 00:02:41,120 So I’ll start by writing something I know will pass. 41 00:02:41,120 --> 00:02:44,000 Just to prove that Mocha has access to this file here. 42 00:02:44,000 --> 00:02:47,147 Say expects, 43 00:02:47,147 --> 00:02:52,740 true.to.be.ok. 44 00:02:52,740 --> 00:02:55,310 So expect true to be okay. 45 00:02:55,310 --> 00:02:58,880 Now, okay is an assertion method for chai. 46 00:02:58,880 --> 00:03:04,740 It tests whether some value is truthy, that means any value besides undefined, 47 00:03:04,740 --> 00:03:08,340 not a number, false, an empty string or zero. 48 00:03:08,340 --> 00:03:11,230 So any value that would satisfy and if condition. 49 00:03:13,650 --> 00:03:18,550 Okay, so now if we run npm test and our console. 50 00:03:18,550 --> 00:03:20,640 We see some really nice output. 51 00:03:20,640 --> 00:03:24,470 It says should run our tests using npm. 52 00:03:24,470 --> 00:03:26,450 Well, that's easy to read and 53 00:03:26,450 --> 00:03:29,360 informative so it looks like everything is ready to go. 54 00:03:29,360 --> 00:03:33,710 In the next video, we'll start writing our bdd outline for battleship.