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 Next Steps Testing Asynchronous Code

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

Why is the following quiz question answer incorrect?

This is the question:

The following test is failing right now with an Assertion Error: Expected undefined to be an object:

it(should update the status after a successful database call, function (done) {
  var status;
  fetch(function () {
    status = success;
    expect(status).to.equal(success);
  });
});

Which of the following revisions would fix it?

Why is only the following answer correct?

 it(should update the status after a successful database call, function (done) {
  var status;
  fetch(function () {{
    status = success;
    done();
    expect(status).to.equal(success);
  });
});

Why is the following answer wrong?

it(should update the status after a successful database call, function (done) {
  var status;
  fetch(function () {
    status = success;
    expect(status).to.equal(success);
      done();
  });
}); 

This is how Guil did it in the previous video and it doesn't seem to make a difference if you call 'done()' before or after 'expect()'.

Regards, Florian

5 Answers

Steven Parker
Steven Parker
229,786 Points

I suspect the video is wrong, not the quiz. Calling expect before done still performs the test prematurely, but the call to done afterwards creates the the time delay that makes it seem like the test was performed later.

A better test would have caused the expected condition to be false until after the timer elapsed.

but the call to done afterwards creates the the time delay that makes it seem like the test was performed later.

If the expectation fails there is no way the test can register it. The later example code works not only fine but it also make way more sense.

Emeka Okoro
PLUS
Emeka Okoro
Courses Plus Student 11,724 Points

Yeah, I think he made a slight error in calling done after the expectation. That said, he did a great job running through the topic

This should give us a timeout error since done was passed to the unit test, mocha has to wait until done is called.

it(should update the status after a successful database call, function (done) {
  var status;
  fetch(function () {
    status = success;
    expect(status).to.equal(success);
  });
});

Calling done before one or more expectations, mocha cannot register the result of the exceptation. So I think that the quiz is wrong but the video is right.

it(should update the status after a successful database call, function (done) {
  var status;
  fetch(function () {
    status = success;
    done();  // everything after the call of done doesn't get registered
    expect(false).to.be.ok; // no exception
  });
});
Aaron Bell
Aaron Bell
6,113 Points

The name of the link made it make sense. You want the function to be 'done' before you pass your expectations onto it. If you pass your expectations onto it before it's done, you'll get the wrong results.

Aaron Bell I've forgotten the most important part of my answer. Here is the updated one.

William Blair
PLUS
William Blair
Courses Plus Student 3,542 Points

The answer is wrong because you aren't executing the stub before your expectation. You've essentially turned what was supposed to be an asynchronous test into a synchronous one.

I was about to ask the same question. still don't quite get what we have tested

  1. saveGame get passed a callback function. That function (the one we pass) gets called/executed from within the saveGame function.

  2. In the test, we call saveGame and pass our own function as a parameter to it. Are we saying we are defining saveGame function behaviour from within a test? What are we testing then? The fact that saveGame executed our code? If not, why would we ever need to pass saveGame a callback function?

  3. I also don't get why expectations are a part of the function we pass to saveGame.

Can anyone clearly explain this?