A typical test in my node.js mocha suite looks like the following:
it("; client should do something", function(done) {
var doneFn = function(args) {
// run a bunch of asserts on args
client.events.removeListener(client.events.someEvent, userMuteFn);
done();
}
client.events.on(someEvent, doneFn);
client.triggerEvent();
});
The problem here is that if client.triggerEvent()
doesn't do something right, or if the server breaks and never invokes someEvent
, then done()
will never get invoked. This leaves an ambiguous error for someone who hasn't worked with the testsuite before like:
Error: timeout of 10500ms exceeded. Ensure the done() callback is being called in this test.
My question is, is there a way to rewrite these tests, whether it's with mocha or in addition to another lib, that makes async work like this easier to follow. I'd love to be able to output something such as:
the callback doneFn() was never invoked after clients.event.on(...) was invoked
or perhaps something similar.
I'm not sure if using something such as promises would help this. More meaningful error messages for async/callback type code would be a great thing. If it means moving from callback/async to another workflow, I'm OK with that as well.
What are some solutions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…