diff --git a/.changeset/angry-toys-shave.md b/.changeset/angry-toys-shave.md new file mode 100644 index 000000000000..1681c17d5713 --- /dev/null +++ b/.changeset/angry-toys-shave.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +Refactor tests to pass strict-null checks diff --git a/packages/wrangler/src/__tests__/kv.test.ts b/packages/wrangler/src/__tests__/kv.test.ts index ea55f744a69b..56a8d5493e69 100644 --- a/packages/wrangler/src/__tests__/kv.test.ts +++ b/packages/wrangler/src/__tests__/kv.test.ts @@ -8,6 +8,11 @@ import { import { runWrangler } from "./run-wrangler"; import { runInTempDir } from "./run-in-tmp"; +interface KVNamespaceInfo { + title: string; + id: string; +} + describe("wrangler", () => { runInTempDir(); @@ -196,30 +201,30 @@ describe("wrangler", () => { } it("should list namespaces", async () => { - const KVNamespaces = [ + const kvNamespaces: KVNamespaceInfo[] = [ { title: "title-1", id: "id-1" }, { title: "title-2", id: "id-2" }, ]; - mockListRequest(KVNamespaces); + mockListRequest(kvNamespaces); const { error, stdout, stderr } = await runWrangler( "kv:namespace list" ); expect(error).toMatchInlineSnapshot(`undefined`); expect(stderr).toMatchInlineSnapshot(`""`); const namespaces = JSON.parse(stdout); - expect(namespaces).toEqual(KVNamespaces); + expect(namespaces).toEqual(kvNamespaces); }); it("should make multiple requests for paginated results", async () => { // Create a lot of mock namespaces, so that the fetch requests will be paginated - const KVNamespaces = []; + const kvNamespaces: KVNamespaceInfo[] = []; for (let i = 0; i < 550; i++) { - KVNamespaces.push({ title: "title-" + i, id: "id-" + i }); + kvNamespaces.push({ title: "title-" + i, id: "id-" + i }); } - const requests = mockListRequest(KVNamespaces); + const requests = mockListRequest(kvNamespaces); const { stdout } = await runWrangler("kv:namespace list"); const namespaces = JSON.parse(stdout); - expect(namespaces).toEqual(KVNamespaces); + expect(namespaces).toEqual(kvNamespaces); expect(requests.count).toEqual(6); }); }); @@ -689,7 +694,7 @@ describe("wrangler", () => { if (expectedKeys.length <= keysPerRequest) { return createFetchResult(expectedKeys); } else { - const start = parseInt(query.get("cursor")) || 0; + const start = parseInt(query.get("cursor") ?? "0") || 0; const end = start + keysPerRequest; const cursor = end < expectedKeys.length ? end : undefined; return createFetchResult( @@ -786,7 +791,7 @@ describe("wrangler", () => { it("should make multiple requests for paginated results", async () => { // Create a lot of mock keys, so that the fetch requests will be paginated - const keys = []; + const keys: string[] = []; for (let i = 0; i < 550; i++) { keys.push("key-" + i); } diff --git a/packages/wrangler/src/__tests__/mock-cfetch.ts b/packages/wrangler/src/__tests__/mock-cfetch.ts index f0ca6c035743..977308070313 100644 --- a/packages/wrangler/src/__tests__/mock-cfetch.ts +++ b/packages/wrangler/src/__tests__/mock-cfetch.ts @@ -1,5 +1,5 @@ import type { RequestInit } from "node-fetch"; -import type { URLSearchParams } from "node:url"; +import { URLSearchParams } from "node:url"; import { pathToRegexp } from "path-to-regexp"; import { CF_API_BASE_URL } from "../cfetch"; import type { FetchResult } from "../cfetch"; @@ -9,8 +9,8 @@ import type { FetchResult } from "../cfetch"; */ export type MockHandler = ( uri: RegExpExecArray, - init?: RequestInit, - queryParams?: URLSearchParams + init: RequestInit, + queryParams: URLSearchParams ) => ResponseType; type RemoveMockFn = () => void; @@ -32,7 +32,7 @@ const mocks: MockFetch[] = []; export async function mockFetchInternal( resource: string, init: RequestInit = {}, - queryParams?: URLSearchParams + queryParams: URLSearchParams = new URLSearchParams() ) { for (const { regexp, method, handler } of mocks) { const resourcePath = new URL(resource, CF_API_BASE_URL).pathname;