I’m trying to mock the current Time (Temporal.Now.plainDateTimeISO()
) with
<code> vi.setSystemTime(new Date('2024-06-01T11:00:00'));
</code>
<code> vi.setSystemTime(new Date('2024-06-01T11:00:00'));
</code>
vi.setSystemTime(new Date('2024-06-01T11:00:00'));
but every time I run this test the mocked time is ignored.
store:
<code>import { Temporal } from '@js-temporal/polyfill'
const createTimeStore = () => {
let currentTime = Temporal.Now.plainDateTimeISO()
const subscribers = new Set<() => void>()
const subscribe = (callback: () => void) => {
subscribers.add(callback)
return () => subscribers.delete(callback)
}
const getSnapshot = () => currentTime
const updateTime = () => {
currentTime = Temporal.Now.plainDateTimeISO()
subscribers.forEach((callback) => callback())
const now = Temporal.Now.plainDateTimeISO()
const msToNextMinute = (60 - now.second) * 1000 - now.millisecond
setTimeout(updateTime, msToNextMinute)
}
updateTime()
return {
subscribe,
getSnapshot,
}
}
const timeStore = createTimeStore()
export const useTimeStore = () => {
const { subscribe, getSnapshot } = timeStore
return {
getSnapshot,
subscribe,
}
}
</code>
<code>import { Temporal } from '@js-temporal/polyfill'
const createTimeStore = () => {
let currentTime = Temporal.Now.plainDateTimeISO()
const subscribers = new Set<() => void>()
const subscribe = (callback: () => void) => {
subscribers.add(callback)
return () => subscribers.delete(callback)
}
const getSnapshot = () => currentTime
const updateTime = () => {
currentTime = Temporal.Now.plainDateTimeISO()
subscribers.forEach((callback) => callback())
const now = Temporal.Now.plainDateTimeISO()
const msToNextMinute = (60 - now.second) * 1000 - now.millisecond
setTimeout(updateTime, msToNextMinute)
}
updateTime()
return {
subscribe,
getSnapshot,
}
}
const timeStore = createTimeStore()
export const useTimeStore = () => {
const { subscribe, getSnapshot } = timeStore
return {
getSnapshot,
subscribe,
}
}
</code>
import { Temporal } from '@js-temporal/polyfill'
const createTimeStore = () => {
let currentTime = Temporal.Now.plainDateTimeISO()
const subscribers = new Set<() => void>()
const subscribe = (callback: () => void) => {
subscribers.add(callback)
return () => subscribers.delete(callback)
}
const getSnapshot = () => currentTime
const updateTime = () => {
currentTime = Temporal.Now.plainDateTimeISO()
subscribers.forEach((callback) => callback())
const now = Temporal.Now.plainDateTimeISO()
const msToNextMinute = (60 - now.second) * 1000 - now.millisecond
setTimeout(updateTime, msToNextMinute)
}
updateTime()
return {
subscribe,
getSnapshot,
}
}
const timeStore = createTimeStore()
export const useTimeStore = () => {
const { subscribe, getSnapshot } = timeStore
return {
getSnapshot,
subscribe,
}
}
test:
<code>test('my test', () => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2024-06-01T11:00:00'));
const { result } = renderHook(() =>
myHook()
);
const time = result.current.getTime();
expect(time).toBe(11:00);
});
</code>
<code>test('my test', () => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2024-06-01T11:00:00'));
const { result } = renderHook(() =>
myHook()
);
const time = result.current.getTime();
expect(time).toBe(11:00);
});
</code>
test('my test', () => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2024-06-01T11:00:00'));
const { result } = renderHook(() =>
myHook()
);
const time = result.current.getTime();
expect(time).toBe(11:00);
});