From 561338dbef3b071cae9ec9dd12ed212dd223395e Mon Sep 17 00:00:00 2001 From: nick-w-nick <43578531+nick-w-nick@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:15:30 -0500 Subject: [PATCH 1/6] updated request header tests to be generated rather than hardcoded --- test/index.test.ts | 60 ++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index a0c78c9..e18fa5e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -31,41 +31,43 @@ describe("fetchNodeRequestHandler", () => { }); }); - it("headers (object)", async () => { - const res = await fetchNodeRequestHandler(echoHandler, "/test", { - headers: { foo: "bar", empty: "" }, - }); - expect(await res.json()).toEqual({ - url: "/test", - headers: { foo: "bar", host: "localhost" }, - }); - }); - - it("headers (Headers)", async () => { - const res = await fetchNodeRequestHandler(echoHandler, "/test", { - headers: new Headers({ foo: "bar", empty: "" }), - }); - expect(await res.json()).toEqual({ - url: "/test", - headers: { foo: "bar", host: "localhost" }, - }); - }); - - it("headers (array)", async () => { - const res = await fetchNodeRequestHandler(echoHandler, "/test", { - headers: [ + const requestHeaderTestCases: { + description: string; + input: Record | Headers | [string, string][]; + expected: Record; + }[] = [ + { + description: "Headers", + input: new Headers({ foo: "bar", empty: "" }), + expected: { foo: "bar", host: "localhost" }, + }, + { + description: "object", + input: { foo: "bar", empty: "" }, + expected: { foo: "bar", host: "localhost" }, + }, + { + description: "array", + input: [ ["foo", "bar"], ["empty", ""], ["array", "a"], ["array", "b"], ["array", "c"], ], - }); - expect(await res.json()).toEqual({ - url: "/test", - headers: { foo: "bar", host: "localhost", array: ["a", "b", "c"] }, - }); - }); + expected: { foo: "bar", host: "localhost", array: ["a", "b", "c"] }, + }, + ]; + + it.each(requestHeaderTestCases)( + "with request headers formatted as ($description)", + async ({ input, expected }) => { + const res = await fetchNodeRequestHandler(echoHandler, "/test", { + headers: input, + }); + expect(await res.json()).toEqual({ url: "/test", headers: expected }); + }, + ); it("error response", async () => { const res = await fetchNodeRequestHandler(() => { From ab301e1ef5a1b5a66c58f58516301bad6a394b26 Mon Sep 17 00:00:00 2001 From: nick-w-nick <43578531+nick-w-nick@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:16:30 -0500 Subject: [PATCH 2/6] removed parenthesis --- test/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.ts b/test/index.test.ts index e18fa5e..293c40e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -60,7 +60,7 @@ describe("fetchNodeRequestHandler", () => { ]; it.each(requestHeaderTestCases)( - "with request headers formatted as ($description)", + "with request headers formatted as $description", async ({ input, expected }) => { const res = await fetchNodeRequestHandler(echoHandler, "/test", { headers: input, From e8e14698e465b543b82dc03bd5b939d990ef15f7 Mon Sep 17 00:00:00 2001 From: nick-w-nick <43578531+nick-w-nick@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:19:37 -0500 Subject: [PATCH 3/6] added empty element to array test --- test/index.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/index.test.ts b/test/index.test.ts index 293c40e..6182c2c 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -53,6 +53,7 @@ describe("fetchNodeRequestHandler", () => { ["empty", ""], ["array", "a"], ["array", "b"], + ["array", ""], ["array", "c"], ], expected: { foo: "bar", host: "localhost", array: ["a", "b", "c"] }, From d56c1af93c708d2c4c9fbb4287b38d78f3921300 Mon Sep 17 00:00:00 2001 From: nick-w-nick <43578531+nick-w-nick@users.noreply.github.com> Date: Thu, 6 Feb 2025 10:31:37 -0500 Subject: [PATCH 4/6] swapped types to use internal types --- test/index.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/index.test.ts b/test/index.test.ts index 6182c2c..772736f 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -3,6 +3,7 @@ import { fetchNodeRequestHandler, callNodeRequestHandler, type NodeRequestHandler, + type NodeRequestHeaders, } from "../src"; const echoHandler: NodeRequestHandler = (req, res) => { @@ -33,7 +34,7 @@ describe("fetchNodeRequestHandler", () => { const requestHeaderTestCases: { description: string; - input: Record | Headers | [string, string][]; + input: (HeadersInit & (HeadersInit | NodeRequestHeaders)); expected: Record; }[] = [ { From 3266721bd7f2ed09989268defdb3f3e42c4c46b6 Mon Sep 17 00:00:00 2001 From: nick-w-nick <43578531+nick-w-nick@users.noreply.github.com> Date: Thu, 6 Feb 2025 10:34:33 -0500 Subject: [PATCH 5/6] lint fixes --- test/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.ts b/test/index.test.ts index 772736f..a898609 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -34,7 +34,7 @@ describe("fetchNodeRequestHandler", () => { const requestHeaderTestCases: { description: string; - input: (HeadersInit & (HeadersInit | NodeRequestHeaders)); + input: HeadersInit & (HeadersInit | NodeRequestHeaders); expected: Record; }[] = [ { From 47fabfda460e9de66fa37241edbda0e0005f95af Mon Sep 17 00:00:00 2001 From: nick-w-nick <43578531+nick-w-nick@users.noreply.github.com> Date: Thu, 6 Feb 2025 10:34:52 -0500 Subject: [PATCH 6/6] converted vitest it.each to traditional for...of loop --- test/index.test.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index a898609..23cd3b8 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -61,15 +61,16 @@ describe("fetchNodeRequestHandler", () => { }, ]; - it.each(requestHeaderTestCases)( - "with request headers formatted as $description", - async ({ input, expected }) => { + for (const testCase of requestHeaderTestCases) { + const { description, input, expected } = testCase; + + it(`with request headers formatted as ${description}`, async () => { const res = await fetchNodeRequestHandler(echoHandler, "/test", { headers: input, }); expect(await res.json()).toEqual({ url: "/test", headers: expected }); - }, - ); + }); + } it("error response", async () => { const res = await fetchNodeRequestHandler(() => {