-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
fs-promises-writeFile-async-iterator.test.ts
53 lines (46 loc) · 1.45 KB
/
fs-promises-writeFile-async-iterator.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
48
49
50
51
52
53
import { test, expect, mock } from "bun:test";
import { writeFile } from "fs/promises";
import { tempDirWithFiles } from "harness";
test("fs.promises.writeFile async iterator", async () => {
const dir = tempDirWithFiles("fs-promises-writeFile-async-iterator", {
"file1.txt": "0 Hello, world!",
});
const path = dir + "/file2.txt";
const stream = async function* () {
yield "1 ";
yield "Hello, ";
yield "world!";
};
await writeFile(path, stream());
expect(await Bun.file(path).text()).toBe("1 Hello, world!");
const bufStream = async function* () {
yield Buffer.from("2 ");
yield Buffer.from("Hello, ");
yield Buffer.from("world!");
};
await writeFile(path, bufStream());
expect(await Bun.file(path).text()).toBe("2 Hello, world!");
});
test("fs.promises.writeFile async iterator throws on invalid input", async () => {
const dir = tempDirWithFiles("fs-promises-writeFile-async-iterator", {
"file1.txt": "0 Hello, world!",
});
const symbolStream = async function* () {
yield Symbol("lolwhat");
};
expect(() => writeFile(dir + "/file2.txt", symbolStream())).toThrow();
expect(() =>
writeFile(
dir + "/file3.txt",
(async function* () {
yield "once";
throw new Error("good");
})(),
),
).toThrow("good");
const fn = {
[Symbol.asyncIterator]: mock(() => {}),
};
expect(() => writeFile(dir, fn)).toThrow();
expect(fn[Symbol.asyncIterator]).not.toBeCalled();
});