Skip to content

Use fakeTimers does not mock imports from node:timers or node:timers/promisesΒ #10871

Closed

Description

@Nokel81

Describe the bug

According to #7097 this should work not, but it does not.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-1gkfprhf?file=test%2Fbasic.test.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 22.22.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 4.1.10 
    vite: latest => 8.2.0 
    vitest: latest => 4.1.10

Used Package Manager

npm

Validations

Activity

  1. added theissue type on Aug 4, 2026
  2. added 2 commits that reference this issue on Aug 5, 2026
    132790e
    e2da3ca
  3. atharv-sys32 commented on Aug 5, 2026

    @atharv-sys32

    Hi @sheremet-va. Opened a fix for this: #10878.

    vi.useFakeTimers() did not mock setTimeout imported from node:timers or node:timers/promises. The fake timers patch read the timer modules at module load time, before vitest_required is populated, so the module methods were never patched, and Node snapshots ESM named import bindings at first load.

    The PR registers node:timers and node:timers/promises as live module stubs in the module runner and reads the timer modules lazily inside the fake timers install(). Regression tests added in test/unit/test/timers-node.test.ts (named, default, namespace, promises, and require imports), passing in the threads, forks, and vmThreads pools. It also fixes the Windows typechecker OOM test that was already failing on main.

    Would appreciate a look.

  4. added
    p3-minor-bug
    and removed on Aug 10, 2026
  5. hi-ogawa commented on Aug 10, 2026

    @hi-ogawa
    Collaborator

    Apparently, docs have never mentioned faketimers mocking node:timers and node:timers/promises, so technically it's not settled what's the expected behavior. Also enabling mocking for this may be considered breaking change.

  6. moved this to P2 - 4 in Team Boardon Aug 10, 2026
  7. moved this from P2 - 4 to P2 - 3 in Team Boardon Aug 10, 2026
  8. added
    p2-to-be-discussed
    on Aug 10, 2026
  9. Nokel81 commented on Aug 10, 2026

    @Nokel81
    Author

    Apparently, docs have never mentioned faketimers mocking node:timers and node:timers/promises, so technically it's not settled what's the expected behavior. Also enabling mocking for this may be considered breaking change.

    Fair, I was mostly going off of the changelogs.

  10. AriPerkkio commented on Aug 14, 2026

    @AriPerkkio
    Member

    I'd say adding support (or fixing it) for mocking node:timers built-in module is just a feature. But to mock those by default in useFakeTimers() is breaking change.

  11. Nokel81 commented on Aug 17, 2026

    @Nokel81
    Author

    Fair enough, I would be fine if I could say useFakeTimers({ toFake: ["node:timers", "node:timers/promises"] })

  12. moved this from P2 - 3 to Approved in Team Boardon Aug 21, 2026
  13. hi-ogawa commented on Aug 21, 2026

    @hi-ogawa
    Collaborator

    We discussed and are going to implement fakeNodeBuiltins flag like:

    useFakeTimers({
      fakeNodeBuiltins: boolean
    })

    We can introduce as default false to be non breaking change. Then in the future, can enable default true in Vitest 6.

  14. removed
    p2-to-be-discussed
    on Aug 21, 2026
  15. locked and limited conversation to collaborators on Nov 3, 2026
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

    p3-minor-bug

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions