温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/test/default/MockTimers/runAll
点击访问原文链接

MockTimers.runAll method | Node.js node:test module | Bun

MockTimers.runAll method | Node.js node:test module | BunBuildDocsReferenceGuidesBlogDiscord/node:test/default/MockTimers/runAllMrunAll

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:test/default/MockTimers/runAllMrunAll

method

test.default.MockTimers.runAll { context.mock.timers.enable({ apis: ['setTimeout', 'Date'] }); const results = []; setTimeout(() => results.push(1), 9999); // Notice that if both timers have the same timeout, // the order of execution is guaranteed setTimeout(() => results.push(3), 8888); setTimeout(() => results.push(2), 8888); assert.deepStrictEqual(results, []); context.mock.timers.runAll(); assert.deepStrictEqual(results, [3, 2, 1]); // The Date object is also advanced to the furthest timer's time assert.strictEqual(Date.now(), 9999); }); ``` **Note:** The `runAll()` function is specifically designed for triggering timers in the context of timer mocking. It does not have any effect on real-time system clocks or actual timers outside of the mocking environment." data-algolia-static="false" data-algolia-merged="false" data-type="Method">runAll(): void;

Triggers all pending mocked timers immediately. If the Date object is also mocked, it will also advance the Date object to the furthest timer's time.

The example below triggers all pending timers immediately, causing them to execute without any delay.

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

test('runAll functions following the given order', (context) => {
context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
const results = [];
setTimeout(() => results.push(1), 9999);

// Notice that if both timers have the same timeout,
// the order of execution is guaranteed
setTimeout(() => results.push(3), 8888);
setTimeout(() => results.push(2), 8888);

assert.deepStrictEqual(results, []);

context.mock.timers.runAll();
assert.deepStrictEqual(results, [3, 2, 1]);
// The Date object is also advanced to the furthest timer's time
assert.strictEqual(Date.now(), 9999);
});

Note: The runAll() function is specifically designed for triggering timers in the context of timer mocking. It does not have any effect on real-time system clocks or actual timers outside of the mocking environment.

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

MockTimers.runAll method | Node.js node:test module | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for method node:test.default.MockTimers.runAll | Bun