Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video, you'll learn to write a test suite using the 'describe()' function, and a test spec using the 'it()' function.
Resources
Video review
- A test suite is a block of unit tests that are all closely related; they test the same function or similar parts of our code base
- We introduce a test suite in Mocha using
describe()
- Each individual unit test is sometimes called a “spec”
- Mocha makes it natural to write specs by containing them in a function called
it()
- To use Chai's
expect
method, import Chai at the top of your file:var expect = require('chai').expect
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
Let's open up our maintest.js file and
start really using Mocha.
0:00
The first thing to do
is set up a Test suite.
0:05
Remember, a Test suite is a block of
unit tests that are all closely related
0:09
because they test the same function or
they test similar parts of our code base.
0:13
We introduce a test suite
in Mocha using describe.
0:18
Describe is a function
that takes two arguments.
0:28
A string and another function.
0:31
We used a string to describe
what the suite will cover.
0:35
So, for example, let's write a test for
0:38
ourselves to prove that
Mocha is working properly.
0:40
This kind of trivial check
is called a sanity check
0:43
because it's easy to write and
0:46
it verifies that all our code structure
is in proper order before we get started.
0:48
Doing this now will help us confirm that
hings at least worked at some point,
0:52
even if we experience
bugs later down the road.
0:57
Since we're testing our mocha framework,
1:01
I'll just write mocha
as the first argument.
1:03
Mocha is the thing we're testing, so
that's a good title for this suite.
1:06
Now the second argument
is an anonymous function.
1:10
It doesn't require any arguments
it's just a wrapper for
1:13
all of the individual unit tests that
we're going to include within the suite.
1:16
So now with our suite in place we can
start adding some simple unit tests.
1:21
Each individual unit test
is sometimes called a spec.
1:27
And just like our suites,
1:35
Moca makes it natural to write our spec by
containing them in a function called it.
1:36
It is similar to describe.
1:44
The first argument is a string.
1:47
That describes the particular behavior
this unit test is responsible for.
1:49
And again, just as a sanity check for
1:55
myself I'll say should
run our tests using npm.
1:58
The second argument is a function.
2:02
This time the function should
contain all of our expectations for
2:05
this unit, remember an expectation
gives a specific state condition for
2:08
a test to count as passing.
2:13
Otherwise it throws an error.
2:15
So let's import chai at
the top of our file so
2:17
that we can use it as an expectation here.
2:20
So once we have the expect method.
2:30
We can write an expectation
inside of our lonely unit tests.
2:32
So I’ll start by writing
something I know will pass.
2:37
Just to prove that Mocha has
access to this file here.
2:41
Say expects,
2:44
true.to.be.ok.
2:47
So expect true to be okay.
2:52
Now, okay is an assertion method for chai.
2:55
It tests whether some value is truthy,
that means any value besides undefined,
2:58
not a number, false,
an empty string or zero.
3:04
So any value that would satisfy and
if condition.
3:08
Okay, so now if we run npm test and
our console.
3:13
We see some really nice output.
3:18
It says should run our tests using npm.
3:20
Well, that's easy to read and
3:24
informative so
it looks like everything is ready to go.
3:26
In the next video, we'll start writing
our bdd outline for battleship.
3:29
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