diff --git a/src/index.ts b/src/index.ts index 6861bb4..1109ca6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ const isSerializable = (obj: Record): boolean => { typeof val === "string" || typeof val === "boolean" || typeof val === "number" || + val.constructor === Date || Array.isArray(val) || isPlainObject(val) ) diff --git a/src/tests/hooks.test.ts b/src/tests/hooks.test.ts index 2340170..ff2101b 100644 --- a/src/tests/hooks.test.ts +++ b/src/tests/hooks.test.ts @@ -125,12 +125,16 @@ 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({ postgresVersion: process.env.POSTGRES_VERSION, workerDedupeKey: "beforeTemplateIsBakedHookNonSerializable", - beforeTemplateIsBaked: async () => { + beforeTemplateIsBaked: async ({ params: { type } }) => { return { - foo: () => "bar", + foo: type === "function" ? () => "bar" : new Date(), } }, }) @@ -138,12 +142,18 @@ test("beforeTemplateIsBaked (result isn't serializable)", async (t) => { // 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) => {