Skip to content

Add support for advancing timers to the next animation frame #6346

@bnjm

Description

@bnjm

Clear and concise description of the problem

When testing code which relies on the browser's requestAnimationFrame method, it would be great to have the ability to advance fake timers by the amount needed to execute any scheduled animation frames. sinonjs/fake-timers supports this with the clock.runToFrame() method, which Jest exposes as jest.advanceTimersToNextFrame().

Suggested solution

vi.advanceTimersToNextFrame() could be introduced to mirror the Jest implementation. This would be straightforward to implement (I am happy to make a PR) and would increase compatibility between Jest and Vitest.

Example:

let frameRendered = false

vi.useFakeTimers({ toFake: 'requestAnimationFrame' })

requestAnimationFrame(() => {
  frameRendered = true
})

vi.advanceTimersToNextFrame()

expect(frameRendered).toBe(true)

Alternative

No response

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p2-nice-to-haveNot breaking anything but nice to have (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions