Files
bun.sh/test/js/node/fs/fs-promises-writeFile-async-iterator.test.ts
2024-09-03 21:32:52 -07:00

54 lines
1.5 KiB
TypeScript

import { expect, mock, test } 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();
});