aboutsummaryrefslogtreecommitdiff
path: root/test/internal/date.spec.ts
blob: 453dae792e849143fbc11a6f2e75917a209280d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { describe, expect, it } from 'vitest';
import { FakerError } from '../../src';
import { toDate } from '../../src/internal/date';

describe('toDate()', () => {
  it('should convert a string date to a valid Date object', () => {
    const dateString = '2024-07-05';
    expect(toDate(dateString)).toEqual(new Date(dateString));
  });

  it('should convert a string datetime to a valid Date object', () => {
    const timestamp = '2024-07-05T15:49:19+0000';
    expect(toDate(timestamp)).toEqual(new Date(timestamp));
  });

  it('should throw a FakerError for an invalid date string', () => {
    const timestamp = 'aaaa-07-05T15:49:19+0000';
    expect(() => toDate(timestamp)).toThrow(
      new FakerError(`Invalid refDate date: ${timestamp}`)
    );
  });
});