|
import { describe, it, expect } from "vitest"; |
|
import { Temporal } from "temporal-polyfill"; |
|
import { z } from "zod"; |
|
import { zInstant } from "temporal-zod"; |
|
|
|
const EventSchema = z.object({ |
|
name: z.string(), |
|
timestamp: zInstant, |
|
}); |
|
|
|
type Event = z.infer<typeof EventSchema>; |
|
|
|
function createEventWithInstant(): Event { |
|
return EventSchema.parse({ |
|
name: "System Boot", |
|
timestamp: Temporal.Now.instant(), |
|
}); |
|
} |
|
|
|
function createEventWithISOString(): Event { |
|
return EventSchema.parse({ |
|
name: "User Login", |
|
timestamp: "2025-10-24T15:30:00Z", |
|
}); |
|
} |
|
|
|
describe("Event creation with Temporal.Instant", () => { |
|
it("should create event with Temporal.Instant", () => { |
|
const event = createEventWithInstant(); |
|
|
|
expect(event.name).toBe("System Boot"); |
|
expect(event.timestamp).toBeInstanceOf(Temporal.Instant); |
|
expect(event.timestamp.toString()).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/); |
|
}); |
|
|
|
it("should have correct type for timestamp", () => { |
|
const event = createEventWithInstant(); |
|
|
|
expect(event.timestamp.constructor.name).toBe("Instant"); |
|
}); |
|
}); |
|
|
|
describe("Event creation with ISO String", () => { |
|
it("should create event with ISO string and parse to Temporal.Instant", () => { |
|
const event = createEventWithISOString(); |
|
|
|
expect(event.name).toBe("User Login"); |
|
expect(event.timestamp).toBeInstanceOf(Temporal.Instant); |
|
expect(event.timestamp.toString()).toBe("2025-10-24T15:30:00Z"); |
|
}); |
|
|
|
it("should have correct type for timestamp", () => { |
|
const event = createEventWithISOString(); |
|
|
|
expect(event.timestamp.constructor.name).toBe("Instant"); |
|
}); |
|
}); |
A subset of https://gist.github.com/kevinmichaelchen/4f48dd7da6e453d6d73adfe49750340b