timers.enable([timers])
启用指定定时器的定时器模拟。
¥Enables timer mocking for the specified timers.
-
timers
<Array> 包含要模拟的定时器的可选数组。当前支持的定时器值为'setInterval'
、'setTimeout'
和'setImmediate'
。如果未提供值,则默认情况下将模拟所有定时器('setInterval'
、'clearInterval'
、'setTimeout'
、'clearTimeout'
、'setImmediate'
和'clearImmediate'
)。¥
timers
<Array> An optional array containing the timers to mock. The currently supported timer values are'setInterval'
,'setTimeout'
, and'setImmediate'
. If no value is provided, all timers ('setInterval'
,'clearInterval'
,'setTimeout'
,'clearTimeout'
,'setImmediate'
, and'clearImmediate'
) will be mocked by default.
注意:当你为特定定时器启用模拟时,其关联的清除函数也将被隐式模拟。
¥Note: When you enable mocking for a specific timer, its associated clear function will also be implicitly mocked.
用法示例:
¥Example usage:
import { mock } from 'node:test';
mock.timers.enable(['setInterval']);
const { mock } = require('node:test');
mock.timers.enable(['setInterval']);
上面的示例启用了对 setInterval
定时器的模拟,并隐式模拟了 clearInterval
函数。仅 node:timers、node:timers/promises 和 globalThis
中的 setInterval
和 clearInterval
函数将被模拟。
¥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.
或者,如果你调用 mock.timers.enable()
时不带任何参数:
¥Alternatively, if you call mock.timers.enable()
without any parameters:
所有定时器('setInterval'
、'clearInterval'
、'setTimeout'
和 'clearTimeout'
)都将被模拟。node:timers
、node:timers/promises
和 globalThis
中的 setInterval
、clearInterval
、setTimeout
和 clearTimeout
函数将被模拟。
¥All timers ('setInterval'
, 'clearInterval'
, 'setTimeout'
, and 'clearTimeout'
)
will be mocked. The setInterval
, clearInterval
, setTimeout
, and clearTimeout
functions from node:timers
, node:timers/promises
,
and globalThis
will be mocked.