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, we’ll meet a powerful testing framework called Mocha.js. We can use an assertion library called Chai that makes our tests nice to read and easy to understand.
Video review
- To have Mocha and Chai in your project, type
npm install --save-dev mocha chai
- The
--save-dev
flag means that these tools are just for development
Resources
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
[MUSIC]
0:00
In the last stage we talked about
a style of development called
0:04
Behavior Driven Development or BDD.
0:08
It means, we write unit test as a guide or
0:11
outline before we start
writing our application code.
0:13
We practice the strategy using
an expectation library called chai.
0:16
In the example code we wrote,
0:21
our expectation functions were
included directly in our running code.
0:23
And they didn't have significantly better
output than console dot log statements.
0:27
As you'll learn in this
section of the course,
0:32
the professional approach to writing
unit tests is to put your test code in
0:34
a file that's separate from
your applications code.
0:38
I'll show you how to do that.
0:40
The important thing to keep in mind and
what separates unit testing from
0:43
logging to the consul,
is that tests describe expected behaviors.
0:46
Unit tests focus on the concrete
output of functions
0:52
without worrying about
how the function does it.
0:55
Now, we could have written our title case
function in anyway that made sense to us
0:58
as long as it ultimately produced
the end result or behavior we expected.
1:03
In this stage will use BDD
to build a game engine for
1:08
a digital version of battleship.
1:11
We'll use a testing framework called Mocha
to make our BDD approach even better.
1:13
We can keep all of our test
functions in a separate file and
1:18
even set them up to run automatically
anytime we make a change, so
1:21
that we'll know right away
whether we're improving.
1:25
To get started, we need to install Mocha.
1:29
Go ahead and follow along in work
spaces or in your own terminal.
1:32
Since we're starting
a completely new project,
1:35
I'm going to start in a new workspace.
1:38
Now, I named this workspace
JavaScript Unit Testing Basics.
1:40
But, if you're following along outside
of workspaces, you can name the project
1:45
directory on your computer Battleship
Engine or something more meaningful.
1:49
So, in order to have mocha and
chai in my project, I can type npm
1:54
install followed by the save-dev flag,
then, mocha chai.
2:00
I used the save-dev flag here because
these tools are just for development.
2:07
And once they're installed,
you can see the new node modules directory
2:13
that includes the new libraries by
simply refreshing the workspace sidebar.
2:17
What mocha now installed,
we can call mocha,
2:22
followed by the name of a JavaScript file
containing our tests from the console and
2:26
will see the test results output there.
2:30
So for example, if I create
a new file called main_test.js.
2:33
Then in the console,
2:41
run mocha main_test.js.
2:44
Mocha prints zero passing
in green to my console.
2:49
Because there are no test in
main test.js to pass or fail.
2:53
But, we don't always want to
run mocha specifically for
3:00
each test file we might have.
3:03
A large project might have
many test files all around and
3:05
it would be annoying to
write each one individually.
3:08
So, in the next video,
3:11
I'll show you how to set up our test
files to make this really easy.
3:12
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