aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets
diff options
context:
space:
mode:
Diffstat (limited to 'integration/bunjs-only-snippets')
-rw-r--r--integration/bunjs-only-snippets/fetch.test.js230
-rw-r--r--integration/bunjs-only-snippets/inspect.test.js80
-rw-r--r--integration/bunjs-only-snippets/text-encoder.test.js72
3 files changed, 310 insertions, 72 deletions
diff --git a/integration/bunjs-only-snippets/fetch.test.js b/integration/bunjs-only-snippets/fetch.test.js
index cfe24d5ac..29e37e26c 100644
--- a/integration/bunjs-only-snippets/fetch.test.js
+++ b/integration/bunjs-only-snippets/fetch.test.js
@@ -1,57 +1,139 @@
import { it, describe, expect } from "bun:test";
import fs from "fs";
+function gc() {
+ // console.trace();
+ Bun.gc(true);
+}
+
describe("fetch", () => {
const urls = ["https://example.com", "http://example.com"];
for (let url of urls) {
+ gc();
it(url, async () => {
+ gc();
const response = await fetch(url);
+ gc();
const text = await response.text();
-
- if (
+ gc();
+ expect(
fs.readFileSync(
import.meta.path.substring(0, import.meta.path.lastIndexOf("/")) +
"/fetch.js.txt",
"utf8"
- ) !== text
- ) {
- throw new Error("Expected fetch.js.txt to match snapshot");
- }
+ )
+ ).toBe(text);
});
}
});
function testBlobInterface(blobbyConstructor, hasBlobFn) {
- it("json", async () => {
- var response = blobbyConstructor(JSON.stringify({ hello: true }));
- expect(JSON.stringify(await response.json())).toBe(
- JSON.stringify({ hello: true })
- );
- });
- it("text", async () => {
- var response = blobbyConstructor(JSON.stringify({ hello: true }));
- expect(await response.text()).toBe(JSON.stringify({ hello: true }));
- });
- it("arrayBuffer", async () => {
- var response = blobbyConstructor(JSON.stringify({ hello: true }));
+ for (let withGC of [false, true]) {
+ it(`json${withGC ? " (with gc) " : ""}`, async () => {
+ if (withGC) gc();
+ var response = blobbyConstructor(JSON.stringify({ hello: true }));
+ if (withGC) gc();
+ expect(JSON.stringify(await response.json())).toBe(
+ JSON.stringify({ hello: true })
+ );
+ if (withGC) gc();
+ });
+
+ it(`arrayBuffer -> json${withGC ? " (with gc) " : ""}`, async () => {
+ if (withGC) gc();
+ var response = blobbyConstructor(
+ new TextEncoder().encode(JSON.stringify({ hello: true }))
+ );
+ if (withGC) gc();
+ expect(JSON.stringify(await response.json())).toBe(
+ JSON.stringify({ hello: true })
+ );
+ if (withGC) gc();
+ });
+
+ it(`text${withGC ? " (with gc) " : ""}`, async () => {
+ if (withGC) gc();
+ var response = blobbyConstructor(JSON.stringify({ hello: true }));
+ if (withGC) gc();
+ expect(await response.text()).toBe(JSON.stringify({ hello: true }));
+ if (withGC) gc();
+ });
+
+ it(`arrayBuffer -> text${withGC ? " (with gc) " : ""}`, async () => {
+ if (withGC) gc();
+ var response = blobbyConstructor(
+ new TextEncoder().encode(JSON.stringify({ hello: true }))
+ );
+ if (withGC) gc();
+ expect(await response.text()).toBe(JSON.stringify({ hello: true }));
+ if (withGC) gc();
+ });
+
+ it(`arrayBuffer${withGC ? " (with gc) " : ""}`, async () => {
+ if (withGC) gc();
- const bytes = new TextEncoder().encode(JSON.stringify({ hello: true }));
- const compare = new Uint8Array(await response.arrayBuffer());
- for (let i = 0; i < compare.length; i++) {
- expect(compare[i]).toBe(bytes[i]);
- }
- });
- hasBlobFn &&
- it("blob", async () => {
var response = blobbyConstructor(JSON.stringify({ hello: true }));
- const size = JSON.stringify({ hello: true }).length;
- const blobed = await response.blob();
- expect(blobed instanceof Blob).toBe(true);
- expect(blobed.size).toBe(size);
- expect(blobed.type).toBe("");
- blobed.type = "application/json";
- expect(blobed.type).toBe("application/json");
+ if (withGC) gc();
+
+ const bytes = new TextEncoder().encode(JSON.stringify({ hello: true }));
+ if (withGC) gc();
+
+ const compare = new Uint8Array(await response.arrayBuffer());
+ if (withGC) gc();
+
+ for (let i = 0; i < compare.length; i++) {
+ if (withGC) gc();
+
+ expect(compare[i]).toBe(bytes[i]);
+ if (withGC) gc();
+ }
+ if (withGC) gc();
});
+
+ it(`arrayBuffer -> arrayBuffer${withGC ? " (with gc) " : ""}`, async () => {
+ if (withGC) gc();
+
+ var response = blobbyConstructor(
+ new TextEncoder().encode(JSON.stringify({ hello: true }))
+ );
+ if (withGC) gc();
+
+ const bytes = new TextEncoder().encode(JSON.stringify({ hello: true }));
+ if (withGC) gc();
+
+ const compare = new Uint8Array(await response.arrayBuffer());
+ if (withGC) gc();
+
+ for (let i = 0; i < compare.length; i++) {
+ if (withGC) gc();
+
+ expect(compare[i]).toBe(bytes[i]);
+ if (withGC) gc();
+ }
+ if (withGC) gc();
+ });
+
+ hasBlobFn &&
+ it(`blob${withGC ? " (with gc) " : ""}`, async () => {
+ if (withGC) gc();
+ var response = blobbyConstructor(JSON.stringify({ hello: true }));
+ if (withGC) gc();
+ const size = JSON.stringify({ hello: true }).length;
+ if (withGC) gc();
+ const blobed = await response.blob();
+ if (withGC) gc();
+ expect(blobed instanceof Blob).toBe(true);
+ if (withGC) gc();
+ expect(blobed.size).toBe(size);
+ if (withGC) gc();
+ expect(blobed.type).toBe("");
+ if (withGC) gc();
+ blobed.type = "application/json";
+ if (withGC) gc();
+ expect(blobed.type).toBe("application/json");
+ if (withGC) gc();
+ });
+ }
}
describe("Blob", () => {
@@ -67,6 +149,7 @@ describe("Blob", () => {
[Uint8Array.from([1, 2, 3, 4]), "5678", 9],
[new Blob([Uint8Array.from([1, 2, 3, 4])]), "5678", 9],
];
+
var expected = [
"123456",
"123456",
@@ -95,39 +178,83 @@ describe("Blob", () => {
expect(res).toBe(expected[i]);
}
});
+
+ for (let withGC of [false, true]) {
+ it(`Blob.slice() ${withGC ? " with gc" : ""}`, async () => {
+ var parts = ["hello", " ", "world"];
+ if (withGC) gc();
+ var str = parts.join("");
+ if (withGC) gc();
+ var combined = new Blob(parts);
+ if (withGC) gc();
+ for (let part of parts) {
+ if (withGC) gc();
+ expect(
+ await combined
+ .slice(str.indexOf(part), str.indexOf(part) + part.length)
+ .text()
+ ).toBe(part);
+ if (withGC) gc();
+ }
+ if (withGC) gc();
+ for (let part of parts) {
+ if (withGC) gc();
+ expect(
+ await combined
+ .slice(str.indexOf(part), str.indexOf(part) + part.length)
+ .text()
+ ).toBe(part);
+ if (withGC) gc();
+ }
+ });
+ }
});
describe("Response", () => {
it("clone", async () => {
+ gc();
var body = new Response("<div>hello</div>", {
headers: {
"content-type": "text/html; charset=utf-8",
},
});
+ gc();
var clone = body.clone();
+ gc();
body.headers.set("content-type", "text/plain");
+ gc();
expect(clone.headers.get("content-type")).toBe("text/html; charset=utf-8");
+ gc();
expect(body.headers.get("content-type")).toBe("text/plain");
+ gc();
expect(await clone.text()).toBe("<div>hello</div>");
+ gc();
});
testBlobInterface((data) => new Response(data), true);
});
describe("Request", () => {
it("clone", async () => {
+ gc();
var body = new Request("https://hello.com", {
headers: {
"content-type": "text/html; charset=utf-8",
},
body: "<div>hello</div>",
});
+ gc();
expect(body.headers.get("content-type")).toBe("text/html; charset=utf-8");
-
+ gc();
var clone = body.clone();
+ gc();
body.headers.set("content-type", "text/plain");
+ gc();
expect(clone.headers.get("content-type")).toBe("text/html; charset=utf-8");
+ gc();
expect(body.headers.get("content-type")).toBe("text/plain");
+ gc();
expect(await clone.text()).toBe("<div>hello</div>");
+ gc();
});
testBlobInterface(
@@ -138,18 +265,31 @@ describe("Request", () => {
describe("Headers", () => {
it("writes", async () => {
- var body = new Request("https://hello.com", {
- headers: {
- "content-type": "text/html; charset=utf-8",
- },
- body: "<div>hello</div>",
+ var headers = new Headers({
+ "content-type": "text/html; charset=utf-8",
});
- expect(body.headers.get("content-type")).toBe("text/html; charset=utf-8");
+ gc();
+ expect(headers.get("content-type")).toBe("text/html; charset=utf-8");
+ gc();
+ headers.delete("content-type");
+ gc();
+ expect(headers.get("content-type")).toBe(null);
+ gc();
+ headers.append("content-type", "text/plain");
+ gc();
+ expect(headers.get("content-type")).toBe("text/plain");
+ gc();
+ headers.append("content-type", "text/plain");
+ gc();
+ expect(headers.get("content-type")).toBe("text/plain, text/plain");
+ gc();
+ headers.set("content-type", "text/html; charset=utf-8");
+ gc();
+ expect(headers.get("content-type")).toBe("text/html; charset=utf-8");
- var clone = body.clone();
- body.headers.set("content-type", "text/plain");
- expect(clone.headers.get("content-type")).toBe("text/html; charset=utf-8");
- expect(body.headers.get("content-type")).toBe("text/plain");
- expect(await clone.text()).toBe("<div>hello</div>");
+ headers.delete("content-type");
+ gc();
+ expect(headers.get("content-type")).toBe(null);
+ gc();
});
});
diff --git a/integration/bunjs-only-snippets/inspect.test.js b/integration/bunjs-only-snippets/inspect.test.js
index d934a57b8..71c9c25cc 100644
--- a/integration/bunjs-only-snippets/inspect.test.js
+++ b/integration/bunjs-only-snippets/inspect.test.js
@@ -1,5 +1,43 @@
import { it, expect } from "bun:test";
+it("console.trace", () => {
+ console.trace("Hi");
+});
+
+it("jsx with two elements", () => {
+ const input = Bun.inspect(
+ <div hello="quoted">
+ <input type="text" value={"123"} />
+ string inside child
+ </div>
+ );
+
+ const output = `<div hello="quoted">
+ <input type="text" value="123" />
+ string inside child
+</div>`;
+
+ expect(input).toBe(output);
+});
+
+const Foo = () => <div hello="quoted">foo</div>;
+
+it("jsx with anon component", () => {
+ const input = Bun.inspect(<Foo />);
+
+ const output = `<NoName />`;
+
+ expect(input).toBe(output);
+});
+
+it("jsx with fragment", () => {
+ const input = Bun.inspect(<>foo bar</>);
+
+ const output = `<>foo bar</>`;
+
+ expect(input).toBe(output);
+});
+
it("inspect", () => {
expect(Bun.inspect(new TypeError("what")).includes("TypeError: what")).toBe(
true
@@ -10,9 +48,51 @@ it("inspect", () => {
expect(Bun.inspect(1, "hi")).toBe("1 hi");
expect(Bun.inspect([])).toBe("[]");
expect(Bun.inspect({})).toBe("{ }");
+ expect(Bun.inspect({ hello: 1 })).toBe("{ hello: 1 }");
+ expect(Bun.inspect({ hello: 1, there: 2 })).toBe("{ hello: 1, there: 2 }");
+ expect(Bun.inspect({ hello: "1", there: 2 })).toBe(
+ '{ hello: "1", there: 2 }'
+ );
+ expect(Bun.inspect({ 'hello-"there': "1", there: 2 })).toBe(
+ '{ "hello-\\"there": "1", there: 2 }'
+ );
var str = "123";
while (str.length < 4096) {
str += "123";
}
expect(Bun.inspect(str)).toBe(str);
+ expect(Bun.inspect(new Headers())).toBe("Headers (0 KB) {}");
+ expect(Bun.inspect(new Response()).length > 0).toBe(true);
+
+ expect(
+ JSON.stringify(
+ new Headers({
+ hi: "ok",
+ })
+ )
+ ).toBe('{"hi":"ok"}');
+ expect(Bun.inspect(new Set())).toBe("Set {}");
+ expect(Bun.inspect(new Map())).toBe("Map {}");
+ expect(Bun.inspect(new Map([["foo", "bar"]]))).toBe(
+ 'Map(1) {\n "foo": "bar",\n}'
+ );
+ expect(Bun.inspect(new Set(["bar"]))).toBe('Set(1) {\n "bar",\n}');
+ expect(Bun.inspect(<div>foo</div>)).toBe("<div>foo</div>");
+ expect(Bun.inspect(<div hello>foo</div>)).toBe("<div hello=true>foo</div>");
+ expect(Bun.inspect(<div hello={1}>foo</div>)).toBe("<div hello=1>foo</div>");
+ expect(Bun.inspect(<div hello={123}>hi</div>)).toBe(
+ "<div hello=123>hi</div>"
+ );
+ expect(Bun.inspect(<div hello="quoted">quoted</div>)).toBe(
+ '<div hello="quoted">quoted</div>'
+ );
+ expect(
+ Bun.inspect(
+ <div hello="quoted">
+ <input type="text" value={"123"} />
+ </div>
+ )
+ ).toBe(`<div hello="quoted">
+ <input type="text" value="123" />
+</div>`);
});
diff --git a/integration/bunjs-only-snippets/text-encoder.test.js b/integration/bunjs-only-snippets/text-encoder.test.js
index e4bf35059..ddba40d75 100644
--- a/integration/bunjs-only-snippets/text-encoder.test.js
+++ b/integration/bunjs-only-snippets/text-encoder.test.js
@@ -1,4 +1,7 @@
import { expect, it, describe } from "bun:test";
+function gcTrace() {
+ Bun.gc(true);
+}
const getByteLength = (str) => {
// returns the byte length of an utf8 string
@@ -15,17 +18,22 @@ const getByteLength = (str) => {
describe("TextDecoder", () => {
it("should decode ascii text", () => {
const decoder = new TextDecoder("latin1");
+ gcTrace(true);
expect(decoder.encoding).toBe("windows-1252");
+ gcTrace(true);
expect(decoder.decode(new Uint8Array([0x41, 0x42, 0x43]))).toBe("ABC");
+ gcTrace(true);
const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33];
+ gcTrace(true);
expect(decoder.decode(Uint8Array.from(result))).toBe(
String.fromCharCode(...result)
);
+ gcTrace(true);
});
it("should decode unicode text", () => {
const decoder = new TextDecoder();
-
+ gcTrace(true);
var text = `❤️ Red Heart`;
const bytes = [
@@ -34,35 +42,45 @@ describe("TextDecoder", () => {
const decoded = decoder.decode(Uint8Array.from(bytes));
expect(decoder.encoding).toBe("utf-8");
+ gcTrace(true);
+
for (let i = 0; i < text.length; i++) {
expect(decoded.charCodeAt(i)).toBe(text.charCodeAt(i));
}
expect(decoded).toHaveLength(text.length);
+ gcTrace(true);
});
it("should decode unicode text with multiple consecutive emoji", () => {
const decoder = new TextDecoder();
const encoder = new TextEncoder();
-
+ gcTrace(true);
var text = `❤️❤️❤️❤️❤️❤️ Red Heart`;
text += ` ✨ Sparkles 🔥 Fire 😀 😃 😄 😁 😆 😅 😂 🤣 🥲 ☺️ 😊 😇 🙂 🙃 😉 😌 😍 🥰 😘 😗 😙 😚 😋 😛 😝 😜 🤪 🤨 🧐 🤓 😎 🥸 🤩 🥳 😏 😒 😞 😔 😟 😕 🙁 ☹️ 😣 😖 😫 😩 🥺 😢 😭 😤 😠 😡 🤬 🤯 😳 🥵 🥶 😱 😨 😰`;
-
+ gcTrace(true);
expect(decoder.decode(encoder.encode(text))).toBe(text);
-
+ gcTrace(true);
const bytes = new Uint8Array(getByteLength(text) * 8);
+ gcTrace(true);
const amount = encoder.encodeInto(text, bytes);
+ gcTrace(true);
expect(decoder.decode(bytes.subarray(0, amount.written))).toBe(text);
+ gcTrace(true);
});
});
describe("TextEncoder", () => {
it("should encode latin1 text", () => {
+ gcTrace(true);
const text = "Hello World!";
const encoder = new TextEncoder();
+ gcTrace(true);
const encoded = encoder.encode(text);
+ gcTrace(true);
expect(encoded instanceof Uint8Array).toBe(true);
expect(encoded.length).toBe(text.length);
+ gcTrace(true);
const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33];
for (let i = 0; i < result.length; i++) {
expect(encoded[i]).toBe(result[i]);
@@ -72,9 +90,12 @@ describe("TextEncoder", () => {
it("should encode long latin1 text", () => {
const text = "Hello World!".repeat(1000);
const encoder = new TextEncoder();
+ gcTrace(true);
const encoded = encoder.encode(text);
+ gcTrace(true);
expect(encoded instanceof Uint8Array).toBe(true);
expect(encoded.length).toBe(text.length);
+ gcTrace(true);
expect(new TextDecoder().decode(encoded)).toBe(text);
});
@@ -82,10 +103,14 @@ describe("TextEncoder", () => {
var text = "Hello";
text += " ";
text += "World!";
+
+ gcTrace(true);
const encoder = new TextEncoder();
const encoded = encoder.encode(text);
+ gcTrace(true);
const into = new Uint8Array(100);
const out = encoder.encodeInto(text, into);
+ gcTrace(true);
expect(out.read).toBe(text.length);
expect(out.written).toBe(encoded.length);
@@ -100,12 +125,14 @@ describe("TextEncoder", () => {
it("should encode utf-16 text", () => {
var text = `❤️ Red Heart
- ✨ Sparkles
- 🔥 Fire
- `;
+ ✨ Sparkles
+ 🔥 Fire
+ `;
var encoder = new TextEncoder();
var decoder = new TextDecoder();
+ gcTrace(true);
expect(decoder.decode(encoder.encode(text))).toBe(text);
+ gcTrace(true);
});
// this test is from a web platform test in WebKit
@@ -145,9 +172,16 @@ describe("TextEncoder", () => {
bad.forEach(function (t) {
it(t.encoding + " - " + t.name, () => {
+ gcTrace(true);
expect(
new TextDecoder(t.encoding).decode(new Uint8Array(t.input))
).toBe(t.expected);
+ expect(
+ new TextDecoder(t.encoding).decode(
+ new Uint16Array(new Uint8Array(t.input).buffer)
+ )
+ ).toBe(t.expected);
+ gcTrace(true);
});
// test(function () {
// assert_throws_js(TypeError, function () {
@@ -160,10 +194,9 @@ describe("TextEncoder", () => {
});
it("should encode utf-16 rope text", () => {
- var textReal = `❤️ Red Heart
- ✨ Sparkles
- 🔥 Fire
- `;
+ gcTrace(true);
+ var textReal = `❤️ Red Heart ✨ Sparkles 🔥 Fire`;
+
var a = textReal.split("");
var text = "";
for (let j of a) {
@@ -171,21 +204,6 @@ describe("TextEncoder", () => {
}
var encoder = new TextEncoder();
-
- var encoded = encoder.encode(text);
-
- expect(encoded instanceof Uint8Array).toBe(true);
- const result = [
- 226, 157, 164, 239, 184, 143, 32, 82, 101, 100, 32, 72, 101, 97, 114, 116,
- 10, 32, 32, 32, 32, 32, 32, 32, 32, 226, 156, 168, 32, 83, 112, 97, 114,
- 107, 108, 101, 115, 10, 32, 32, 32, 32, 32, 32, 32, 32, 240, 159, 148,
- 165, 32, 70, 105, 114, 101, 10, 32, 32, 32, 32,
- ];
- var len = Math.min(result.length, encoded.length);
-
- for (let i = 0; i < len; i++) {
- expect(encoded[i]).toBe(result[i]);
- }
- expect(encoded.length).toBe(getByteLength(textReal));
+ expect(new TextDecoder().decode(encoder.encode(text))).toBe(textReal);
});
});