Skip to content

FakeTime does not mock node:timers methods #7177

@regseb

Description

@regseb

Describe the bug

FakeTime does not mock node:timers methods.

Steps to Reproduce

  1. Create index.js file
    import { assertSpyCalls, spy } from "jsr:@std/testing/mock";
    import { FakeTime } from "jsr:@std/testing/time";
    import timers from "node:timers/promises";
    
    Deno.test("with timers", async () => {
        const init = async (cb) => {
            await timers.setTimeout(1000);
            cb();
        };
    
        using time = new FakeTime();
    
        const cb = spy();
        init(cb);
    
        time.tick(1000);
        await time.runMicrotasks();
        assertSpyCalls(cb, 1);
    });
    
    Deno.test("with setTimeout", async () => {
        const init = async (cb) => {
            await new Promise((r) => setTimeout(r, 1000));
            cb();
        };
    
        using time = new FakeTime();
    
        const cb = spy();
        init(cb);
    
        time.tick(1000);
        await time.runMicrotasks();
        assertSpyCalls(cb, 1);
    });
  2. Execute the command: deno test index.js
  3. See error
    running 2 tests from ./index.js
    with timers ... FAILED (3ms)
    with setTimeout ... ok (16ms)
    
     ERRORS 
    
    with timers => ./index.js:5:6
    error: AssertionError: Spy not called as much as expected:
    
    
        [Diff] Actual / Expected
    
    
    -   0
    +   1
    
        throw new AssertionError(message);
              ^
        at assertSpyCalls (https://jsr.io/@std/testing/1.0.19/mock.ts:1190:11)
        at file:///home/regseb/tmp/deno/index.js:18:5
    
     FAILURES 
    
    with timers => ./index.js:5:6
    
    FAILED | 1 passed | 1 failed (27ms)
    
    error: Test failed
    

Expected behavior

FakeTime mock the timersPromises.setTimeout([delay[, value[, options]]]) method.

Actual behavior

FakeTime does not mock the timersPromises.setTimeout() method.

Environment

  • OS: Ubuntu 25.04
  • deno version: 2.8.2
  • std version:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions