-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
162 lines (123 loc) · 3.83 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import test from 'ava';
import timeSpan from 'time-span';
import inRange from 'in-range';
import makeAsynchronous, {makeAsynchronousIterable} from './index.js';
const abortError = new Error('Aborted');
test('main', async t => {
const fixture = {x: '🦄'};
const end = timeSpan();
const result = await makeAsynchronous(fixture => {
let x = '1';
while (true) { // eslint-disable-line no-constant-condition
x += Math.random() < 0.5 ? Date.now().toString() : '0';
if (x >= 9_999_999_999_999) {
break;
}
}
return fixture;
})(fixture);
t.true(inRange(end(), {start: 10, end: 1000}), `${end()}`);
t.deepEqual(result, fixture);
});
test('with pre-aborted AbortSignal', async t => {
const controller = new AbortController();
controller.abort(abortError);
await t.throwsAsync(makeAsynchronous(() => {
while (true) {} // eslint-disable-line no-constant-condition, no-empty
}).withSignal(controller.signal), {
message: abortError.message,
});
});
test('with interrupting abortion of AbortSignal', async t => {
const controller = new AbortController();
const promise = makeAsynchronous(() => {
while (true) {} // eslint-disable-line no-constant-condition, no-empty
}).withSignal(controller.signal)();
controller.abort(abortError);
await t.throwsAsync(promise, {
message: abortError.message,
});
});
test('error', async t => {
await t.throwsAsync(
makeAsynchronous(() => {
throw new TypeError('unicorn');
})(),
{
instanceOf: TypeError,
message: 'unicorn',
},
);
});
test.failing('dynamic import works', async t => {
await t.notThrowsAsync(
makeAsynchronous(async () => {
await import('time-span');
})(),
);
});
test('iterator object', async t => {
const fixture = [1, 2];
const asyncIterable = makeAsynchronousIterable(fixture => fixture[Symbol.iterator]())(fixture);
const result = [];
for await (const value of asyncIterable) {
result.push(value);
}
t.deepEqual(result, fixture);
});
test('iterator object with pre-aborted AbortSignal', async t => {
const controller = new AbortController();
controller.abort(abortError);
const asyncIterable = makeAsynchronousIterable(function * () { // eslint-disable-line require-yield
while (true) {} // eslint-disable-line no-constant-condition, no-empty
}).withSignal(controller.signal)();
await t.throwsAsync(async () => {
for await (const _ of asyncIterable) {} // eslint-disable-line no-unused-vars, no-empty
}, {
message: abortError.message,
});
});
test('iterator object with interrupting abortion of AbortSignal', async t => {
const controller = new AbortController();
const asyncIterable = makeAsynchronousIterable(function * () { // eslint-disable-line require-yield
while (true) {} // eslint-disable-line no-constant-condition, no-empty
}).withSignal(controller.signal)();
controller.abort(abortError);
await t.throwsAsync(async () => {
for await (const _ of asyncIterable) {} // eslint-disable-line no-unused-vars, no-empty
}, {
message: abortError.message,
});
});
test('generator function', async t => {
const fixture = [1, 2];
const asyncIterable = makeAsynchronousIterable(function * (fixture) {
for (const value of fixture) {
yield value;
}
})(fixture);
const result = [];
for await (const value of asyncIterable) {
result.push(value);
}
t.deepEqual(result, fixture);
});
test('generator function that throws', async t => {
const fixture = [1, 2];
const errorMessage = 'Catch me if you can!';
const asyncIterable = makeAsynchronousIterable(function * (fixture, errorMessage) {
for (const value of fixture) {
yield value;
}
throw new Error(errorMessage);
})(fixture, errorMessage);
const result = [];
await t.throwsAsync(async () => {
for await (const value of asyncIterable) {
result.push(value);
}
}, {
message: errorMessage,
}, 'error is propagated');
t.deepEqual(result, fixture);
});