-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_test.ts
47 lines (43 loc) · 1.29 KB
/
collect_test.ts
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
import { test } from "@cross/test";
import { assertEquals, assertRejects } from "@std/assert";
import { deadline } from "./_testutil.ts";
import { collect } from "./collect.ts";
test("collect returns an empty array for an empty stream", async () => {
const stream = new ReadableStream<string>({
start(controller) {
controller.close();
},
});
const result = await collect(stream);
assertEquals(result, []);
});
test("collect returns all chunks in order for a non-empty stream", async () => {
const chunks = ["a", "b", "c"];
const stream = new ReadableStream<string>({
start(controller) {
chunks.forEach((chunk) => controller.enqueue(chunk));
controller.close();
},
});
const result = await collect(stream);
assertEquals(result, chunks);
});
test("collect throws an error when the stream emits an error", async () => {
const error = new Error("test error");
const stream = new ReadableStream<string>({
start(controller) {
controller.error(error);
},
});
await assertRejects(
() => collect(stream),
);
});
test("collect waits forever when the stream is not closed", async () => {
const stream = new ReadableStream<string>();
await assertRejects(
() => deadline(collect(stream), 100),
DOMException,
"Signal timed out.",
);
});