test(js): false negative in sleep test

It is entirely possible that the difference between the specified
sleep time and the actual sleep time is greater than 15 seconds.

https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#Notes

> Note that in either case, the actual delay may be longer than
> intended; see Reasons for delays longer than specified below.

It is however an error for the delay to be shorter.

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
 FAIL  web_src/js/utils.test.js > sleep
AssertionError: expected false to be truthy
 ❯ testSleep web_src/js/utils.test.js:192:48
    190|   const endTime = Date.now();    // Record the end time
    191|   const actualSleepTime = endTime - startTime;
    192|   expect(Math.abs(actualSleepTime - ms) <= 15).toBeTruthy();
       |                                                ^
    193| }
    194|
 ❯ web_src/js/utils.test.js:184:3
This commit is contained in:
Earl Warren 2024-07-02 19:29:04 +02:00
parent 9524361bb4
commit a0d9d8d576
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00

View file

@ -177,10 +177,6 @@ test('serializeXml', () => {
}); });
test('sleep', async () => { test('sleep', async () => {
// Test 500 ms sleep
await testSleep(500);
// Test 2000 ms sleep
await testSleep(2000); await testSleep(2000);
}); });
@ -189,5 +185,5 @@ async function testSleep(ms) {
await sleep(ms); await sleep(ms);
const endTime = Date.now(); // Record the end time const endTime = Date.now(); // Record the end time
const actualSleepTime = endTime - startTime; const actualSleepTime = endTime - startTime;
expect(Math.abs(actualSleepTime - ms) <= 15).toBeTruthy(); expect(actualSleepTime >= ms).toBeTruthy();
} }