Skip to content

Commit

Permalink
Refactor tests to pass strict-null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Jan 18, 2022
1 parent 074ef42 commit dc7ce83
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
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

0 comments on commit dc7ce83

Please sign in to comment.