Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
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.
- A good use of the
--reporterflag is when you only want to see test failures
- To show only the failing test errors, you can use
mocha --reporter min
mocha --reporter markdownwill print the same test report we’re used to, but using markdown formatting
- You can add a
--reporterflag to your
package.json-- file in your “test” command -- so that
npm testwill always use that reporter
We've covered a lot so far using BDD to lay the groundwork of a battleship game
using Mocha and Chai to write unit test for code that already exist.
Setting up test suites and test specs with Mocha's describe and it blocks,
and writing test expectations with Chai's Expect method.
But there's a lot more that Mocha can do,
and this stage will wrap up by looking at some of Mocha's expert tools.
Let's get started.
So far we've been looking at Mocha's default reports in our console.
there are other options, too.
you can change the reporter style using the reporter flag when running Mocha.
A good use of the reporter flag is when you only want to see test failures.
When you have lots of tests running all the time,
you might not care to see other specific passing testing in your console.
So to show only the failing test errors, you can use mocha
So in our console, now Mocha only prints 15 passing.
But if I change one of our expectations to fail a unit test, then Mocha will print
the same error report we're used to seeing, and hides the passing tests.
So for example, in main test dot JS.
I'll change expect)true)to.be.okay to expect(true)to.be.false, so that it fails.
So when I save the file and run the same test again, Mocha now prints
14 passing and 1 failing, followed by the usual error report.
So another cool reporter is the mark down reporter.
Mocha --reporter markdown will print the same long test
report we're used to, but using markdown format.
That means you can just copy and paste it into the read me file for
your GitHub repo, and use the passing tests as pre-written documentation.
This may help other developers understand how your code works
without them having to download and run the tests.
Go ahead and check the teachers notes to learn more about the mark down format
if you'd like to learn more.
You can browse all the possible reporting mechanisms in the Mocha documentation.
Now some of them are just for fun and others, like mark down and
min, are useful in different situations.
So try some out and share your favorites in the community.
I personally like the nyan reporter.
I think it's pretty awesome.
And if you find one you like better than the defaults, you can add a reporter
flag to your package dot JSON file and your test command, like this.
This way, npm test will always use that reporter.
You need to sign up for Treehouse in order to download course files.Sign up