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 | |
| parent | 45150d1ed6d5ce03d0468b938bb5ce4962476966 (diff) | |
| download | faker-3485e7eece540b63059e97bd8166d1ffd88334de.tar.xz faker-3485e7eece540b63059e97bd8166d1ffd88334de.zip | |
refactor(date)!: fail on invalid dates (#2757)
Diffstat (limited to 'test/modules')
| -rw-r--r-- | test/modules/__snapshots__/date.spec.ts.snap | 48 | ||||
| -rw-r--r-- | test/modules/__snapshots__/git.spec.ts.snap | 36 | ||||
| -rw-r--r-- | test/modules/date.spec.ts | 46 |
3 files changed, 83 insertions, 47 deletions
diff --git a/test/modules/__snapshots__/date.spec.ts.snap b/test/modules/__snapshots__/date.spec.ts.snap index 9b3c3d76..f67a339d 100644 --- a/test/modules/__snapshots__/date.spec.ts.snap +++ b/test/modules/__snapshots__/date.spec.ts.snap @@ -95,21 +95,21 @@ exports[`date > 42 > month > with abbreviated = true and context = true 1`] = `" exports[`date > 42 > month > with context = true 1`] = `"May"`; -exports[`date > 42 > past > with only Date refDate 1`] = `2020-10-08T00:10:57.898Z`; +exports[`date > 42 > past > with only Date refDate 1`] = `2020-07-08T10:07:32.524Z`; -exports[`date > 42 > past > with only number refDate 1`] = `2020-10-08T00:10:57.898Z`; +exports[`date > 42 > past > with only number refDate 1`] = `2020-07-08T10:07:32.524Z`; -exports[`date > 42 > past > with only string refDate 1`] = `2020-10-08T00:10:57.898Z`; +exports[`date > 42 > past > with only string refDate 1`] = `2020-07-08T10:07:32.524Z`; -exports[`date > 42 > past > with value 1`] = `2017-05-26T15:26:23.206Z`; +exports[`date > 42 > past > with value 1`] = `2014-11-22T18:52:07.216Z`; -exports[`date > 42 > recent > with only Date refDate 1`] = `2021-02-21T08:09:54.819Z`; +exports[`date > 42 > recent > with only Date refDate 1`] = `2021-02-21T02:08:35.603Z`; -exports[`date > 42 > recent > with only number refDate 1`] = `2021-02-21T08:09:54.819Z`; +exports[`date > 42 > recent > with only number refDate 1`] = `2021-02-21T02:08:35.603Z`; -exports[`date > 42 > recent > with only string refDate 1`] = `2021-02-21T08:09:54.819Z`; +exports[`date > 42 > recent > with only string refDate 1`] = `2021-02-21T02:08:35.603Z`; -exports[`date > 42 > recent > with value 1`] = `2021-02-17T23:15:52.423Z`; +exports[`date > 42 > recent > with value 1`] = `2021-02-15T11:02:37.999Z`; exports[`date > 42 > soon > with only Date refDate 1`] = `2021-02-22T02:08:36.603Z`; @@ -223,21 +223,21 @@ exports[`date > 1211 > month > with abbreviated = true and context = true 1`] = exports[`date > 1211 > month > with context = true 1`] = `"December"`; -exports[`date > 1211 > past > with only Date refDate 1`] = `2020-03-19T19:19:04.066Z`; +exports[`date > 1211 > past > with only Date refDate 1`] = `2021-01-26T14:59:26.356Z`; -exports[`date > 1211 > past > with only number refDate 1`] = `2020-03-19T19:19:04.066Z`; +exports[`date > 1211 > past > with only number refDate 1`] = `2021-01-26T14:59:26.356Z`; -exports[`date > 1211 > past > with only string refDate 1`] = `2020-03-19T19:19:04.066Z`; +exports[`date > 1211 > past > with only string refDate 1`] = `2021-01-26T14:59:26.356Z`; -exports[`date > 1211 > past > with value 1`] = `2011-11-12T14:47:19.904Z`; +exports[`date > 1211 > past > with value 1`] = `2020-06-05T19:31:10.518Z`; -exports[`date > 1211 > recent > with only Date refDate 1`] = `2021-02-20T18:52:11.498Z`; +exports[`date > 1211 > recent > with only Date refDate 1`] = `2021-02-21T15:26:18.924Z`; -exports[`date > 1211 > recent > with only number refDate 1`] = `2021-02-20T18:52:11.498Z`; +exports[`date > 1211 > recent > with only number refDate 1`] = `2021-02-21T15:26:18.924Z`; -exports[`date > 1211 > recent > with only string refDate 1`] = `2021-02-20T18:52:11.498Z`; +exports[`date > 1211 > recent > with only string refDate 1`] = `2021-02-21T15:26:18.924Z`; -exports[`date > 1211 > recent > with value 1`] = `2021-02-12T10:18:34.226Z`; +exports[`date > 1211 > recent > with value 1`] = `2021-02-20T23:59:56.196Z`; exports[`date > 1211 > soon > with only Date refDate 1`] = `2021-02-22T15:26:19.924Z`; @@ -349,21 +349,21 @@ exports[`date > 1337 > month > with abbreviated = true and context = true 1`] = exports[`date > 1337 > month > with context = true 1`] = `"April"`; -exports[`date > 1337 > past > with only Date refDate 1`] = `2020-11-18T01:49:04.822Z`; +exports[`date > 1337 > past > with only Date refDate 1`] = `2020-05-28T08:29:25.600Z`; -exports[`date > 1337 > past > with only number refDate 1`] = `2020-11-18T01:49:04.822Z`; +exports[`date > 1337 > past > with only number refDate 1`] = `2020-05-28T08:29:25.600Z`; -exports[`date > 1337 > past > with only string refDate 1`] = `2020-11-18T01:49:04.822Z`; +exports[`date > 1337 > past > with only string refDate 1`] = `2020-05-28T08:29:25.600Z`; -exports[`date > 1337 > past > with value 1`] = `2018-07-11T07:47:33.460Z`; +exports[`date > 1337 > past > with value 1`] = `2013-10-08T02:30:56.962Z`; -exports[`date > 1337 > recent > with only Date refDate 1`] = `2021-02-21T10:51:56.041Z`; +exports[`date > 1337 > recent > with only Date refDate 1`] = `2021-02-20T23:26:34.381Z`; -exports[`date > 1337 > recent > with only number refDate 1`] = `2021-02-21T10:51:56.041Z`; +exports[`date > 1337 > recent > with only number refDate 1`] = `2021-02-20T23:26:34.381Z`; -exports[`date > 1337 > recent > with only string refDate 1`] = `2021-02-21T10:51:56.041Z`; +exports[`date > 1337 > recent > with only string refDate 1`] = `2021-02-20T23:26:34.381Z`; -exports[`date > 1337 > recent > with value 1`] = `2021-02-19T02:16:05.654Z`; +exports[`date > 1337 > recent > with value 1`] = `2021-02-14T08:02:24.768Z`; exports[`date > 1337 > soon > with only Date refDate 1`] = `2021-02-21T23:26:35.381Z`; diff --git a/test/modules/__snapshots__/git.spec.ts.snap b/test/modules/__snapshots__/git.spec.ts.snap index 3c8df2e2..8746903c 100644 --- a/test/modules/__snapshots__/git.spec.ts.snap +++ b/test/modules/__snapshots__/git.spec.ts.snap @@ -2,16 +2,16 @@ exports[`git > 42 > branch 1`] = `"array-parse"`; -exports[`git > 42 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 15:00:39 2019 +1100"`; +exports[`git > 42 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 08:59:19 2019 +1100"`; -exports[`git > 42 > commitDate > with only number refDate 1`] = `"Tue Dec 31 15:00:39 2019 +1100"`; +exports[`git > 42 > commitDate > with only number refDate 1`] = `"Tue Dec 31 08:59:19 2019 +1100"`; -exports[`git > 42 > commitDate > with only string refDate 1`] = `"Tue Dec 31 15:00:39 2019 +1100"`; +exports[`git > 42 > commitDate > with only string refDate 1`] = `"Tue Dec 31 08:59:19 2019 +1100"`; exports[`git > 42 > commitEntry > with only Date refDate 1`] = ` "commit ead331ddf0fc4446b96d368ab4bd1d31efb62f92 Author: Jerome Vandervort <[email protected]> -Date: Tue Dec 31 09:39:01 2019 +1100 +Date: Tue Dec 31 14:20:57 2019 +1100 bypass digital protocol " @@ -20,7 +20,7 @@ Date: Tue Dec 31 09:39:01 2019 +1100 exports[`git > 42 > commitEntry > with only number refDate 1`] = ` "commit ead331ddf0fc4446b96d368ab4bd1d31efb62f92 Author: Jerome Vandervort <[email protected]> -Date: Tue Dec 31 09:39:01 2019 +1100 +Date: Tue Dec 31 14:20:57 2019 +1100 bypass digital protocol " @@ -29,7 +29,7 @@ Date: Tue Dec 31 09:39:01 2019 +1100 exports[`git > 42 > commitEntry > with only string refDate 1`] = ` "commit ead331ddf0fc4446b96d368ab4bd1d31efb62f92 Author: Jerome Vandervort <[email protected]> -Date: Tue Dec 31 09:39:01 2019 +1100 +Date: Tue Dec 31 14:20:57 2019 +1100 bypass digital protocol " @@ -45,16 +45,16 @@ exports[`git > 42 > commitSha > with length 8 1`] = `"8ead331d"`; exports[`git > 1211 > branch 1`] = `"capacitor-reboot"`; -exports[`git > 1211 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 01:42:55 2019 +1000"`; +exports[`git > 1211 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 22:17:03 2019 +1000"`; -exports[`git > 1211 > commitDate > with only number refDate 1`] = `"Tue Dec 31 01:42:55 2019 +1000"`; +exports[`git > 1211 > commitDate > with only number refDate 1`] = `"Tue Dec 31 22:17:03 2019 +1000"`; -exports[`git > 1211 > commitDate > with only string refDate 1`] = `"Tue Dec 31 01:42:55 2019 +1000"`; +exports[`git > 1211 > commitDate > with only string refDate 1`] = `"Tue Dec 31 22:17:03 2019 +1000"`; exports[`git > 1211 > commitEntry > with only Date refDate 1`] = ` "commit d4fefa7fbaec9dc4c48fa8ebf46fb7c8563cf3fa Author: Deion Durgan <[email protected]> -Date: Tue Dec 31 12:51:43 2019 -0600 +Date: Tue Dec 31 11:08:15 2019 -0600 calculate optical bandwidth " @@ -63,7 +63,7 @@ Date: Tue Dec 31 12:51:43 2019 -0600 exports[`git > 1211 > commitEntry > with only number refDate 1`] = ` "commit d4fefa7fbaec9dc4c48fa8ebf46fb7c8563cf3fa Author: Deion Durgan <[email protected]> -Date: Tue Dec 31 12:51:43 2019 -0600 +Date: Tue Dec 31 11:08:15 2019 -0600 calculate optical bandwidth " @@ -72,7 +72,7 @@ Date: Tue Dec 31 12:51:43 2019 -0600 exports[`git > 1211 > commitEntry > with only string refDate 1`] = ` "commit d4fefa7fbaec9dc4c48fa8ebf46fb7c8563cf3fa Author: Deion Durgan <[email protected]> -Date: Tue Dec 31 12:51:43 2019 -0600 +Date: Tue Dec 31 11:08:15 2019 -0600 calculate optical bandwidth " @@ -88,16 +88,16 @@ exports[`git > 1211 > commitSha > with length 8 1`] = `"ed4fefa7"`; exports[`git > 1337 > branch 1`] = `"port-hack"`; -exports[`git > 1337 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 17:42:40 2019 -0800"`; +exports[`git > 1337 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 06:17:18 2019 -0800"`; -exports[`git > 1337 > commitDate > with only number refDate 1`] = `"Tue Dec 31 17:42:40 2019 -0800"`; +exports[`git > 1337 > commitDate > with only number refDate 1`] = `"Tue Dec 31 06:17:18 2019 -0800"`; -exports[`git > 1337 > commitDate > with only string refDate 1`] = `"Tue Dec 31 17:42:40 2019 -0800"`; +exports[`git > 1337 > commitDate > with only string refDate 1`] = `"Tue Dec 31 06:17:18 2019 -0800"`; exports[`git > 1337 > commitEntry > with only Date refDate 1`] = ` "commit 36a7b5fa28d2f9bb79ca46ea394bc4f9bb0af328 Author: Matt_Hills <[email protected]> -Date: Tue Dec 31 23:39:16 2019 -0900 +Date: Tue Dec 31 00:20:42 2019 -0900 reboot mobile sensor " @@ -106,7 +106,7 @@ Date: Tue Dec 31 23:39:16 2019 -0900 exports[`git > 1337 > commitEntry > with only number refDate 1`] = ` "commit 36a7b5fa28d2f9bb79ca46ea394bc4f9bb0af328 Author: Matt_Hills <[email protected]> -Date: Tue Dec 31 23:39:16 2019 -0900 +Date: Tue Dec 31 00:20:42 2019 -0900 reboot mobile sensor " @@ -115,7 +115,7 @@ Date: Tue Dec 31 23:39:16 2019 -0900 exports[`git > 1337 > commitEntry > with only string refDate 1`] = ` "commit 36a7b5fa28d2f9bb79ca46ea394bc4f9bb0af328 Author: Matt_Hills <[email protected]> -Date: Tue Dec 31 23:39:16 2019 -0900 +Date: Tue Dec 31 00:20:42 2019 -0900 reboot mobile sensor " 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(); }); }); }); |
