-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathhelpers.test.ts
More file actions
92 lines (78 loc) · 2.36 KB
/
Copy pathhelpers.test.ts
File metadata and controls
92 lines (78 loc) · 2.36 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @vitest-environment node
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { chunkArray, interruptibleSleep, noop, sleep } from './helpers.js'
describe('sleep', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
it('resolves after the specified delay', async () => {
const promise = sleep(1000)
vi.advanceTimersByTime(1000)
await expect(promise).resolves.toBeUndefined()
})
it('does not resolve before the delay', async () => {
let resolved = false
sleep(1000).then(() => {
resolved = true
})
vi.advanceTimersByTime(999)
await Promise.resolve()
expect(resolved).toBe(false)
})
})
describe('interruptibleSleep', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
it('resolves after the delay when no signal is provided', async () => {
const promise = interruptibleSleep(1000)
vi.advanceTimersByTime(1000)
await expect(promise).resolves.toBeUndefined()
})
it('resolves after the delay when the signal never aborts', async () => {
const controller = new AbortController()
const promise = interruptibleSleep(1000, controller.signal)
vi.advanceTimersByTime(1000)
await expect(promise).resolves.toBeUndefined()
})
it('resolves early when the signal aborts mid-sleep', async () => {
const controller = new AbortController()
let resolved = false
interruptibleSleep(60_000, controller.signal).then(() => {
resolved = true
})
vi.advanceTimersByTime(1)
controller.abort()
await Promise.resolve()
expect(resolved).toBe(true)
})
it('resolves immediately for an already-aborted signal', async () => {
const controller = new AbortController()
controller.abort()
await expect(interruptibleSleep(60_000, controller.signal)).resolves.toBeUndefined()
})
})
describe('noop', () => {
it('is a function', () => {
expect(typeof noop).toBe('function')
})
it('returns undefined', () => {
expect(noop()).toBeUndefined()
})
})
describe('chunkArray', () => {
it('preserves order while bounding chunk size', () => {
expect(chunkArray([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]])
})
it('rejects a non-positive chunk size', () => {
expect(() => chunkArray([1], 0)).toThrow('positive integer')
})
})