定时器
【Timers】
模拟定时器是一种常用于软件测试的技术,用于模拟和控制定时器的行为,例如 setInterval 和 setTimeout,而无需实际等待指定的时间间隔。
【Mocking timers is a technique commonly used in software testing to simulate and
control the behavior of timers, such as setInterval and setTimeout,
without actually waiting for the specified time intervals.】
请参阅 MockTimers 类以获取完整的方法和功能列表。
【Refer to the MockTimers class for a full list of methods and features.】
这允许开发者为时间相关的功能编写更可靠、更可预测的测试。
【This allows developers to write more reliable and predictable tests for time-dependent functionality.】
下面的示例显示了如何模拟 setTimeout。使用 .enable({ apis: ['setTimeout'] }); 它将模拟 node:timers 和 node:timers/promises 模块中的 setTimeout 函数,以及来自 Node.js 全局上下文的 setTimeout。
【The example below shows how to mock setTimeout.
Using .enable({ apis: ['setTimeout'] });
it will mock the setTimeout functions in the node:timers and
node:timers/promises modules,
as well as from the Node.js global context.】
注意: 像 import { setTimeout } from 'node:timers' 这样的解构函数目前不被此 API 支持。
import assert from 'node:assert';
import { mock, test } from 'node:test';
test('mocks setTimeout to be executed synchronously without having to actually wait for it', () => {
const fn = mock.fn();
// Optionally choose what to mock
mock.timers.enable({ apis: ['setTimeout'] });
setTimeout(fn, 9999);
assert.strictEqual(fn.mock.callCount(), 0);
// Advance in time
mock.timers.tick(9999);
assert.strictEqual(fn.mock.callCount(), 1);
// Reset the globally tracked mocks.
mock.timers.reset();
// If you call reset mock instance, it will also reset timers instance
mock.reset();
});const assert = require('node:assert');
const { mock, test } = require('node:test');
test('mocks setTimeout to be executed synchronously without having to actually wait for it', () => {
const fn = mock.fn();
// Optionally choose what to mock
mock.timers.enable({ apis: ['setTimeout'] });
setTimeout(fn, 9999);
assert.strictEqual(fn.mock.callCount(), 0);
// Advance in time
mock.timers.tick(9999);
assert.strictEqual(fn.mock.callCount(), 1);
// Reset the globally tracked mocks.
mock.timers.reset();
// If you call reset mock instance, it will also reset timers instance
mock.reset();
});相同的模拟功能也可以通过每个测试的 TestContext 对象上的 mock 属性访问。通过测试上下文进行模拟的好处是,一旦测试结束,测试运行器会自动恢复所有被模拟的定时器功能。
【The same mocking functionality is also exposed in the mock property on the TestContext object
of each test. The benefit of mocking via the test context is
that the test runner will automatically restore all mocked timers
functionality once the test finishes.】
import assert from 'node:assert';
import { test } from 'node:test';
test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
const fn = context.mock.fn();
// Optionally choose what to mock
context.mock.timers.enable({ apis: ['setTimeout'] });
setTimeout(fn, 9999);
assert.strictEqual(fn.mock.callCount(), 0);
// Advance in time
context.mock.timers.tick(9999);
assert.strictEqual(fn.mock.callCount(), 1);
});const assert = require('node:assert');
const { test } = require('node:test');
test('mocks setTimeout to be executed synchronously without having to actually wait for it', (context) => {
const fn = context.mock.fn();
// Optionally choose what to mock
context.mock.timers.enable({ apis: ['setTimeout'] });
setTimeout(fn, 9999);
assert.strictEqual(fn.mock.callCount(), 0);
// Advance in time
context.mock.timers.tick(9999);
assert.strictEqual(fn.mock.callCount(), 1);
});