Check out a free preview of the full Introduction to JavaScript course

The "Testing Exercise" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to make a test pass, and students are instructed to do the same with the remaining tests.

Preview
Close

Transcript from the "Testing Exercise" Lesson

[00:00:00]
>> Brian: So the first thing you're gonna see here,
>> Brian: Just in your preview window. This is like an interface for the testing suite that I'm using, so all the green circles you see here, those are tests that are currently passing. All of the red Xs here are tests that are currently failing.

[00:00:21]
So you have 19 tests and 16 of them are failing. I wrote three of them so you can kind of get the gist of how this is supposed to work, but as you pass each one, these will start turning green until all of them will be green, okay?

[00:00:36]
Then here it's gonna tell you this is the name of the test case, find largest number of test case 1, and it says expected 0 to be 30, right? And those are obviously not equal to each other, so you need to go make these tests so that this ends up being 30.

[00:00:54]
>> Brian: Okay, so here's the first one that that's passing right now, addTwoToEveryNumberInTheList. It takes in the list. Again, when I say list, I mean array, same thing.
>> Brian: So I have a new list here, and what it does is it goes through, takes a new list, and it pushes into the list.

[00:01:17]
Right, remember, push adds something to the end of the array. List, list[i] +2, right? So if this was something like 1, 2, 3, 4, then it's going to first add 2 to 1, and then it's gonna push that into newList. Then it's gonna add it to 2, then it's gonna add to 3, then it's gonna add to 4, and so on and so forth until I end up with something that looks like 3456.

[00:01:49]
Makes sense? So that one's already written for you. Looks good, findLargestNumber. So this one had three different tests case on it. So once I made a pass then those made it work. Now what happens if I put three here?.
>> Brian: Notice now that one of the tests pass, but two of the other ones fail, right?

[00:02:15]
And I think the one that it passes on is what happens if I pass in an empty list. Well, it doesn't matter what you're adding to it, so that one still passed, cuz if you pass in an empty list, then it should get an empty list back.
>> Brian: Makes sense?

[00:02:33]
>> Brian: So let's look at some of the ones you have to do here. You have to do findLargestNumber, right? So you're gonna have an entire list of numbers, and you're gonna have to return the largest number in that list. This one, you're gonna have to mad a DogObject that has a speak method that returns whoof, as in name of Fido has a color property of white and has an age property of 6.

[00:02:57]
>> Brian: Okay?
>> Brian: Get a list of names.
>> Brian: [LAUGH] I was definitely watching Game of Thrones when I wrote this one [LAUGH]
>> Speaker 2: [LAUGH]
>> Brian: So this is going to be an array of objects that looks something like this, has a name, a job title, and an age, and what I want you to do is I want you to pull out all of the names and return just a list of names.

[00:03:25]
You can do that with any one of those. You could do it with a forloop. You could do it with a map. You could do it with four each. Any one of those would work. They're getting progressively more difficult. Here I want you to take a list of ages and I want you to get the average age based on those people's ages.

[00:03:46]
The objects are gonna look the same as this.
>> Brian: Okay.
>> Brian: Here, [LAUGH] this is Assassin's Creed [LAUGH]. Same thing here, you're gonna have a list of coworker, and here the job title is boss person, and I want you to return only the people, so it should be the same objects, the same people that have engineer in their title.

[00:04:31]
>> Brian: Here it says don't use sort. I revoked that, so you can use sort if you want to, so let's save that.
>> Brian: If you want to use sort, go ahead. If you want to write something like a bubble sort by hand, then be my guest just so you know that's gonna be a little bit more difficult, but you do have all the tools right now.

[00:04:50]
You know the tools to be able to write bubble sort.
>> Brian: Let's put, if you can use sort, if you want to write it by hand, use bubble sort or you could use insertion sort as well. I'm fine with either one of those. Bubble sort's gonna tend to be a little bit easier if you wanna do it by hand.

[00:05:21]
>> Brian: We'll click Save up there.
>> Brian: Any questions about any of these tasks? What do these people objects look like?
>> Brian: Okay, so the one thing here I wanted to show you, which I don't think I've shown you before, it's the one that like, find all the people that have engineer in their title, right?

[00:05:46]
So notice that it's just part of the word. It's not necessarily that job title, triple equals engineer. It's just part of that title, so I wanna show you really quick how you might do that. There's a function on strings, so if I say const x = "My name is Brian, and I think this is long enough now", okay.

[00:06:11]
So x represents a string right now, right? Strings have something called includes, so if I put includes Brian like that, it returns true because Brian is literally contained in there, right? And if I put something,
>> Brian: Someting else in there it's gonna be false because include it's not included somewhere in there, so that's a good one for you to know and use there.

[00:06:41]
Okay, last thing here I wanna show you a little bit about how to interact with the unit tests here. So these are called unit tests. You're testing little units of your code.
>> Brian: Don't modify the code below or else you're not really gonna be tested, but there is something that I can show you that helps us make this little bit easier.

[00:07:06]
If I put x describe that here, that means if you look here, and notice these are grayed out now. It means that it is no longer running these tests, so if you put x in front of anything, x in front of describe or x in front of it.

[00:07:18]
Notice here, now it's not running the first test, right. Subtle habits skip tests. This can be useful because, if you're running a bunch of tests or you're writing loops or something like that, you don't want it to run the other tests. You wanna just focus on one test or skip other tests.

[00:07:34]
That's how you do it. By the same token you can put f in front of these it will only run that test. It will focus on that test right? So now it is only running this one test right now. Same thing in front of describe, you can put f in front of describe.

[00:07:49]
It'll only run that suite of tests
>> Speaker 3: iI that because you are using Jasmine that it does that?
>> Brian: It is something specific to Jasmine.
>> Brian: Yeah, it's just useful for when you have to write tests.
>> Speaker 4: Is that just strictly for JavaScript, or is it for multiple languages, or IDs or anything?

[00:08:11]
>> Brian: It's a JavaScript testing framework, yeah. You can invoke other languages for you, but in general, it's pretty much just meant for JavaScript.
>> Brian: And here you don't have to worry about anything other than what I just showed you. You shouldn't have to look at the docs or anything like that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now