-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.js
124 lines (107 loc) · 2.41 KB
/
test.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import test from 'ava';
import delay from 'delay';
import timeSpan from 'time-span';
import pWaitFor from './index.js';
test('waits for condition', async t => {
const ms = 200;
const end = timeSpan();
await pWaitFor(async () => {
await delay(ms);
return true;
});
t.true(end() > (ms - 20));
});
test('rejects promise if condition rejects or throws', async t => {
await t.throwsAsync(pWaitFor(() => {
throw new Error('foo');
}));
});
test('waits no longer than `timeout` milliseconds before rejecting', async t => {
const end = timeSpan();
const ms = 200;
const maxWait = 100;
await t.throwsAsync(pWaitFor(async () => {
await delay(ms);
return true;
}, {
interval: 20,
timeout: maxWait,
}));
const timeTaken = end();
t.true(timeTaken < ms);
t.true(timeTaken > (maxWait - 20));
});
test('stops performing checks if a timeout occurs', async t => {
let checksPerformed = 0;
await pWaitFor(
() => {
checksPerformed += 1;
return false;
},
{
interval: 10,
timeout: 200,
},
)
.catch(async _ => {
const checksAtTimeout = checksPerformed;
await delay(100);
t.is(checksPerformed, checksAtTimeout);
});
});
test('stops performing async checks if a timeout occurs', async t => {
let checksPerformed = 0;
try {
await pWaitFor(
async () => {
await new Promise(resolve => {
setTimeout(resolve, 20);
});
checksPerformed += 1;
return false;
},
{
interval: 10,
timeout: 200,
});
} catch {
const checksAtTimeout = checksPerformed;
await delay(100);
// One more check might have been run if it was already started
t.true([checksAtTimeout, checksAtTimeout + 1].includes(checksPerformed));
}
});
test('does not perform a leading check', async t => {
const ms = 200;
const end = timeSpan();
await pWaitFor(async () => true, {
interval: ms,
before: false,
});
t.true(end() > (ms - 20));
});
test('resolveWith()', async t => {
t.true(await pWaitFor(() => pWaitFor.resolveWith(true)));
});
test('timeout option - object', async t => {
class CustomizedTimeoutError extends Error {
constructor() {
super();
this.name = 'MyError';
this.message = 'Time’s up!';
}
}
await t.throwsAsync(pWaitFor(async () => {
await delay(1000);
return true;
}, {
timeout: {
milliseconds: 100,
message: new CustomizedTimeoutError(),
},
}), {
name: 'MyError',
message: 'Time’s up!',
instanceOf: CustomizedTimeoutError,
});
});