diff options
Diffstat (limited to 'test/js/web')
-rw-r--r-- | test/js/web/crypto/web-crypto.test.ts | 6 | ||||
-rw-r--r-- | test/js/web/fetch/body.test.ts | 2 | ||||
-rw-r--r-- | test/js/web/fetch/fetch-gzip.test.ts | 17 | ||||
-rw-r--r-- | test/js/web/fetch/fetch.test.ts | 35 | ||||
-rw-r--r-- | test/js/web/html/FormData.test.ts | 22 |
5 files changed, 44 insertions, 38 deletions
diff --git a/test/js/web/crypto/web-crypto.test.ts b/test/js/web/crypto/web-crypto.test.ts index 250282b96..b8155c3ba 100644 --- a/test/js/web/crypto/web-crypto.test.ts +++ b/test/js/web/crypto/web-crypto.test.ts @@ -37,7 +37,7 @@ describe("Web Crypto", () => { }); it("should verify and sign", async () => { - async function importKey(secret) { + async function importKey(secret: string) { return await crypto.subtle.importKey( "raw", new TextEncoder().encode(secret), @@ -47,7 +47,7 @@ describe("Web Crypto", () => { ); } - async function signResponse(message, secret) { + async function signResponse(message: string, secret: string) { const key = await importKey(secret); const signature = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(message)); @@ -55,7 +55,7 @@ describe("Web Crypto", () => { return btoa(String.fromCharCode(...new Uint8Array(signature))); } - async function verifySignature(message, signature, secret) { + async function verifySignature(message: string, signature: string, secret: string) { const key = await importKey(secret); // Convert Base64 to Uint8Array diff --git a/test/js/web/fetch/body.test.ts b/test/js/web/fetch/body.test.ts index fa3d4e53b..2b9b3e11d 100644 --- a/test/js/web/fetch/body.test.ts +++ b/test/js/web/fetch/body.test.ts @@ -245,11 +245,9 @@ for (const { body, fn } of bodyTypes) { }); test(body.name, async () => { for (const { string, buffer } of utf8) { - // @ts-expect-error expect(() => { fn(buffer); }).not.toThrow(); - // @ts-expect-error expect(await fn(buffer).text()).toBe(string); } }); diff --git a/test/js/web/fetch/fetch-gzip.test.ts b/test/js/web/fetch/fetch-gzip.test.ts index 91ea8de9f..076b5845b 100644 --- a/test/js/web/fetch/fetch-gzip.test.ts +++ b/test/js/web/fetch/fetch-gzip.test.ts @@ -1,7 +1,6 @@ -import { concatArrayBuffers } from "bun"; -import { it, describe, expect } from "bun:test"; -import fs from "fs"; -import { gc, gcTick } from "harness"; +import { concatArrayBuffers, Socket, TCPSocketListener } from "bun"; +import { it, expect } from "bun:test"; +import { gcTick } from "harness"; it("fetch() with a buffered gzip response works (one chunk)", async () => { var server = Bun.serve({ @@ -122,9 +121,15 @@ it("fetch() with a gzip response works (one chunk, streamed, with a delay", asyn server.stop(); }); +const arg = Bun.listen({ + hostname: "asdf", + port: 1234, + socket: {}, +}); + it("fetch() with a gzip response works (multiple chunks, TCP server", async done => { const compressed = await Bun.file(import.meta.dir + "/fixture.html.gz").arrayBuffer(); - var socketToClose; + var socketToClose!: Socket; const server = Bun.listen({ port: 0, hostname: "0.0.0.0", @@ -134,7 +139,7 @@ it("fetch() with a gzip response works (multiple chunks, TCP server", async done var corked: any[] = []; var cork = true; - async function write(chunk) { + async function write(chunk: any) { await new Promise<void>((resolve, reject) => { if (cork) { corked.push(chunk); diff --git a/test/js/web/fetch/fetch.test.ts b/test/js/web/fetch/fetch.test.ts index 1f2345f85..f723761a7 100644 --- a/test/js/web/fetch/fetch.test.ts +++ b/test/js/web/fetch/fetch.test.ts @@ -1,4 +1,4 @@ -import { serve, sleep } from "bun"; +import { AnyFunction, serve, ServeOptions, Server, sleep } from "bun"; import { afterAll, afterEach, beforeAll, describe, expect, it, beforeEach } from "bun:test"; import { chmodSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "fs"; import { mkfifo } from "mkfifo"; @@ -10,8 +10,8 @@ const tmp_dir = mkdtempSync(join(realpathSync(tmpdir()), "fetch.test")); const fixture = readFileSync(join(import.meta.dir, "fetch.js.txt"), "utf8"); -let server; -function startServer({ fetch, ...options }) { +let server: Server; +function startServer({ fetch, ...options }: ServeOptions) { server = serve({ ...options, fetch, @@ -38,7 +38,7 @@ describe("AbortSignal", () => { return new Response("Hello"); } if (request.url.endsWith("/stream")) { - const reader = request.body.getReader(); + const reader = request.body!.getReader(); const body = new ReadableStream({ async pull(controller) { if (!reader) controller.close(); @@ -113,11 +113,11 @@ describe("AbortSignal", () => { const controller = new AbortController(); const signal = controller.signal; signal.addEventListener("abort", ev => { - const target = ev.currentTarget; + const target = ev.currentTarget!; expect(target).toBeDefined(); expect(target.aborted).toBe(true); expect(target.reason).toBeDefined(); - expect(target.reason.name).toBe("AbortError"); + expect(target.reason!.name).toBe("AbortError"); }); expect(async () => { @@ -280,11 +280,11 @@ describe("fetch", () => { "http://example.com", new URL("https://example.com"), new Request({ url: "https://example.com" }), - { toString: () => "https://example.com" }, + { toString: () => "https://example.com" } as string, ]; for (let url of urls) { gc(); - let name; + let name: string; if (url instanceof URL) { name = "URL: " + url; } else if (url instanceof Request) { @@ -292,7 +292,7 @@ describe("fetch", () => { } else if (url.hasOwnProperty("toString")) { name = "Object: " + url.toString(); } else { - name = url; + name = url as string; } it(name, async () => { gc(); @@ -347,7 +347,7 @@ describe("fetch", () => { fetch(req) { return new Response(req.body); }, - host: "localhost", + hostname: "localhost", }); // POST with body @@ -388,7 +388,7 @@ it("website with tlsextname", async () => { await fetch("https://bun.sh", { method: "HEAD" }); }); -function testBlobInterface(blobbyConstructor, hasBlobFn?) { +function testBlobInterface(blobbyConstructor: { (..._: any[]): any }, hasBlobFn?: boolean) { for (let withGC of [false, true]) { for (let jsonObject of [ { hello: true }, @@ -534,7 +534,7 @@ describe("Bun.file", () => { let count = 0; testBlobInterface(data => { const blob = new Blob([data]); - const buffer = Bun.peek(blob.arrayBuffer()); + const buffer = Bun.peek(blob.arrayBuffer()) as ArrayBuffer; const path = join(tmp_dir, `tmp-${count++}.bytes`); writeFileSync(path, buffer); const file = Bun.file(path); @@ -549,8 +549,8 @@ describe("Bun.file", () => { expect(size).toBe(Infinity); }); - function forEachMethod(fn, skip?) { - const method = ["arrayBuffer", "text", "json"]; + const method = ["arrayBuffer", "text", "json"] as const; + function forEachMethod(fn: (m: (typeof method)[number]) => any, skip?: AnyFunction) { for (const m of method) { (skip ? it.skip : it)(m, fn(m)); } @@ -643,7 +643,7 @@ describe("Blob", () => { "π π π π π π
π π€£ π₯² βΊοΈ π π π π π π π π₯° π π π π π π π π π€ͺ π€¨ π§ π€ π π₯Έ π€© π₯³", ), ], - ]; + ] as any[]; var expected = [ "123456", @@ -721,7 +721,7 @@ describe("Blob", () => { const input = Constructor === Blob ? [data] : Constructor === Request ? { body: data, url: "http://example.com" } : data; if (withGC) gc(); - const blob = new Constructor(input); + const blob = new Constructor(input as any); if (withGC) gc(); const out = await blob.arrayBuffer(); if (withGC) gc(); @@ -1101,12 +1101,14 @@ it("body nullable", async () => { }); it("Request({}) throws", async () => { + // @ts-expect-error expect(() => new Request({})).toThrow(); }); it("Request({toString() { throw 'wat'; } }) throws", async () => { expect( () => + // @ts-expect-error new Request({ toString() { throw "wat"; @@ -1123,6 +1125,5 @@ it("should not be able to parse json from empty body", () => { 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 edefe8a53..af2871b10 100644 --- a/test/js/web/html/FormData.test.ts +++ b/test/js/web/html/FormData.test.ts @@ -1,7 +1,9 @@ import { afterAll, beforeAll, describe, expect, it, test } from "bun:test"; import fs, { chmodSync, unlinkSync } from "fs"; +import { gc, withoutAggressiveGC } from "harness"; import { mkfifo } from "mkfifo"; -import { gc, withoutAggressiveGC } from "../../gc"; + +gc; describe("FormData", () => { it("should be able to append a string", () => { @@ -14,14 +16,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 ((await formData.get("foo")) as Blob)!.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 ((await formData.get("foo")) as Blob).text()).toBe("bar"); expect(formData.getAll("foo")[0] instanceof Blob).toBe(true); }); @@ -135,8 +137,8 @@ describe("FormData", () => { 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 = {}; - const expected = Object.assign({}, expected_); + const entry: { [k: string]: any } = {}; + const expected: { [k: string]: any } = Object.assign({}, expected_); for (const key of formData.keys()) { const values = formData.getAll(key); @@ -180,7 +182,7 @@ describe("FormData", () => { const b = bValues[i]; if (a instanceof Blob) { expect(b instanceof Blob).toBe(true); - expect(await a.text()).toBe(await b.text()); + expect(await a.text()).toBe(await (b as Blob).text()); } else { expect(a).toBe(b); } @@ -200,7 +202,7 @@ describe("FormData", () => { const b = bValues[i]; if (c instanceof Blob) { expect(b instanceof Blob).toBe(true); - expect(await c.text()).toBe(await b.text()); + expect(await c.text()).toBe(await (b as Blob).text()); } else { expect(c).toBe(b); } @@ -219,7 +221,7 @@ describe("FormData", () => { try { await response.formData(); throw "should have thrown"; - } catch (e) { + } catch (e: any) { expect(typeof e.message).toBe("string"); } }); @@ -233,7 +235,7 @@ describe("FormData", () => { try { await response.formData(); throw "should have thrown"; - } catch (e) { + } catch (e: any) { expect(typeof e.message).toBe("string"); } }); @@ -247,7 +249,7 @@ describe("FormData", () => { try { await response.formData(); throw "should have thrown"; - } catch (e) { + } catch (e: any) { expect(typeof e.message).toBe("string"); } }); |