vitest
timezones#
dayjs#
typescript
import dayjs from "dayjs";
import tz from "dayjs/plugin/timezone";
dayjs.extend(tz);
const now = dayjs();
const sydneyDate = dayjs("2025-03-03").tz("Australia/Sydney");
const isToday = now.isSame(sydneyDate, "day");
Intl.DateTimeFormat
#
typescript
const date = new Date("2025-03-03");
const asSydneyTime = date.toLocaleDateString("en-AU", {
timeZone: "Australia/Sydney",
});
vitest#
Running tests in multiple time zones#
typescript
// vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
globals: true,
workspace: [
{
extends: true,
test: {
name: "GMT",
env: {
TZ: "UTC",
},
},
},
{
extends: true,
test: {
name: "GMT+13",
env: {
TZ: "America/Auckland",
},
},
},
],
},
});
Mocking now & timezones in tests#
typescript
export const mockNow = (now: string, timeZone?: string) => {
beforeEach(() => {
vi.useFakeTimers();
const date = new Date(now);
vi.setSystemTime(date);
if (timeZone) {
vi.spyOn(globalThis.Intl, "DateTimeFormat").mockImplementation(() => ({
resolvedOptions: () => ({
timeZone, // Your timezone, rest is irrelevant
calendar: "gregory",
locale: "en-AU",
numberingSystem: "latin",
}),
format: () => "1/1/2022",
formatRange: () => "1/1/2022 – 1/1/2023",
formatRangeToParts: () => [],
formatToParts: () => [],
}));
}
});
afterEach(() => {
vi.useRealTimers();
});
};
chrome#
Simulate different time zones in Chrome DevTools:
- Open DevTools
- Open Console Drawer (Esc)
- Click on the three dots in the left corner
- Select Sensors
- Enter a Timezone ID like
UTC
orAustralia/Sydney
links#
Evalite - a vitest-based eval runner by Matt Pocock.
Vitest runs tests 3x slower than Jest with threads: true (default) setting discusses performance differences between Vitest and Jest, leading to the introduction of vmThreads pool in Vitest 0.34.0 to achieve comparable speeds while maintaining test isolation.
Since resolved.