Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha: async vs sync

Tags:

mocha.js

According to Mocha documentation, "Mocha tests run serially" which means in the order they are defined.

My question is: what makes async (with done callback) tests different than sync?

like image 541
user2867106 Avatar asked May 05 '14 20:05

user2867106


1 Answers

You tell Mocha that a test is asynchronous by passing to the it call a function that takes an argument (traditionally named done). Mocha will then call this function with a first argument which is a callback that you must call to tell Mocha the test is over.

The only difference between an asynchronous test and a synchronous one is that for an asynchronous test Mocha will wait for the done callback to be called before moving on to the next test. If the test is deemed to be synchronous, then Mocha will move on to the next test as soon as the function you passed to it returns. If Mocha were to do this with asynchronous tests too then it would not be able to associate unhandled exceptions with the appropriate test.

like image 108
Louis Avatar answered Jan 01 '23 15:01

Louis