Skip to content

test_runner: MockTimers does not default interval delay or coerce string timeout delay #67576

Closed as not planned

Description

@archidoge0

Version

Node.js v24.22.2, v26.13.0 and v26.13.1

Platform

macOS 15.2, Darwin 24.2.0, arm64.

Subsystem

test_runner

What steps will reproduce the bug?

Save as repro.cjs:

const { mock } = require('node:test');
for (const omitted of [true, false]) {
  mock.timers.enable({ apis: ['setInterval'] });
  let count = 0;
  const callback = () => { count++; };
  const timer = omitted ? setInterval(callback) : setInterval(callback, 1);
  mock.timers.tick(3);
  clearInterval(timer);
  console.log(omitted ? 'omitted interval delay:' : 'explicit interval delay 1:', count);
  mock.timers.reset();
}
for (const delay of ['40', 40]) {
  mock.timers.enable({ apis: ['setTimeout'] });
  mock.timers.tick(10);
  let fired = false;
  setTimeout(() => { fired = true; }, delay);
  mock.timers.tick(100);
  console.log(typeof delay + ' timeout delay:', fired);
  mock.timers.reset();
}

Run:

node repro.cjs

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

Every run. The interval case omits delay; the timeout case uses a numeric string after advancing the mocked clock. Each case gets a fresh mock clock.

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

omitted interval delay: 3
explicit interval delay 1: 3
string timeout delay: true
number timeout delay: true

The setInterval documentation gives delay a default of 1. An omitted delay should therefore behave like an explicit 1, keeping the interval active. For the timeout comparison, the real Timeout implementation coerces delay to a number; the mock should likewise schedule both 40 and "40" at the same time. No relative ordering of different callbacks is required by this reproduction.

What do you see instead?

The listed versions produce:

omitted interval delay: 1
explicit interval delay 1: 3
string timeout delay: false
number timeout delay: true

Additional information

The mock #createTimer implementation computes now + delay without defaulting or coercing it. For an omitted interval delay, interval is undefined, so the tick loop treats it as a one-shot timer. #55375 addresses oversized delays, not these cases.

AI tools assisted with analysis and preparation of this report and its reproduction. A human has reviewed and confirmed the issue and verified that it is reproducible.

Credit: UCSB PLSE — https://ucsbplse.github.io/

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions