[Unit testing] Vitest, mock Time

发布时间 2023-09-27 21:49:44作者: Zhentiw
import { afterEach, test, expect, vi, beforeEach } from 'vitest';
import { render } from 'test/utilities';
import TimeZone from '.';

beforeEach(() => {
  // freeze time
  vi.useFakeTimers();
  // set system time to a certain point
  vi.setSystemTime(1679492355195);
});

test('it should render successfully', () => {
  render(<TimeZone />);
});

test('should match the snapshot', async () => {
  const { container } = render(<TimeZone />);
  expect(container).toMatchSnapshot();
});

afterEach(() => {
  vi.useRealTimers();
});