Skip to content

fake timers do not mock AbortSignal.timeout or timers/promisesΒ #3088

Closed

Description

@everett1992

Describe the bug

mock timers do not mock node's timers/promises or AbortSignal.timeout

There is an open issue in sinon fake-timers to implement these.
sinonjs/fake-timers#418

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-5rmugo?file=test/basic.test.ts

describe('with fake timers', () => {
  beforeAll(() => {
    vi.useFakeTimers();
  });

  test('signal should abort after timeout', () => {
    const signal = AbortSignal.timeout(100);
    expect(signal.aborted).toBe(false);
    
    vi.advanceTimersByTime(200);
    
    // This will fail, aborted is still false.
    expect(signal.aborted).toBe(true);
  });
});

System Info

stackblitz

Used Package Manager

npm

Validations

Activity

  1. zbyte64 commented on Aug 14, 2023

    @zbyte64
  2. LumaKernel commented on Dec 4, 2023

    @LumaKernel

    Could mocking module "timers/promises" and "node:timers/promises" builtin solve the problem? Or waiting sinonJS' decision?

  3. everett1992 commented on Dec 4, 2023

    @everett1992
    ContributorAuthor

    I think we're waiting on sinon. In the mean time I've been mocking timers/promises with an implementation that delegates to global setTimeout so it's correctly mocked by useFakeTimers. I'm not sure how accurate my mock is so I won't bother sharing it here.

  4. sheremet-va commented on Feb 16, 2024

    @sheremet-va
    Member

    I am closing the issue here since Vitest cannot do anything to resolve this unless @sinonjs/fake-timers implement the correct API.

  5. locked and limited conversation to collaborators on Mar 2, 2024
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