aboutsummaryrefslogtreecommitdiff
path: root/test/js
diff options
context:
space:
mode:
Diffstat (limited to 'test/js')
-rw-r--r--test/js/bun/globals.test.js2
-rw-r--r--test/js/bun/net/socket.test.ts10
-rw-r--r--test/js/bun/test/test-test.test.ts80
-rw-r--r--test/js/bun/util/filesystem_router.test.ts4
-rw-r--r--test/js/node/os/os.test.js2
-rw-r--r--test/js/web/fetch/fetch.test.ts30
-rw-r--r--test/js/web/html/FormData.test.ts13
-rw-r--r--test/js/web/streams/streams.test.js2
8 files changed, 125 insertions, 18 deletions
diff --git a/test/js/bun/globals.test.js b/test/js/bun/globals.test.js
index 5d491cde7..ac2d40659 100644
--- a/test/js/bun/globals.test.js
+++ b/test/js/bun/globals.test.js
@@ -5,7 +5,7 @@ it("extendable", () => {
// None of these should error
for (let Class of classes) {
var Foo = class extends Class {};
- var bar = new Foo();
+ var bar = Class === Request ? new Request({ url: "https://example.com" }) : new Foo();
expect(bar instanceof Class).toBe(true);
expect(!!Class.prototype).toBe(true);
expect(typeof Class.prototype).toBe("object");
diff --git a/test/js/bun/net/socket.test.ts b/test/js/bun/net/socket.test.ts
index 9ee60bc63..7f509966a 100644
--- a/test/js/bun/net/socket.test.ts
+++ b/test/js/bun/net/socket.test.ts
@@ -1,5 +1,5 @@
import { expect, it } from "bun:test";
-import { bunExe, expectObjectTypeCount } from "harness";
+import { bunEnv, bunExe, expectMaxObjectTypeCount } from "harness";
import { connect, spawn } from "bun";
it("should keep process alive only when active", async () => {
@@ -9,9 +9,7 @@ it("should keep process alive only when active", async () => {
stdout: "pipe",
stdin: null,
stderr: "pipe",
- env: {
- BUN_DEBUG_QUIET_LOGS: 1,
- },
+ env: bunEnv,
});
expect(await exited).toBe(0);
@@ -92,7 +90,7 @@ it("should reject on connection error, calling both connectError() and rejecting
});
it("should not leak memory when connect() fails", async () => {
- await expectObjectTypeCount("TCPSocket", 1, 100);
+ await expectMaxObjectTypeCount("TCPSocket", 1, 100);
});
// this also tests we mark the promise as handled if connectError() is called
@@ -134,5 +132,5 @@ it("should handle connection error", done => {
});
it("should not leak memory when connect() fails again", async () => {
- await expectObjectTypeCount("TCPSocket", 1, 100);
+ await expectMaxObjectTypeCount("TCPSocket", 1, 100);
});
diff --git a/test/js/bun/test/test-test.test.ts b/test/js/bun/test/test-test.test.ts
index b834d2152..6dc40f97c 100644
--- a/test/js/bun/test/test-test.test.ts
+++ b/test/js/bun/test/test-test.test.ts
@@ -1,11 +1,52 @@
import { spawn, spawnSync } from "bun";
import { describe, expect, it, test } from "bun:test";
-import { bunExe, bunEnv } from "harness";
import { mkdirSync, realpathSync, rmSync, writeFileSync } from "fs";
import { mkdtemp, rm, writeFile } from "fs/promises";
+import { bunEnv, bunExe } from "harness";
import { tmpdir } from "os";
import { join } from "path";
+it("shouldn't crash when async test runner callback throws", async () => {
+ const code = `
+ beforeEach(async () => {
+ await 1;
+ throw "##123##";
+ });
+
+ afterEach(async () => {
+ await 1;
+ console.error("#[Test passed successfully]");
+ });
+
+ it("current", async () => {
+ await 1;
+ throw "##456##";
+ })
+`;
+
+ const test_dir = realpathSync(await mkdtemp(join(tmpdir(), "test")));
+ try {
+ await writeFile(join(test_dir, "bad.test.js"), code);
+ const { stdout, stderr, exited } = spawn({
+ cmd: [bunExe(), "test", "bad.test.js"],
+ cwd: test_dir,
+ stdout: null,
+ stdin: "pipe",
+ stderr: "pipe",
+ env: bunEnv,
+ });
+ const err = await new Response(stderr).text();
+ expect(err).toContain("Test passed successfully");
+ expect(err).toContain("error: ##123##");
+ expect(err).toContain("error: ##456##");
+ expect(stdout).toBeDefined();
+ expect(await new Response(stdout).text()).toBe("");
+ expect(await exited).toBe(1);
+ } finally {
+ await rm(test_dir, { force: true, recursive: true });
+ }
+});
+
test("toStrictEqual() vs toEqual()", () => {
expect([1, , 3]).toEqual([1, , 3]);
expect({}).toEqual({});
@@ -176,6 +217,43 @@ test("toThrow", () => {
expect(() => {
return true;
}).not.toThrow(err);
+
+ const weirdThings = [
+ /watttt/g,
+ BigInt(123),
+ -42,
+ NaN,
+ Infinity,
+ -Infinity,
+ undefined,
+ null,
+ true,
+ false,
+ 0,
+ 1,
+ "",
+ "hello",
+ {},
+ [],
+ new Date(),
+ new Error(),
+ new RegExp("foo"),
+ new Map(),
+ new Set(),
+ Promise.resolve(),
+ Promise.reject(Symbol("123")).finally(() => {}),
+ Symbol("123"),
+ ];
+ for (const weirdThing of weirdThings) {
+ expect(() => {
+ throw weirdThing;
+ }).toThrow();
+ }
+
+ err.message = "null";
+ expect(() => {
+ throw null;
+ }).toThrow(err);
});
test("deepEquals derived strings and strings", () => {
diff --git a/test/js/bun/util/filesystem_router.test.ts b/test/js/bun/util/filesystem_router.test.ts
index b55e716c0..f5ee5c936 100644
--- a/test/js/bun/util/filesystem_router.test.ts
+++ b/test/js/bun/util/filesystem_router.test.ts
@@ -234,7 +234,7 @@ it("should support Request", async () => {
});
for (let current of [
- new Request({ url: "/posts/hello-world" }),
+ new Request({ url: "https://example.com123/posts/hello-world" }),
new Request({ url: "http://example.com/posts/hello-world" }),
]) {
const {
@@ -261,7 +261,7 @@ it("assetPrefix, src, and origin", async () => {
for (let current of [
// Reuqest
- new Request({ url: "/posts/hello-world" }),
+ new Request({ url: "http://helloooo.com/posts/hello-world" }),
new Request({ url: "https://nextjs.org/posts/hello-world" }),
]) {
const { name, src, filePath, checkThisDoesntCrash } = router.match(current);
diff --git a/test/js/node/os/os.test.js b/test/js/node/os/os.test.js
index ea685cdb7..d7cc41031 100644
--- a/test/js/node/os/os.test.js
+++ b/test/js/node/os/os.test.js
@@ -79,7 +79,7 @@ it("userInfo", () => {
if (process.platform !== "win32") {
expect(info.username).toBe(process.env.USER);
- expect(info.shell).toBe(process.env.SHELL);
+ expect(info.shell).toBe(process.env.SHELL || "unknown");
expect(info.uid >= 0).toBe(true);
expect(info.gid >= 0).toBe(true);
} else {
diff --git a/test/js/web/fetch/fetch.test.ts b/test/js/web/fetch/fetch.test.ts
index 065506fad..1f2345f85 100644
--- a/test/js/web/fetch/fetch.test.ts
+++ b/test/js/web/fetch/fetch.test.ts
@@ -718,7 +718,8 @@ describe("Blob", () => {
it(`${Constructor.name} arrayBuffer() with ${TypedArray.name}${withGC ? " with gc" : ""}`, async () => {
const data = new TypedArray(sample);
if (withGC) gc();
- const input = Constructor === Blob ? [data] : Constructor === Request ? { body: data } : data;
+ const input =
+ Constructor === Blob ? [data] : Constructor === Request ? { body: data, url: "http://example.com" } : data;
if (withGC) gc();
const blob = new Constructor(input);
if (withGC) gc();
@@ -1098,3 +1099,30 @@ it("body nullable", async () => {
expect(req.body).not.toBeNull();
}
});
+
+it("Request({}) throws", async () => {
+ expect(() => new Request({})).toThrow();
+});
+
+it("Request({toString() { throw 'wat'; } }) throws", async () => {
+ expect(
+ () =>
+ new Request({
+ toString() {
+ throw "wat";
+ },
+ }),
+ ).toThrow("wat");
+});
+
+it("should not be able to parse json from empty body", () => {
+ expect(async () => await new Response().json()).toThrow(SyntaxError);
+ expect(async () => await new Request("http://example.com/").json()).toThrow(SyntaxError);
+});
+
+it("#874", () => {
+ expect(new Request(new Request("https://example.com"), {}).url).toBe("https://example.com");
+ expect(new Request(new Request("https://example.com")).url).toBe("https://example.com");
+ // @ts-expect-error
+ expect(new Request({ url: "https://example.com" }).url).toBe("https://example.com");
+});
diff --git a/test/js/web/html/FormData.test.ts b/test/js/web/html/FormData.test.ts
index 9d0db4361..edefe8a53 100644
--- a/test/js/web/html/FormData.test.ts
+++ b/test/js/web/html/FormData.test.ts
@@ -14,14 +14,14 @@ describe("FormData", () => {
it("should be able to append a Blob", async () => {
const formData = new FormData();
formData.append("foo", new Blob(["bar"]));
- expect(await formData.get("foo").text()).toBe("bar");
+ expect(await formData.get("foo")!.text()).toBe("bar");
expect(formData.getAll("foo")[0] instanceof Blob).toBe(true);
});
it("should be able to set a Blob", async () => {
const formData = new FormData();
formData.set("foo", new Blob(["bar"]));
- expect(await formData.get("foo").text()).toBe("bar");
+ expect(await formData.get("foo")!.text()).toBe("bar");
expect(formData.getAll("foo")[0] instanceof Blob).toBe(true);
});
@@ -131,7 +131,8 @@ describe("FormData", () => {
const Class = [Response, Request] as const;
for (const C of Class) {
it(`should parse multipart/form-data (${name}) with ${C.name}`, async () => {
- const response = C === Response ? new Response(body, { headers }) : new Request({ headers, body });
+ const response =
+ C === Response ? new Response(body, { headers }) : new Request({ headers, body, url: "http://hello.com" });
const formData = await response.formData();
expect(formData instanceof FormData).toBe(true);
const entry = {};
@@ -159,7 +160,8 @@ describe("FormData", () => {
});
it(`should roundtrip multipart/form-data (${name}) with ${C.name}`, async () => {
- const response = C === Response ? new Response(body, { headers }) : new Request({ headers, body });
+ const response =
+ C === Response ? new Response(body, { headers }) : new Request({ headers, body, url: "http://hello.com" });
const formData = await response.formData();
expect(formData instanceof FormData).toBe(true);
@@ -306,7 +308,8 @@ describe("FormData", () => {
await Bun.write(path, "foo!");
const formData = new FormData();
formData.append("foo", Bun.file(path));
- const response = C === Response ? new Response(formData) : new Request({ body: formData });
+ const response =
+ C === Response ? new Response(formData) : new Request({ body: formData, url: "http://example.com" });
expect(response.headers.get("content-type")?.startsWith("multipart/form-data;")).toBe(true);
const formData2 = await response.formData();
diff --git a/test/js/web/streams/streams.test.js b/test/js/web/streams/streams.test.js
index 27b9b703f..5fc25b37c 100644
--- a/test/js/web/streams/streams.test.js
+++ b/test/js/web/streams/streams.test.js
@@ -304,7 +304,7 @@ it("new Request({body: stream}).body", async () => {
},
cancel() {},
});
- var response = new Request({ body: stream });
+ var response = new Request({ body: stream, url: "https://example.com" });
expect(response.body).toBe(stream);
expect(await response.text()).toBe("helloworld");
});