Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict null checks for test code #243

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-toys-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Refactor tests to pass strict-null checks
23 changes: 14 additions & 9 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
});
});
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/wrangler/src/__tests__/mock-cfetch.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -9,8 +9,8 @@ import type { FetchResult } from "../cfetch";
*/
export type MockHandler<ResponseType> = (
uri: RegExpExecArray,
init?: RequestInit,
queryParams?: URLSearchParams
init: RequestInit,
queryParams: URLSearchParams
) => ResponseType;

type RemoveMockFn = () => void;
Expand All @@ -32,7 +32,7 @@ const mocks: MockFetch<unknown>[] = [];
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;
Expand Down