timers.enable([enableOptions])


启用指定定时器的定时器模拟。

¥Enables timer mocking for the specified timers.

  • enableOptions <Object> 用于启用定时器模拟的可选配置选项。支持以下属性:

    ¥enableOptions <Object> Optional configuration options for enabling timer mocking. The following properties are supported:

    • apis <Array> 包含要模拟的定时器的可选数组。当前支持的定时器值为 'setInterval''setTimeout''setImmediate''Date'。默认值:['setInterval', 'setTimeout', 'setImmediate', 'Date']。如果没有提供数组,则默认模拟所有时间相关的 API('setInterval''clearInterval''setTimeout''clearTimeout''setImmediate''clearImmediate''Date')。

      ¥apis <Array> An optional array containing the timers to mock. The currently supported timer values are 'setInterval', 'setTimeout', 'setImmediate', and 'Date'. Default: ['setInterval', 'setTimeout', 'setImmediate', 'Date']. If no array is provided, all time related APIs ('setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'setImmediate', 'clearImmediate', and 'Date') will be mocked by default.

    • now <number> | <Date> 表示用作 Date.now() 值的初始时间(以毫秒为单位)的可选数字或日期对象。默认值:0

      ¥now <number> | <Date> An optional number or Date object representing the initial time (in milliseconds) to use as the value for Date.now(). Default: 0.

注意:当你为特定定时器启用模拟时,其关联的清除函数也将被隐式模拟。

¥Note: When you enable mocking for a specific timer, its associated clear function will also be implicitly mocked.

注意:模拟 Date 将影响模拟定时器的行为,因为它们使用相同的内部时钟。

¥Note: Mocking Date will affect the behavior of the mocked timers as they use the same internal clock.

不设置初始时间的示例用法:

¥Example usage without setting initial time:

import { mock } from 'node:test';
mock.timers.enable({ apis: ['setInterval'] });const { mock } = require('node:test');
mock.timers.enable({ apis: ['setInterval'] });

上面的示例启用了对 setInterval 定时器的模拟,并隐式模拟了 clearInterval 函数。仅 node:timersnode:timers/promisesglobalThis 中的 setIntervalclearInterval 函数将被模拟。

¥The above example enables mocking for the setInterval timer and implicitly mocks the clearInterval function. Only the setInterval and clearInterval functions from node:timers, node:timers/promises, and globalThis will be mocked.

设置初始时间的示例用法

¥Example usage with initial time set

import { mock } from 'node:test';
mock.timers.enable({ apis: ['Date'], now: 1000 });const { mock } = require('node:test');
mock.timers.enable({ apis: ['Date'], now: 1000 });

将初始 Date 对象设置为时间的示例用法

¥Example usage with initial Date object as time set

import { mock } from 'node:test';
mock.timers.enable({ apis: ['Date'], now: new Date() });const { mock } = require('node:test');
mock.timers.enable({ apis: ['Date'], now: new Date() });

或者,如果你调用 mock.timers.enable() 时不带任何参数:

¥Alternatively, if you call mock.timers.enable() without any parameters:

所有计时器('setInterval''clearInterval''setTimeout''clearTimeout''setImmediate''clearImmediate')都将被模拟。node:timersnode:timers/promisesglobalThis 中的 setIntervalclearIntervalsetTimeoutclearTimeoutsetImmediateclearImmediate 函数将被模拟。以及全局 Date 对象。

¥All timers ('setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'setImmediate', and 'clearImmediate') will be mocked. The setInterval, clearInterval, setTimeout, clearTimeout, setImmediate, and clearImmediate functions from node:timers, node:timers/promises, and globalThis will be mocked. As well as the global Date object.