You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm doing multiple stubs on an object, the stubs seems to work but when I check the call count it's zero.
In the example I provide i want to verify on the log function that it was called with the proper parameters.
After debugging it I verified that the parameters are correct, I also tried with td.matchers.anything().
Your issue is that your test executes synchronously and exits immediately, but your subject is asynchronous and doesn't exit until at least two additional ticks of the event loop.
Annotated source:
// You're good so farmath.sum(a,b).then((r)=>{// All of this will execute in the next tick (after the test has exited)math.log(r);// Therefore, at test-time, this call won't have happened yet:math.substract(a,b).then((rs)=>{// And this is now scheduled at least one tick latermath.log(rs);constbody={sum: r,substract: rs};// To test this, you need some kind of mechanism (a callback,// a returned promise, etc.) to continue execution of the testres.status(200).send(body);})});
Options:
Refactor the subject to not be fire-and-forget, which would be good form as that's as unobservable as a private anonymous function. For how to write async tests in Jest I'll have to refer you to their docs
Configure a fake promise implementation which will execute synchronously for you. I wouldn't recommend this as you might find other bugs sneak in due to synchronous execution of code that will always be async in prod. docs
Description
I'm doing multiple stubs on an object, the stubs seems to work but when I check the call count it's zero.
In the example I provide i want to verify on the
log
function that it was called with the proper parameters.After debugging it I verified that the parameters are correct, I also tried with
td.matchers.anything()
.Environment
-Node 8.11.1
-npm 6.4.1
-testdouble 3.8.2
Example Repo
https://github.com/gonzalon/node-ut
The text was updated successfully, but these errors were encountered: