Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed JavaScript Unit Testing!
You have completed JavaScript Unit Testing!
Preview
Letβs compare the output of an automated test with our old manual debugging methods.
Resources
Benefits of automated testing
- Results displayed in an informative way
- Test files are separate from real code
- Output is easy to read and understand
- Guarantees code works as expected
- Instantly see if anything has broken when making changes
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In this course we'll be using a tool
called Mocha to write automated tests for
0:00
individual units of code
in our application.
0:04
We'll build a collection of
test functions called a suite.
0:07
Mocha, like all testing frameworks,
0:11
lets us run all of our
tests whenever we want to.
0:13
So we can make sure our functions
run as we expect them to.
0:16
This will increase our confidence
that our code is working, and
0:19
it will make understanding
problems much easier.
0:23
So let me show you how easy it can be.
0:26
Developers use the term test suite
in different ways sometimes.
0:29
Some use it to refer to the whole
collection of all our test.
0:33
Sometimes it means a particular
selection of tests that are related.
0:37
For example of the test for logging into
our website might compose a test suite.
0:40
Now the team that
designed Mocha uses suite
0:45
to describe a specific group of related
tests so that's how all use it too.
0:48
Just be aware that some people
use it in different ways and
0:53
you might have to ask someone what exactly
they mean depending on the context.
0:56
So I've already written a test suite for
1:00
our tiny gatherNamesOf function
we used in the previous video.
1:02
And don't worry about what
any of this means yet.
1:06
I'll talk a lot more about how to write
a test suite and what it looks like soon.
1:08
For now, I just want to show
you how Mocha runs tests.
1:12
With mocha already installed and my test
set up, I can go over to the console and
1:16
simply type mocha followed by test.
1:22
And mocha automatically runs the test
1:27
to check that the function
behaves the way we expect.
1:30
So we can see here an output.
1:32
That gatherNamesOf looks
good in our first tests.
1:35
It returns an array, and the array is
just as long as the array we passed in.
1:38
But then it fails when we expect it to
give us the name back from the object.
1:43
We can see that gatherNamesOf
1:47
returns undefined instead of
the person's actual name.
1:50
And when I'm ready to go fix this function
Mocha tells me that I can check line 23
1:55
column 25 of my utility spec.js
file to start debugging.
1:59
So the error mocha is reporting is
just helpful enough in this case.
2:06
So it says expected,
undefined to equal Guncer.
2:10
So that means our function is
returning undefined at some point.
2:14
Instead of Gunther.
2:18
So if I go look at the gather names of
a function in my utilities.js file.
2:20
I see one return statement from irate.
2:25
But the function being used by
map doesn't return anything.
2:29
I'm getting undefined in my new array.
2:33
Because gather names of checks for
a name, but never returns it, so
2:35
I'll fix this function by adding
return before person.name.
2:41
I'll save this file and
2:46
when I run the test again by typing Mocha
test, we can see that they all passed.
2:49
So great,
now I know that the function is working.
2:55
Don't worry if this doesn't make
a whole lot of sense just yet.
2:58
It will all make more sense later.
3:01
The main takeaway here is
understanding how automated testing
3:03
solves a lot of the problems with manual
testing We noted in the last video.
3:07
First our test suite could cover
every important function in our code.
3:12
And when we run the tests we can see
all of the results nicely displayed
3:16
in an informative way.
3:20
So we don't have the problem of
our results being too narrow and
3:21
second the test files
are completely separate.
3:25
From our real code.
3:28
So we don't have to make any changes
there or clean up our live code later.
3:29
Third, the output is easy to read and
understand.
3:34
So it's much easier to interpret
what we're seeing here
3:38
than than in our consul
dot log statements.
3:41
And finally, good tests guarantee that
our code works the way we expect.
3:44
We can run the tests over and over any
time we make a change anywhere and
3:48
instantly see whether
anything new has broken.
3:52
We'll even see what the specific
nature of the problem is.
3:55
As you learned,
automated tests handle a ton of the hard
3:59
tedious work we were
previously doing by hand.
4:02
It makes our code more reliable.
4:05
It makes programming less frustrating and
the benefits scale more and
4:07
more as our applications grow.
4:10
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up