diff options
| author | ST-DDT <[email protected]> | 2024-03-24 13:51:49 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-24 12:51:49 +0000 |
| commit | 3485e7eece540b63059e97bd8166d1ffd88334de (patch) | |
| tree | 08474cb5729326a5966f0cda33221c3179f6ddc4 /test/modules/date.spec.ts | |
| parent | 45150d1ed6d5ce03d0468b938bb5ce4962476966 (diff) | |
| download | faker-3485e7eece540b63059e97bd8166d1ffd88334de.tar.xz faker-3485e7eece540b63059e97bd8166d1ffd88334de.zip | |
refactor(date)!: fail on invalid dates (#2757)
Diffstat (limited to 'test/modules/date.spec.ts')
| -rw-r--r-- | test/modules/date.spec.ts | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/test/modules/date.spec.ts b/test/modules/date.spec.ts index 303dc407..bef01610 100644 --- a/test/modules/date.spec.ts +++ b/test/modules/date.spec.ts @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, it } from 'vitest'; +import { afterEach, describe, expect, it, vi } from 'vitest'; import { FakerError, faker, fakerAZ } from '../../src'; import { seededTests } from '../support/seeded-runs'; import { times } from './../support/times'; @@ -144,6 +144,26 @@ describe('date', () => { describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))( 'random seeded tests for seed %i', () => { + describe('toDate()', () => { + describe.each([ + 'anytime', + 'past', + 'future', + 'recent', + 'soon', + 'birthdate', + ] as const)('%s', (method) => { + it.each(['invalid', Number.NaN, new Date(Number.NaN)] as const)( + 'should reject invalid refDates %s', + (refDate) => { + expect(() => faker.date[method]({ refDate })).toThrow( + new FakerError(`Invalid refDate date: ${refDate.toString()}`) + ); + } + ); + }); + }); + describe('anytime()', () => { it('should return a date', () => { const actual = faker.date.anytime(); @@ -564,22 +584,38 @@ describe('date', () => { faker.seed(20200101); const date = faker.date.past(); expect(date).toBeInstanceOf(Date); - expect(date).toMatchInlineSnapshot('2019-02-25T21:52:41.819Z'); + expect(date).toMatchInlineSnapshot(`2019-11-06T02:07:17.181Z`); faker.seed(20200101); const date2 = faker.date.past(); - expect(date2).toMatchInlineSnapshot('2019-02-25T21:52:41.819Z'); + expect(date2).toMatchInlineSnapshot(`2019-11-06T02:07:17.181Z`); }); it('should use the refDateSource when refDate is not provided (with value)', () => { faker.setDefaultRefDate(Date.UTC(2020, 0, 1)); faker.seed(20200101); const date = faker.date.past(); - expect(date).toMatchInlineSnapshot('2019-02-25T21:52:41.819Z'); + expect(date).toMatchInlineSnapshot(`2019-11-06T02:07:17.181Z`); faker.seed(20200101); const date2 = faker.date.past(); - expect(date2).toMatchInlineSnapshot('2019-02-25T21:52:41.819Z'); + expect(date2).toMatchInlineSnapshot(`2019-11-06T02:07:17.181Z`); + }); + + it('should not use the refDateSource when refDate is provided (with function)', () => { + const spy: () => Date = vi.fn(); + faker.setDefaultRefDate(spy); + faker.seed(20200101); + + const date = faker.date.past({ refDate: Date.UTC(2020, 0, 1) }); + expect(date).toBeInstanceOf(Date); + expect(date).toMatchInlineSnapshot(`2019-11-06T02:07:17.181Z`); + + faker.seed(20200101); + const date2 = faker.date.past({ refDate: Date.UTC(2020, 0, 1) }); + expect(date2).toMatchInlineSnapshot(`2019-11-06T02:07:17.181Z`); + + expect(spy).not.toHaveBeenCalled(); }); }); }); |
