Skip to content

Commit

Permalink
fix: Merge pull request #31 from seamapi/fix-allow-returning-dates
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb authored Apr 10, 2024
2 parents 42f399a + 32cadae commit b4d5dca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const isSerializable = (obj: Record<any, any>): boolean => {
typeof val === "string" ||
typeof val === "boolean" ||
typeof val === "number" ||
val?.constructor === Date ||
val === null ||
Array.isArray(val) ||
isPlainObject(val)
)
Expand Down
36 changes: 32 additions & 4 deletions src/tests/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,53 @@ 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" | null
}

const getTestServer = getTestPostgresDatabaseFactory<HookReturn>({
postgresVersion: process.env.POSTGRES_VERSION,
workerDedupeKey: "beforeTemplateIsBakedHookNonSerializable",
beforeTemplateIsBaked: async () => {
beforeTemplateIsBaked: async ({ params: { type } }) => {
if (type === "function") {
return {
foo: () => "bar",
}
}

if (type === "date") {
return {
foo: new Date(),
}
}

return {
foo: () => "bar",
foo: null,
}
},
})

// 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)

// 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 b4d5dca

Please sign in to comment.