Skip to content

Commit 32cadae

Browse files
committed
Allow null
1 parent 73ed605 commit 32cadae

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const isSerializable = (obj: Record<any, any>): boolean => {
3030
typeof val === "string" ||
3131
typeof val === "boolean" ||
3232
typeof val === "number" ||
33-
val.constructor === Date ||
33+
val?.constructor === Date ||
34+
val === null ||
3435
Array.isArray(val) ||
3536
isPlainObject(val)
3637
)

src/tests/hooks.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,27 @@ test("beforeTemplateIsBaked (propagates error that isn't serializable)", async (
126126

127127
test("beforeTemplateIsBaked (result isn't serializable)", async (t) => {
128128
type HookReturn = {
129-
type: "function" | "date"
129+
type: "function" | "date" | null
130130
}
131131

132132
const getTestServer = getTestPostgresDatabaseFactory<HookReturn>({
133133
postgresVersion: process.env.POSTGRES_VERSION,
134134
workerDedupeKey: "beforeTemplateIsBakedHookNonSerializable",
135135
beforeTemplateIsBaked: async ({ params: { type } }) => {
136+
if (type === "function") {
137+
return {
138+
foo: () => "bar",
139+
}
140+
}
141+
142+
if (type === "date") {
143+
return {
144+
foo: new Date(),
145+
}
146+
}
147+
136148
return {
137-
foo: type === "function" ? () => "bar" : new Date(),
149+
foo: null,
138150
}
139151
},
140152
})
@@ -154,6 +166,12 @@ test("beforeTemplateIsBaked (result isn't serializable)", async (t) => {
154166
type: "date",
155167
})
156168
t.true(beforeTemplateIsBakedResult.foo instanceof Date)
169+
170+
// Can return null
171+
const { beforeTemplateIsBakedResult: result } = await getTestServer(t, {
172+
type: null,
173+
})
174+
t.is(result.foo, null)
157175
})
158176

159177
test("beforeTemplateIsBaked with manual template build", async (t) => {

0 commit comments

Comments
 (0)