UNPKG

1.29 kBJavaScriptView Raw
1const delay = require('./delay')
2const { AbortController } = require('./AbortController')
3
4describe('delay', () => {
5 it('should wait for the specified time', async () => {
6 const start = Date.now()
7 await delay(100)
8 // 100 is less of a rule, more of a guideline
9 // according to CI
10 expect(Date.now() - start).toBeGreaterThanOrEqual(90)
11 })
12
13 it('should reject if signal is already aborted', async () => {
14 const signal = { aborted: true }
15 const start = Date.now()
16 await expect(delay(100, { signal })).rejects.toHaveProperty('name', 'AbortError')
17 // should really be instant but using a very large range in case CI decides to be super busy and block the event loop for a while
18 expect(Date.now() - start).toBeLessThan(50)
19 })
20
21 it('should reject when signal is aborted', async () => {
22 const controller = new AbortController()
23 const start = Date.now()
24 const testDelay = delay(100, { signal: controller.signal })
25 await Promise.all([
26 delay(50).then(() => controller.abort()),
27 expect(testDelay).rejects.toHaveProperty('name', 'AbortError'),
28 ])
29
30 // should have rejected before the timer is done
31 const time = Date.now() - start
32 expect(time).toBeGreaterThanOrEqual(50)
33 expect(time).toBeLessThan(100)
34 })
35})