Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/atomic-sleep/test.js
1126 views
1
'use strict'
2
const test = require('tape')
3
const sleep = require('.')
4
5
test('blocks event loop for given amount of milliseconds', ({ is, end }) => {
6
const now = Date.now()
7
setTimeout(() => {
8
const delta = Date.now() - now
9
const fuzzyDelta = Math.floor(delta / 10) * 10 // allow up to 10ms of execution lag
10
is(fuzzyDelta, 1000)
11
end()
12
}, 100)
13
sleep(1000)
14
})
15
16
if (typeof BigInt !== 'undefined') {
17
18
test('allows ms to be supplied as a BigInt number', ({ is, end }) => {
19
const now = Date.now()
20
setTimeout(() => {
21
const delta = Date.now() - now
22
const fuzzyDelta = Math.floor(delta / 10) * 10 // allow up to 10ms of execution lag
23
is(fuzzyDelta, 1000)
24
end()
25
}, 100)
26
sleep(BigInt(1000)) // avoiding n notation as this will error on legacy node/browsers
27
})
28
29
}
30
31
test('throws range error if ms less than 0', ({ throws, end }) => {
32
throws(() => sleep(-1), RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity'))
33
end()
34
})
35
36
test('throws range error if ms is Infinity', ({ throws, end }) => {
37
throws(() => sleep(Infinity), RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity'))
38
end()
39
})
40
41
test('throws range error if ms is not a number or bigint', ({ throws, end }) => {
42
throws(() => sleep('Infinity'), TypeError('sleep: ms must be a number'))
43
throws(() => sleep('foo'), TypeError('sleep: ms must be a number'))
44
throws(() => sleep({a: 1}), TypeError('sleep: ms must be a number'))
45
throws(() => sleep([1,2,3]), TypeError('sleep: ms must be a number'))
46
end()
47
})
48