diff options
Diffstat (limited to 'test/js')
-rw-r--r-- | test/js/node/watch/fs.watch.test.ts (renamed from test/js/node/watch/fs.watch.test.js) | 86 | ||||
-rw-r--r-- | test/js/third_party/socket.io/support/util.ts | 1 | ||||
-rw-r--r-- | test/js/web/html/FormData.test.ts | 19 | ||||
-rw-r--r-- | test/js/web/html/URLSearchParams.test.ts | 5 |
4 files changed, 58 insertions, 53 deletions
diff --git a/test/js/node/watch/fs.watch.test.js b/test/js/node/watch/fs.watch.test.ts index faf6a8546..aa7959bed 100644 --- a/test/js/node/watch/fs.watch.test.js +++ b/test/js/node/watch/fs.watch.test.ts @@ -1,4 +1,4 @@ -import fs from "fs"; +import fs, { FSWatcher } from "node:fs"; import path from "path"; import { tempDirWithFiles, bunRun, bunRunAsScript } from "harness"; import { pathToFileURL } from "bun"; @@ -7,7 +7,7 @@ import { describe, expect, test } from "bun:test"; // Because macOS (and possibly other operating systems) can return a watcher // before it is actually watching, we need to repeat the operation to avoid // a race condition. -function repeat(fn) { +function repeat(fn: any) { const interval = setInterval(fn, 20); return interval; } @@ -30,7 +30,7 @@ describe("fs.watch", () => { // https://github.com/joyent/node/issues/2293 - non-persistent watcher should not block the event loop bunRun(path.join(import.meta.dir, "fixtures", "persistent.js")); done(); - } catch (e) { + } catch (e: any) { done(e); } }); @@ -39,7 +39,7 @@ describe("fs.watch", () => { try { bunRun(path.join(import.meta.dir, "fixtures", "close.js")); done(); - } catch (e) { + } catch (e: any) { done(e); } }); @@ -48,7 +48,7 @@ describe("fs.watch", () => { try { bunRun(path.join(import.meta.dir, "fixtures", "unref.js")); done(); - } catch (e) { + } catch (e: any) { done(e); } }); @@ -57,7 +57,7 @@ describe("fs.watch", () => { try { bunRunAsScript(testDir, path.join(import.meta.dir, "fixtures", "relative.js")); done(); - } catch (e) { + } catch (e: any) { done(e); } }); @@ -68,7 +68,7 @@ describe("fs.watch", () => { try { fs.mkdirSync(root); } catch {} - let err = undefined; + let err: Error | undefined = undefined; const watcher = fs.watch(root, { signal: AbortSignal.timeout(3000) }); watcher.on("change", (event, filename) => { count++; @@ -78,7 +78,7 @@ describe("fs.watch", () => { if (count >= 2) { watcher.close(); } - } catch (e) { + } catch (e: any) { err = e; watcher.close(); } @@ -106,9 +106,9 @@ describe("fs.watch", () => { const subfolder = path.join(root, "subfolder"); fs.mkdirSync(subfolder); const watcher = fs.watch(root, { recursive: true, signal: AbortSignal.timeout(3000) }); - let err = undefined; + let err: Error | undefined = undefined; watcher.on("change", (event, filename) => { - const basename = path.basename(filename); + const basename = path.basename(filename as string); if (basename === "subfolder") return; count++; @@ -118,7 +118,7 @@ describe("fs.watch", () => { if (count >= 2) { watcher.close(); } - } catch (e) { + } catch (e: any) { err = e; watcher.close(); } @@ -141,12 +141,12 @@ describe("fs.watch", () => { "deleted.txt": "hello", }); const filepath = path.join(testsubdir, "deleted.txt"); - let err = undefined; + let err: Error | undefined = undefined; const watcher = fs.watch(testsubdir, function (event, filename) { try { expect(event).toBe("rename"); expect(filename).toBe("deleted.txt"); - } catch (e) { + } catch (e: any) { err = e; } finally { clearInterval(interval); @@ -169,12 +169,12 @@ describe("fs.watch", () => { const filepath = path.join(testDir, "watch.txt"); const watcher = fs.watch(filepath); - let err = undefined; + let err: Error | undefined = undefined; watcher.on("change", function (event, filename) { try { expect(event).toBe("change"); expect(filename).toBe("watch.txt"); - } catch (e) { + } catch (e: any) { err = e; } finally { clearInterval(interval); @@ -195,7 +195,7 @@ describe("fs.watch", () => { try { fs.watch(path.join(testDir, "404.txt")); done(new Error("should not reach here")); - } catch (err) { + } catch (err: any) { expect(err).toBeInstanceOf(Error); expect(err.code).toBe("ENOENT"); expect(err.syscall).toBe("watch"); @@ -203,13 +203,13 @@ describe("fs.watch", () => { } }); - const encodings = ["utf8", "buffer", "hex", "ascii", "base64", "utf16le", "ucs2", "latin1", "binary"]; + const encodings = ["utf8", "buffer", "hex", "ascii", "base64", "utf16le", "ucs2", "latin1", "binary"] as const; test(`should work with encodings ${encodings.join(", ")}`, async () => { - const watchers = []; + const watchers: FSWatcher[] = []; const filepath = path.join(testDir, encodingFileName); - const promises = []; + const promises: Promise<any>[] = []; encodings.forEach(name => { const encoded_filename = name !== "buffer" ? Buffer.from(encodingFileName, "utf8").toString(name) : Buffer.from(encodingFileName); @@ -225,11 +225,11 @@ describe("fs.watch", () => { expect(filename).toBe(encoded_filename); } else { expect(filename).toBeInstanceOf(Buffer); - expect(filename.toString("utf8")).toBe(encodingFileName); + expect((filename as any as Buffer)!.toString("utf8")).toBe(encodingFileName); } - resolve(); - } catch (e) { + resolve(undefined); + } catch (e: any) { reject(e); } }), @@ -254,12 +254,12 @@ describe("fs.watch", () => { const filepath = path.join(testDir, "url.txt"); try { const watcher = fs.watch(pathToFileURL(filepath)); - let err = undefined; + let err: Error | undefined = undefined; watcher.on("change", function (event, filename) { try { expect(event).toBe("change"); expect(filename).toBe("url.txt"); - } catch (e) { + } catch (e: any) { err = e; } finally { clearInterval(interval); @@ -274,7 +274,7 @@ describe("fs.watch", () => { const interval = repeat(() => { fs.writeFileSync(filepath, "world"); }); - } catch (e) { + } catch (e: any) { done(e); } }); @@ -288,12 +288,12 @@ describe("fs.watch", () => { try { watcher.close(); done(); - } catch (e) { + } catch (e: any) { done("Should not error when calling close from error event"); } }); ac.abort(); - } catch (e) { + } catch (e: any) { done(e); } }); @@ -308,13 +308,13 @@ describe("fs.watch", () => { try { watcher.close(); done(); - } catch (e) { + } catch (e: any) { done("Should not error when calling close from close event"); } }); ac.abort(); - } catch (e) { + } catch (e: any) { done(e); } }); @@ -325,7 +325,7 @@ describe("fs.watch", () => { const ac = new AbortController(); const promise = new Promise((resolve, reject) => { const watcher = fs.watch(filepath, { signal: ac.signal }); - watcher.once("error", err => (err.message === "The operation was aborted." ? resolve() : reject(err))); + watcher.once("error", err => (err.message === "The operation was aborted." ? resolve(undefined) : reject(err))); watcher.once("close", () => reject()); }); await Bun.sleep(10); @@ -339,7 +339,7 @@ describe("fs.watch", () => { const signal = AbortSignal.abort(); await new Promise((resolve, reject) => { const watcher = fs.watch(filepath, { signal }); - watcher.once("error", err => (err.message === "The operation was aborted." ? resolve() : reject(err))); + watcher.once("error", err => (err.message === "The operation was aborted." ? resolve(undefined) : reject(err))); watcher.once("close", () => reject()); }); }); @@ -353,13 +353,13 @@ describe("fs.watch", () => { }); const promise = new Promise((resolve, reject) => { - let timeout = null; + let timeout: any = null; const watcher = fs.watch(filepath, event => { clearTimeout(timeout); clearInterval(interval); try { resolve(event); - } catch (e) { + } catch (e: any) { reject(e); } finally { watcher.close(); @@ -383,7 +383,7 @@ describe("fs.promises.watch", () => { fs.mkdirSync(root); } catch {} let success = false; - let err = undefined; + let err: Error | undefined = undefined; try { const ac = new AbortController(); const watcher = fs.promises.watch(root, { signal: ac.signal }); @@ -405,13 +405,13 @@ describe("fs.promises.watch", () => { clearInterval(interval); ac.abort(); } - } catch (e) { + } catch (e: any) { err = e; clearInterval(interval); ac.abort(); } } - } catch (e) { + } catch (e: any) { if (!success) { throw err || e; } @@ -427,7 +427,7 @@ describe("fs.promises.watch", () => { const subfolder = path.join(root, "subfolder"); fs.mkdirSync(subfolder); let success = false; - let err = undefined; + let err: Error | undefined = undefined; try { const ac = new AbortController(); @@ -439,7 +439,7 @@ describe("fs.promises.watch", () => { fs.rmdirSync(path.join(subfolder, "new-folder.txt")); }); for await (const event of watcher) { - const basename = path.basename(event.filename); + const basename = path.basename(event.filename!); if (basename === "subfolder") continue; count++; @@ -452,13 +452,13 @@ describe("fs.promises.watch", () => { clearInterval(interval); ac.abort(); } - } catch (e) { + } catch (e: any) { err = e; clearInterval(interval); ac.abort(); } } - } catch (e) { + } catch (e: any) { if (!success) { throw err || e; } @@ -474,7 +474,7 @@ describe("fs.promises.watch", () => { const promise = (async () => { try { for await (const _ of watcher); - } catch (e) { + } catch (e: any) { expect(e.message).toBe("The operation was aborted."); } })(); @@ -491,7 +491,7 @@ describe("fs.promises.watch", () => { await (async () => { try { for await (const _ of watcher); - } catch (e) { + } catch (e: any) { expect(e.message).toBe("The operation was aborted."); } })(); @@ -511,7 +511,7 @@ describe("fs.promises.watch", () => { for await (const event of watcher) { return event.eventType; } - } catch (e) { + } catch (e: any) { expect("unreacheable").toBe(false); } finally { clearInterval(interval); diff --git a/test/js/third_party/socket.io/support/util.ts b/test/js/third_party/socket.io/support/util.ts index 597b40d65..b5f515568 100644 --- a/test/js/third_party/socket.io/support/util.ts +++ b/test/js/third_party/socket.io/support/util.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import type { Server } from "socket.io"; import request from "supertest"; diff --git a/test/js/web/html/FormData.test.ts b/test/js/web/html/FormData.test.ts index cbaf5aaa7..45b4f2f5a 100644 --- a/test/js/web/html/FormData.test.ts +++ b/test/js/web/html/FormData.test.ts @@ -301,17 +301,18 @@ describe("FormData", () => { expect(await (body.get("foo") as Blob).text()).toBe("baz"); server.stop(true); }); - + type FetchReqArgs = [request: Request, init?: RequestInit]; + type FetchURLArgs = [url: string | URL | Request, init?: FetchRequestInit]; for (let useRequestConstructor of [true, false]) { describe(useRequestConstructor ? "Request constructor" : "fetch()", () => { - function send(args: Parameters<typeof fetch>) { + function send(args: FetchReqArgs | FetchURLArgs) { if (useRequestConstructor) { - return fetch(new Request(...args)); + return fetch(new Request(...(args as FetchReqArgs))); } else { - return fetch(...args); + return fetch(...(args as FetchURLArgs)); } } - for (let headers of [{}, undefined, { headers: { X: "Y" } }]) { + for (let headers of [{} as {}, undefined, { headers: { X: "Y" } }]) { describe("headers: " + Bun.inspect(headers).replaceAll(/([\n ])/gim, ""), () => { it("send on HTTP server with FormData & Blob (roundtrip)", async () => { let contentType = ""; @@ -330,11 +331,10 @@ describe("FormData", () => { form.append("bar", "baz"); // @ts-ignore - const reqBody = [ + const reqBody: FetchURLArgs = [ `http://${server.hostname}:${server.port}`, { body: form, - headers, method: "POST", }, @@ -364,7 +364,6 @@ describe("FormData", () => { form.append("foo", file); form.append("bar", "baz"); - // @ts-ignore const reqBody = [ `http://${server.hostname}:${server.port}`, { @@ -374,7 +373,7 @@ describe("FormData", () => { method: "POST", }, ]; - const res = await send(reqBody); + const res = await send(reqBody as FetchURLArgs); const body = await res.formData(); expect(await (body.get("foo") as Blob).text()).toBe(text); expect(contentType).toContain("multipart/form-data"); @@ -410,7 +409,7 @@ describe("FormData", () => { method: "POST", }, ]; - const res = await send(reqBody); + const res = await send(reqBody as FetchURLArgs); const body = await res.formData(); expect(contentType).toContain("multipart/form-data"); expect(body.get("foo")).toBe("boop"); diff --git a/test/js/web/html/URLSearchParams.test.ts b/test/js/web/html/URLSearchParams.test.ts index 120bb2321..41c42c25d 100644 --- a/test/js/web/html/URLSearchParams.test.ts +++ b/test/js/web/html/URLSearchParams.test.ts @@ -7,15 +7,20 @@ describe("URLSearchParams", () => { params.append("foo", "bar"); params.append("foo", "boop"); params.append("bar", "baz"); + // @ts-ignore expect(params.length).toBe(3); params.delete("foo"); + // @ts-ignore expect(params.length).toBe(1); params.append("foo", "bar"); + // @ts-ignore expect(params.length).toBe(2); params.delete("foo"); params.delete("foo"); + // @ts-ignore expect(params.length).toBe(1); params.delete("bar"); + // @ts-ignore expect(params.length).toBe(0); }); |