Skip to content

Commit

Permalink
New seralizable check too strict, should allow returning Dates from hook
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Apr 9, 2024
1 parent 42f399a commit 73ed605
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const isSerializable = (obj: Record<any, any>): boolean => {
typeof val === "string" ||
typeof val === "boolean" ||
typeof val === "number" ||
val.constructor === Date ||
Array.isArray(val) ||
isPlainObject(val)
)
Expand Down
18 changes: 14 additions & 4 deletions src/tests/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,35 @@ 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<HookReturn>({
postgresVersion: process.env.POSTGRES_VERSION,
workerDedupeKey: "beforeTemplateIsBakedHookNonSerializable",
beforeTemplateIsBaked: async () => {
beforeTemplateIsBaked: async ({ params: { type } }) => {
return {
foo: () => "bar",
foo: type === "function" ? () => "bar" : new Date(),
}
},
})

// 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) => {
Expand Down

0 comments on commit 73ed605

Please sign in to comment.