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
Mocha lets us specify what the test output looks like, and even what environment it will run in! In this video, we set up our test suite to display in our browser, and see some different reporting styles.
Resources
Video review
- A good use of the
--reporter
flag is when you only want to see test failures - To show only the failing test errors, you can use
mocha --reporter min
-
mocha --reporter markdown
will print the same test report we’re used to, but using markdown formatting - You can add a
--reporter
flag to yourpackage.json
-- file in your “test” command -- so thatnpm test
will always use that reporter
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
We've covered a lot so far using BDD to
lay the groundwork of a battleship game
0:04
using Mocha and Chai to write unit
test for code that already exist.
0:08
Setting up test suites and test specs
with Mocha's describe and it blocks,
0:13
and writing test expectations
with Chai's Expect method.
0:17
But there's a lot more that Mocha can do,
0:21
and this stage will wrap up by looking
at some of Mocha's expert tools.
0:23
Let's get started.
0:27
So far we've been looking at Mocha's
default reports in our console.
0:29
They're much easier to read than standard
JavaScript console.log statements, but
0:32
there are other options, too.
0:37
For instance,
0:38
you can change the reporter style using
the reporter flag when running Mocha.
0:39
A good use of the reporter flag is when
you only want to see test failures.
0:44
When you have lots of tests
running all the time,
0:48
you might not care to see other specific
passing testing in your console.
0:51
So to show only the failing test errors,
you can use mocha
0:54
--reporter min.
0:58
So in our console,
now Mocha only prints 15 passing.
1:04
But if I change one of our expectations to
fail a unit test, then Mocha will print
1:09
the same error report we're used to
seeing, and hides the passing tests.
1:13
So for example, in main test dot JS.
1:17
I'll change expect)true)to.be.okay to
expect(true)to.be.false, so that it fails.
1:20
So when I save the file and
run the same test again, Mocha now prints
1:27
14 passing and 1 failing,
followed by the usual error report.
1:32
So another cool reporter
is the mark down reporter.
1:44
Mocha --reporter markdown
will print the same long test
1:52
report we're used to, but
using markdown format.
1:57
That means you can just copy and
paste it into the read me file for
2:01
your GitHub repo, and use the passing
tests as pre-written documentation.
2:05
This may help other developers
understand how your code works
2:10
without them having to download and
run the tests.
2:13
Go ahead and check the teachers notes to
learn more about the mark down format
2:16
if you'd like to learn more.
2:19
You can browse all the possible reporting
mechanisms in the Mocha documentation.
2:21
Now some of them are just for
fun and others, like mark down and
2:26
min, are useful in different situations.
2:29
So try some out and
share your favorites in the community.
2:32
I personally like the nyan reporter.
2:36
I think it's pretty awesome.
2:37
And if you find one you like better than
the defaults, you can add a reporter
2:39
flag to your package dot JSON file and
your test command, like this.
2:44
This way,
npm test will always use that reporter.
2:57
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