Skip to content

Commit dc7ce83

Browse files
Refactor tests to pass strict-null checks
1 parent 074ef42 commit dc7ce83

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

.changeset/angry-toys-shave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Refactor tests to pass strict-null checks

packages/wrangler/src/__tests__/kv.test.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
import { runWrangler } from "./run-wrangler";
99
import { runInTempDir } from "./run-in-tmp";
1010

11+
interface KVNamespaceInfo {
12+
title: string;
13+
id: string;
14+
}
15+
1116
describe("wrangler", () => {
1217
runInTempDir();
1318

@@ -196,30 +201,30 @@ describe("wrangler", () => {
196201
}
197202

198203
it("should list namespaces", async () => {
199-
const KVNamespaces = [
204+
const kvNamespaces: KVNamespaceInfo[] = [
200205
{ title: "title-1", id: "id-1" },
201206
{ title: "title-2", id: "id-2" },
202207
];
203-
mockListRequest(KVNamespaces);
208+
mockListRequest(kvNamespaces);
204209
const { error, stdout, stderr } = await runWrangler(
205210
"kv:namespace list"
206211
);
207212
expect(error).toMatchInlineSnapshot(`undefined`);
208213
expect(stderr).toMatchInlineSnapshot(`""`);
209214
const namespaces = JSON.parse(stdout);
210-
expect(namespaces).toEqual(KVNamespaces);
215+
expect(namespaces).toEqual(kvNamespaces);
211216
});
212217

213218
it("should make multiple requests for paginated results", async () => {
214219
// Create a lot of mock namespaces, so that the fetch requests will be paginated
215-
const KVNamespaces = [];
220+
const kvNamespaces: KVNamespaceInfo[] = [];
216221
for (let i = 0; i < 550; i++) {
217-
KVNamespaces.push({ title: "title-" + i, id: "id-" + i });
222+
kvNamespaces.push({ title: "title-" + i, id: "id-" + i });
218223
}
219-
const requests = mockListRequest(KVNamespaces);
224+
const requests = mockListRequest(kvNamespaces);
220225
const { stdout } = await runWrangler("kv:namespace list");
221226
const namespaces = JSON.parse(stdout);
222-
expect(namespaces).toEqual(KVNamespaces);
227+
expect(namespaces).toEqual(kvNamespaces);
223228
expect(requests.count).toEqual(6);
224229
});
225230
});
@@ -689,7 +694,7 @@ describe("wrangler", () => {
689694
if (expectedKeys.length <= keysPerRequest) {
690695
return createFetchResult(expectedKeys);
691696
} else {
692-
const start = parseInt(query.get("cursor")) || 0;
697+
const start = parseInt(query.get("cursor") ?? "0") || 0;
693698
const end = start + keysPerRequest;
694699
const cursor = end < expectedKeys.length ? end : undefined;
695700
return createFetchResult(
@@ -786,7 +791,7 @@ describe("wrangler", () => {
786791

787792
it("should make multiple requests for paginated results", async () => {
788793
// Create a lot of mock keys, so that the fetch requests will be paginated
789-
const keys = [];
794+
const keys: string[] = [];
790795
for (let i = 0; i < 550; i++) {
791796
keys.push("key-" + i);
792797
}

packages/wrangler/src/__tests__/mock-cfetch.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RequestInit } from "node-fetch";
2-
import type { URLSearchParams } from "node:url";
2+
import { URLSearchParams } from "node:url";
33
import { pathToRegexp } from "path-to-regexp";
44
import { CF_API_BASE_URL } from "../cfetch";
55
import type { FetchResult } from "../cfetch";
@@ -9,8 +9,8 @@ import type { FetchResult } from "../cfetch";
99
*/
1010
export type MockHandler<ResponseType> = (
1111
uri: RegExpExecArray,
12-
init?: RequestInit,
13-
queryParams?: URLSearchParams
12+
init: RequestInit,
13+
queryParams: URLSearchParams
1414
) => ResponseType;
1515

1616
type RemoveMockFn = () => void;
@@ -32,7 +32,7 @@ const mocks: MockFetch<unknown>[] = [];
3232
export async function mockFetchInternal(
3333
resource: string,
3434
init: RequestInit = {},
35-
queryParams?: URLSearchParams
35+
queryParams: URLSearchParams = new URLSearchParams()
3636
) {
3737
for (const { regexp, method, handler } of mocks) {
3838
const resourcePath = new URL(resource, CF_API_BASE_URL).pathname;

0 commit comments

Comments
 (0)