New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: await jasmine.clock().asyncTick() #1725
Comments
This sounds reasonable and I'd be happy to review a Pull Request to add an awaitable Sorry for the delay. Thanks for using Jasmine! |
I actually have some time now, so I'm taking a look. Seems like I should inject DelayedFunctionScheduler with (customPromise || global.Promise) ? |
Several previous issues have discussed the way mock clock doesn't work very well with Promises #1659 #1282 #710 . Here I have a proposal for a new method on the mock clock that will inter-operate with Promises nicely.
It's quite common to write code like this:
You might expect
jasmine.clock().tick(2000)
to log both A and B, but it will log neither A nor B! The issue is that.tick()
runs any scheduled timeouts one after the other, synchronously, and doesn't allow microtasks to run.Instead, we need to run a timeout, wait for microtasks to run, and then run the remaining timeouts, repeat. This is pretty close to the way the JS engine actually works.
I propose adding an
.asyncTick()
method that does what I've just described. The test would call it like this:One difficulty is knowing how to wait for microtasks to run. In previous issues, people pointed out that various Promise implementations work in different ways. My solution to this (in other settings) has been to schedule a (real)
setTimeout
. This does slow the test down, but it's a lot better than using a real clock.Another alternative could be to call jasmine's
clearStack()
. If a user has a Promise implementation that is slower than whateverclearStack()
does, we could just not care. (They can always get a better Promise implementation!) This would be my preferred approach.If this seems like a reasonable proposal, I will try to code it up and send a PR. Seems like I need to add a
runScheduledFunctionsAsync
method toDelayedFunctionScheduler
.The text was updated successfully, but these errors were encountered: