Skip to content

waitFor not working when vi.useFakeTimers is used #565

Closed

Description

@jonaskuske

When vi.useFakeTimers is used in a test, waitFor does not longer run change detection.

it('works', async () => {
  vi.useFakeTimers()
  const view = await render(MyComponent)
  view.detectChanges()
  expect(screen.getByRole('input').toHaveAccessibleDescription(/something/))
})

it('does not work', async () => {
  vi.useFakeTimers()
  await render(MyComponent)
  await waitFor(() => expect(screen.getByRole('input').toHaveAccessibleDescription(/something/)))
})

It only happens when using Vitest, not when using Jest. (in fact, globalThis.jest = vi in the setup script suffices as workaround for some reason)

The cause appears to be the use of setTimeout in the waitForWrapper: https://github.com/testing-library/angular-testing-library/blob/main/projects/testing-library/src/lib/testing-library.ts#L592

The problem disappears when I replace

    setTimeout(() => detectChanges(), 0);

with

    Promise.resolve().then(() => detectChanges());

I assume that waitFor itself is using the fake setTimeout and thus never runs, waiting for the time to advance.

Activity

  1. ThiloAschebrock commented on Mar 4, 2026

    @ThiloAschebrock

    A workaround is to configure the fake timers to advance time via vi.userFakeTimers({ shouldAdvanceTime: true }).

  2. timdeschryver commented on Mar 8, 2026

    @timdeschryver
    Member

    @jonaskuske could you see any problems if we change the timeout to your suggested implementation? If not, I think it's a good suggestion, and you can create a PR with this change.

    This should also be resolved in #570.

  3. added a commit that references this issue on Mar 11, 2026
    0faab0a
  4. jonaskuske commented on Mar 11, 2026

    @jonaskuske
    ContributorAuthor

    I don't think so, PR is out :)

    (no guarantee though, Promise.resolve()/queueMicrotask() will make this a microtask rather than a task, so the timing can be a bit different)

  5. added a commit that references this issue on Mar 11, 2026
    906e8dd
  6. github-actions commented on Mar 11, 2026

    @github-actions

    🎉 This issue has been resolved in version 19.1.1 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions