Skip to content

mock.timers removes limit 2147483647 for setTimeout delay #55367

@regseb

Description

@regseb

Version

v22.9.0

Platform

Linux regseblaptop 6.8.0-45-generic #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

Ubuntu 24.04.1 LTS

What steps will reproduce the bug?

  1. Create test.js
import assert from 'node:assert/strict';
import { describe, it, mock } from 'node:test';

describe("testcase", () => {
    it("native", async () => {
        const fn = mock.fn();
        setTimeout(fn, 2_147_483_647 + 1);

        // Wait 1 millisecond.
        await new Promise((resolve) => setTimeout(resolve, 1));

        assert.equal(fn.mock.callCount(), 1);
    });

    it("mock", async () => {
        mock.timers.enable({ apis: ["setTimeout"] });

        const fn = mock.fn();
        setTimeout(fn, 2_147_483_647 + 1);

        // Advance by 1 millisecond.
        mock.timers.tick(1);

        assert.equal(fn.mock.callCount(), 1);
    });
});
  1. node --test test.mjs

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior? Why is that the expected behavior?

(node:74278) TimeoutOverflowWarning: 2147483648 does not fit into a 32-bit signed integer.
Timeout duration was set to 1.
(Use `node --trace-warnings ...` to show where the warning was created)
(node:74278) ExperimentalWarning: The MockTimers API is an experimental feature and might change at any time
▶ testcase
  ✔ native (8.692551ms)
  ✔ mock (0.953117ms)
▶ testcase (11.250127ms)
ℹ tests 2
ℹ suites 1
ℹ pass 2
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 79.820412

What do you see instead?

(node:74346) TimeoutOverflowWarning: 2147483648 does not fit into a 32-bit signed integer.
Timeout duration was set to 1.
(Use `node --trace-warnings ...` to show where the warning was created)
(node:74346) ExperimentalWarning: The MockTimers API is an experimental feature and might change at any time
▶ testcase
  ✔ native (7.450392ms)
  ✖ mock (2.579836ms)
    AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
    
    0 !== 1
    
        at TestContext.<anonymous> (file:///home/regseb/testcase/test.mjs:24:16)
        at Test.runInAsyncScope (node:async_hooks:211:14)
        at Test.run (node:internal/test_runner/test:930:25)
        at Suite.processPendingSubtests (node:internal/test_runner/test:629:18)
        at Test.postRun (node:internal/test_runner/test:1026:19)
        at Test.run (node:internal/test_runner/test:969:12)
        at async Promise.all (index 0)
        at async Suite.run (node:internal/test_runner/test:1295:7)
        at async startSubtestAfterBootstrap (node:internal/test_runner/harness:289:3) {
      generatedMessage: true,
      code: 'ERR_ASSERTION',
      actual: 0,
      expected: 1,
      operator: 'strictEqual'
    }

▶ testcase (11.83703ms)
ℹ tests 2
ℹ suites 1
ℹ pass 1
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 83.201768

✖ failing tests:

test at test.mjs:15:5
✖ mock (2.579836ms)
  AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
  
  0 !== 1
  
      at TestContext.<anonymous> (file:///home/regseb/testcase/test.mjs:24:16)
      at Test.runInAsyncScope (node:async_hooks:211:14)
      at Test.run (node:internal/test_runner/test:930:25)
      at Suite.processPendingSubtests (node:internal/test_runner/test:629:18)
      at Test.postRun (node:internal/test_runner/test:1026:19)
      at Test.run (node:internal/test_runner/test:969:12)
      at async Promise.all (index 0)
      at async Suite.run (node:internal/test_runner/test:1295:7)
      at async startSubtestAfterBootstrap (node:internal/test_runner/harness:289:3) {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: 0,
    expected: 1,
    operator: 'strictEqual'
  }

Additional information

If setTimeout is called with a delay larger than 2147483647, the delay will be set to 1. When you mock setTimeout with mock.timers, there's no limit: setTimeout supports delay larger than 2147483647.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssues that are suitable for first-time contributors.test_runnerIssues and PRs related to the test runner subsystem.timersIssues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions