Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Unit Testing Behavior Driven Development with Mocha & Chai Answer: Expanding Our Expectations

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Following Quiz: What is the main problem with writing unit tests with more than one expectation?

Why is the answer "Mocha only reports the first failure in a given test." wrong?

When I do something like this

expect(true).to.be.false;
expect(true).to.be.true;
expect(5).to.equal(4);

Mocha only gives me an AssertionError for "expect(true).to.be.false;" and doesn't continue to report that "expect(5).to.equal(4);" is not passing also. So it only reports the first failure to me.

Regards, Florian

2 Answers

What if the second expectation failed? Or the third? How would you know which one had failed?

Gabbie Metheny
Gabbie Metheny
33,778 Points

While it's true that Mocha will only report the first error it finds in the spec, like JavaScript does in general, the question asked what the main problem with multiple expectations is. It's a little subjective, but in this video, around the 3:20 mark, Guil says "you normally don't want your unit test to have more than one expectation, because it might be unclear to someone reading it what the test is supposed to prove." You could certainly argue that clarity is the more critical issue here. If more than one expectation breaks, Mocha will just give you the first one to start with, and you can work through them one at a time until the test passes.