aboutsummaryrefslogtreecommitdiff
path: root/test/js/web/fetch/fetch.test.ts
diff options
context:
space:
mode:
authorGravatar Colin McDonnell <colinmcd94@gmail.com> 2023-03-22 15:01:01 -0700
committerGravatar GitHub <noreply@github.com> 2023-03-22 15:01:01 -0700
commita5f92224b586289fc72f0abdb68b08eef9f017db (patch)
tree6092397858776820b431b0dffa635d8bc3b3185e /test/js/web/fetch/fetch.test.ts
parent2bdaa81b1c2325687c5115b4e97627533cb3646b (diff)
downloadbun-a5f92224b586289fc72f0abdb68b08eef9f017db.tar.gz
bun-a5f92224b586289fc72f0abdb68b08eef9f017db.tar.zst
bun-a5f92224b586289fc72f0abdb68b08eef9f017db.zip
Fix types (#2453)
* WIP * WIP * WIP * WIP * Improve typechecking in type files * Fix typechecking * Update * Update submodule * CI for typechecking * Add ci * Update commands * Format after build * Dont use bunx * Rename job * Use nodemodules prettier * Update workflow * Use symlink * Debug * Debug * Clean up and rename jobs
Diffstat (limited to 'test/js/web/fetch/fetch.test.ts')
-rw-r--r--test/js/web/fetch/fetch.test.ts35
1 files changed, 18 insertions, 17 deletions
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");
});