Skip to content

A test in official document failed: runs timers as setTime passes ticks. #61039

@boningmaple

Description

@boningmaple

Version

v25.2.1

Platform

Darwin bonings-MacBook-Pro.local 25.1.0 Darwin Kernel Version 25.1.0: Mon Oct 20 19:26:51 PDT 2025; root:xnu-12377.41.6~2/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

  1. The code snippet is the official document code snippet, link below:

The code snippet in the Node official document.

import assert from 'node:assert';
import { test } from 'node:test';

test('runs timers as setTime passes ticks', (context) => {
  // Optionally choose what to mock
  context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
  const fn = context.mock.fn();
  setTimeout(fn, 1000);

  context.mock.timers.setTime(800);
  // Timer is not executed as the time is not yet reached
  assert.strictEqual(fn.mock.callCount(), 0);
  assert.strictEqual(Date.now(), 800);

  context.mock.timers.setTime(1200);
  // Timer is executed as the time is now reached
  assert.strictEqual(fn.mock.callCount(), 1);
  assert.strictEqual(Date.now(), 1200);
});
  1. Run this code with node v25, and it failed.
✖ failing tests:

test at node-test-runs-timers-as-setTime-passes-ticks.test.js:4:1
✖ runs timers as setTime passes ticks (3.751258ms)
  AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

  0 !== 1

      at TestContext.<anonymous> (file:///Users/boning/Documents/Github/js-ts-lab/node-test-runs-timers-as-setTime-passes-ticks.test.js:18:9)
      at Test.runInAsyncScope (node:async_hooks:213:14)
      at Test.run (node:internal/test_runner/test:1106:25)
      at Test.start (node:internal/test_runner/test:1003:17)
      at startSubtestAfterBootstrap (node:internal/test_runner/harness:358:17)
      at process.processTicksAndRejections (node:internal/process/task_queues:103:5) {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: 0,
    expected: 1,
    operator: 'strictEqual',
    diff: 'simple'
  }

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

Not required condition, reproduce every time when running the code.

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

What is the expected behavior?

The test should pass. after calling context.mock.timers.setTime(1200), the timer scheduled for 1000ms should execute, the result of fn.mock.callCount() should be 1.

Why is that the expected behavior?

The Node.js document states:

"If you have any timer that's set to run in the past, it will be executed as if the .tick() method has been called."

If I understand it correctly, the second call of the setTime should let the mocked function be executed.

What do you see instead?

The test failed:

✖ failing tests:

test at node-test-runs-timers-as-setTime-passes-ticks.test.js:4:1
✖ runs timers as setTime passes ticks (3.751258ms)
  AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

  0 !== 1

      at TestContext.<anonymous> (file:///Users/boning/Documents/Github/js-ts-lab/node-test-runs-timers-as-setTime-passes-ticks.test.js:18:9)
      at Test.runInAsyncScope (node:async_hooks:213:14)
      at Test.run (node:internal/test_runner/test:1106:25)
      at Test.start (node:internal/test_runner/test:1003:17)
      at startSubtestAfterBootstrap (node:internal/test_runner/harness:358:17)
      at process.processTicksAndRejections (node:internal/process/task_queues:103:5) {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: 0,
    expected: 1,
    operator: 'strictEqual',
    diff: 'simple'
  }

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions