Skip to content

Commit

Permalink
Allow null
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Apr 10, 2024
1 parent 73ed605 commit 32cadae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const isSerializable = (obj: Record<any, any>): boolean => {
typeof val === "string" ||
typeof val === "boolean" ||
typeof val === "number" ||
val.constructor === Date ||
val?.constructor === Date ||
val === null ||
Array.isArray(val) ||
isPlainObject(val)
)
Expand Down
22 changes: 20 additions & 2 deletions src/tests/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HookReturn>({
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,
}
},
})
Expand All @@ -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) => {
Expand Down

0 comments on commit 32cadae

Please sign in to comment.