forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-runner-mock-timers-esm.mjs
More file actions
45 lines (39 loc) · 1.42 KB
/
test-runner-mock-timers-esm.mjs
File metadata and controls
45 lines (39 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import '../common/index.mjs';
import { describe, it } from 'node:test';
import { setTimeout } from 'node:timers/promises';
import assert from 'node:assert';
describe('Mock Timers ESM Regression', () => {
it('should work with node:timers/promises and runAll() in ESM', async (t) => {
t.mock.timers.enable({ apis: ['Date', 'setTimeout'] });
const startTime = Date.now();
let t1 = 0;
await Promise.all([
(async () => {
await setTimeout(1000);
t1 = Date.now();
})(),
(async () => {
// Wait for the next tick to ensure setTimeout has been called
await new Promise((resolve) => process.nextTick(resolve));
t.mock.timers.runAll();
})(),
]);
assert.strictEqual(t1 - startTime, 1000);
});
it('should work with node:timers/promises and tick() in ESM', async (t) => {
t.mock.timers.enable({ apis: ['Date', 'setTimeout'] });
const startTime = Date.now();
let t1 = 0;
await Promise.all([
(async () => {
await setTimeout(500);
t1 = Date.now();
})(),
(async () => {
await new Promise((resolve) => process.nextTick(resolve));
t.mock.timers.tick(500);
})(),
]);
assert.strictEqual(t1 - startTime, 500);
});
});