From 73ed605325e338b7bc7ca1415ec7f48bc89aa248 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Tue, 9 Apr 2024 15:43:52 -0700 Subject: [PATCH 1/2] New seralizable check too strict, should allow returning Dates from hook --- src/index.ts | 1 + src/tests/hooks.test.ts | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6861bb4..1109ca6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ const isSerializable = (obj: Record): boolean => { typeof val === "string" || typeof val === "boolean" || typeof val === "number" || + val.constructor === Date || Array.isArray(val) || isPlainObject(val) ) diff --git a/src/tests/hooks.test.ts b/src/tests/hooks.test.ts index 2340170..ff2101b 100644 --- a/src/tests/hooks.test.ts +++ b/src/tests/hooks.test.ts @@ -125,12 +125,16 @@ test("beforeTemplateIsBaked (propagates error that isn't serializable)", async ( }) test("beforeTemplateIsBaked (result isn't serializable)", async (t) => { - const getTestServer = getTestPostgresDatabaseFactory({ + type HookReturn = { + type: "function" | "date" + } + + const getTestServer = getTestPostgresDatabaseFactory({ postgresVersion: process.env.POSTGRES_VERSION, workerDedupeKey: "beforeTemplateIsBakedHookNonSerializable", - beforeTemplateIsBaked: async () => { + beforeTemplateIsBaked: async ({ params: { type } }) => { return { - foo: () => "bar", + foo: type === "function" ? () => "bar" : new Date(), } }, }) @@ -138,12 +142,18 @@ test("beforeTemplateIsBaked (result isn't serializable)", async (t) => { // Should throw error with clear message await t.throwsAsync( async () => { - await getTestServer(t) + await getTestServer(t, { type: "function" }) }, { message: /could not be serialized/, } ) + + // Can return a date + const { beforeTemplateIsBakedResult } = await getTestServer(t, { + type: "date", + }) + t.true(beforeTemplateIsBakedResult.foo instanceof Date) }) test("beforeTemplateIsBaked with manual template build", async (t) => { From 32cadae089d907b7cfb0d2ece9552e02d29c1881 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Tue, 9 Apr 2024 17:27:21 -0700 Subject: [PATCH 2/2] Allow null --- src/index.ts | 3 ++- src/tests/hooks.test.ts | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1109ca6..e3a1759 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,7 +30,8 @@ const isSerializable = (obj: Record): boolean => { typeof val === "string" || typeof val === "boolean" || typeof val === "number" || - val.constructor === Date || + val?.constructor === Date || + val === null || Array.isArray(val) || isPlainObject(val) ) diff --git a/src/tests/hooks.test.ts b/src/tests/hooks.test.ts index ff2101b..735c9ea 100644 --- a/src/tests/hooks.test.ts +++ b/src/tests/hooks.test.ts @@ -126,15 +126,27 @@ test("beforeTemplateIsBaked (propagates error that isn't serializable)", async ( test("beforeTemplateIsBaked (result isn't serializable)", async (t) => { type HookReturn = { - type: "function" | "date" + type: "function" | "date" | null } const getTestServer = getTestPostgresDatabaseFactory({ postgresVersion: process.env.POSTGRES_VERSION, workerDedupeKey: "beforeTemplateIsBakedHookNonSerializable", beforeTemplateIsBaked: async ({ params: { type } }) => { + if (type === "function") { + return { + foo: () => "bar", + } + } + + if (type === "date") { + return { + foo: new Date(), + } + } + return { - foo: type === "function" ? () => "bar" : new Date(), + foo: null, } }, }) @@ -154,6 +166,12 @@ test("beforeTemplateIsBaked (result isn't serializable)", async (t) => { type: "date", }) t.true(beforeTemplateIsBakedResult.foo instanceof Date) + + // Can return null + const { beforeTemplateIsBakedResult: result } = await getTestServer(t, { + type: null, + }) + t.is(result.foo, null) }) test("beforeTemplateIsBaked with manual template build", async (t) => {