From 32cadae089d907b7cfb0d2ece9552e02d29c1881 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Tue, 9 Apr 2024 17:27:21 -0700 Subject: [PATCH] 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) => {