aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets/response.file.test.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-04 01:07:38 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-04 01:07:38 -0700
commitafc7da33c952c1f900317eb022187f9be40643c6 (patch)
tree3f355e6dfad3c22ff46f669a3b9bf7307116426c /integration/bunjs-only-snippets/response.file.test.js
parentd820a9890fe39c6a737da542657489cfc7505b39 (diff)
downloadbun-afc7da33c952c1f900317eb022187f9be40643c6.tar.gz
bun-afc7da33c952c1f900317eb022187f9be40643c6.tar.zst
bun-afc7da33c952c1f900317eb022187f9be40643c6.zip
Add a couple more tests
Diffstat (limited to 'integration/bunjs-only-snippets/response.file.test.js')
-rw-r--r--integration/bunjs-only-snippets/response.file.test.js27
1 files changed, 27 insertions, 0 deletions
diff --git a/integration/bunjs-only-snippets/response.file.test.js b/integration/bunjs-only-snippets/response.file.test.js
index 97e5902c5..a7c829d57 100644
--- a/integration/bunjs-only-snippets/response.file.test.js
+++ b/integration/bunjs-only-snippets/response.file.test.js
@@ -170,6 +170,7 @@ it("Bun.file -> Response", async () => {
await gcTick();
const resp = await fetch("https://example.com");
await gcTick();
+
expect(await Bun.write("/tmp/fetch.js.out", resp)).toBe(text.length);
await gcTick();
expect(await Bun.file("/tmp/fetch.js.out").text()).toBe(text);
@@ -189,3 +190,29 @@ it("Response -> Bun.file -> Response -> text", async () => {
expect(await response2.text()).toBe(text);
await gcTick();
});
+
+// If you write nothing to a file, it shouldn't modify it
+// If you want to truncate a file, it should be more explicit
+it("Bun.write('output.html', '')", async () => {
+ await Bun.write("/tmp/output.html", "lalalala");
+ expect(await Bun.write("/tmp/output.html", "")).toBe(0);
+ expect(await Bun.file("/tmp/output.html").text()).toBe("lalalala");
+});
+
+// Since Bun.file is resolved lazily, this needs to specifically be checked
+it("Bun.write('output.html', HTMLRewriter.transform(Bun.file)))", async () => {
+ var rewriter = new HTMLRewriter();
+ rewriter.on("div", {
+ element(element) {
+ element.setInnerContent("<blink>it worked!</blink>", { html: true });
+ },
+ });
+ await Bun.write("/tmp/html-rewriter.txt.js", "<div>hello</div>");
+ var input = new Response(Bun.file("/tmp/html-rewriter.txt.js"));
+ var output = rewriter.transform(input);
+ const outpath = `/tmp/html-rewriter.${Date.now()}.html`;
+ await Bun.write(outpath, output);
+ expect(await Bun.file(outpath).text()).toBe(
+ "<div><blink>it worked!</blink></div>"
+ );
+});