Delaying stub answer in Javascript

88 Views Asked by At

In my production code I have a function which is blocking program for 10 ms (because of use execSync). In my test I used testdouble.js library to stub it:

td.when(getSignalStrength()).thenReturn.apply(null, array);

However stub is responding immediately and in reality function getCurrentSignalStrength is responding after 10 ms. I would like to delay the answer of stub so test would be more similar to reality. I read documentation of testdouble.js: https://github.com/testdouble/testdouble.js/blob/master/docs/5-stubbing-results.md#delay but unfortunately it seems that delay option is not available for thenReturn method. So how to delay stub answer?

1

There are 1 best solutions below

0
On

There is no way to cause testdouble.js to block the thread of execution. Like the documentation says, even defer and delay are frowned upon.

This sounds like an example of an overly defensive specification; the "answer" in this case is to consider whether you really need to block and delay for 10ms. What does this indicate in your design?