Skip to content
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

Add async method variants from sinonjs/fake-timers to vitest #1804

Closed
4 tasks done
dkulchenko opened this issue Aug 5, 2022 · 0 comments · Fixed by #2209
Closed
4 tasks done

Add async method variants from sinonjs/fake-timers to vitest #1804

dkulchenko opened this issue Aug 5, 2022 · 0 comments · Fixed by #2209
Labels
enhancement New feature or request pr welcome

Comments

@dkulchenko
Copy link

dkulchenko commented Aug 5, 2022

Clear and concise description of the problem

fake-timers provides an await clock.runAllAsync() call as an alternative to clock.runAll() (https://github.com/sinonjs/fake-timers#clockrunall--await-clockrunallasync) which makes it significantly easier to run all timers in a more complex asynchronous context (e.g. timers called by promises that are called by timers). Without it, it can be really difficult to actually have all timers run when those timers are initiated by other promises.

See Stack Overflow discussions such as https://stackoverflow.com/questions/51126786/jest-fake-timers-with-promises and https://stackoverflow.com/questions/52177631/jest-timer-and-promise-dont-work-well-settimeout-and-async-function to see scenarios where this is an issue.

For a similar reason, await clock.tickAsync, await clock.nextAsync, await clock.runToLastAsync would ideally also be exposed in some way.

I'm currently having to import sinon-js/fake-timers manually to provide this functionality, and I'd prefer to stay within vitest, especially since it's using fake-timers under the hood.

Suggested solution

Adding something like:

runAllTimersAsync(): Promise<void> {
    if (this._checkFakeTimers())
      this._clock.runAllAsync()
}

to packages/vitest/src/integrations/mock/timers.ts for each associated method (and tests as needed).

Alternative

No response

Additional context

No response

Validations

@dkulchenko dkulchenko changed the title Add runAllAsync from sinonjs/fake-timers to vitest Add async method variants from sinonjs/fake-timers to vitest Aug 5, 2022
@sheremet-va sheremet-va added pr welcome enhancement New feature or request labels Aug 6, 2022
docschina-bot pushed a commit to vitest-dev/docs-cn that referenced this issue Jan 16, 2023
sheremet-va pushed a commit to sheremet-va/vitest that referenced this issue Jan 18, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request pr welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants