Skip to content

Commit 4d2a47e

Browse files
committed
test(node:fs): add test that actually checks that re-exported streams work
1 parent 048410e commit 4d2a47e

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

test/bun.js/fs.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,25 @@ describe("fs.WriteStream", () => {
540540
expect(stream instanceof WriteStreamStar_).toBe(true);
541541
expect(stream instanceof fs.WriteStream).toBe(true);
542542
});
543+
544+
it("should be able to write to a file with re-exported WriteStream", (done) => {
545+
const pathToDir = `${tmpdir()}/${Date.now()}`;
546+
mkdirSync(pathToDir);
547+
const path = `${pathToDir}/fs-writestream-re-exported-test.txt`;
548+
549+
const stream = new WriteStream_(path, { flags: "w+" });
550+
stream.write("Test file written successfully");
551+
stream.end();
552+
553+
stream.on("error", (e) => {
554+
done(e instanceof Error ? e : new Error(e));
555+
});
556+
557+
stream.on("finish", () => {
558+
expect(readFileSync(path, "utf8")).toBe("Test file written successfully");
559+
done();
560+
});
561+
});
543562
});
544563

545564
describe("fs.ReadStream", () => {
@@ -607,6 +626,34 @@ describe("fs.ReadStream", () => {
607626
expect(stream instanceof ReadStream_).toBe(true);
608627
expect(stream instanceof fs.ReadStream).toBe(true);
609628
});
629+
630+
it("should be able to read from a file, with re-exported ReadStream", (done) => {
631+
const pathToDir = `${tmpdir()}/${Date.now()}`;
632+
mkdirSync(pathToDir);
633+
const path = `${pathToDir}fs-readstream-re-exported-test.txt`;
634+
635+
writeFileSync(path, "Test file written successfully", {
636+
encoding: "utf8",
637+
flag: "w+",
638+
});
639+
640+
const stream = new ReadStream_(path);
641+
stream.setEncoding("utf8");
642+
stream.on("error", (e) => {
643+
done(e instanceof Error ? e : new Error(e));
644+
});
645+
646+
let data = "";
647+
648+
stream.on("data", (chunk) => {
649+
data += chunk;
650+
});
651+
652+
stream.on("end", () => {
653+
expect(data).toBe("Test file written successfully");
654+
done();
655+
});
656+
});
610657
});
611658

612659
describe("createWriteStream", () => {

0 commit comments

Comments
 (0)