From df1c83df7547c26fc7f10f530cc1f301e35b65c8 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sun, 24 Mar 2024 13:32:36 +0100 Subject: [PATCH 1/3] infra(netlify): fix ignore command (#2767) --- netlify.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netlify.toml b/netlify.toml index a3c815cc46d..73355306402 100644 --- a/netlify.toml +++ b/netlify.toml @@ -11,7 +11,7 @@ exit 1; else # Otherwise check if something doc related changed. - git diff --quiet origin/next...$COMMIT_REF -- . \':!src/locale/\' \':!src/locales/\' \':!test/\' \':!package.json\' \':!pnpm-lock.yaml\' \':!.github/\' + git diff --quiet origin/next...$COMMIT_REF -- . ":!src/locale/" ":!src/locales/" ":!test/" ":!package.json" ":!pnpm-lock.yaml" ":!.github/" fi ''' From 45150d1ed6d5ce03d0468b938bb5ce4962476966 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sun, 24 Mar 2024 13:43:07 +0100 Subject: [PATCH 2/3] test(finance): improve readability (#2766) --- test/modules/finance.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/modules/finance.spec.ts b/test/modules/finance.spec.ts index 5c3549c1c36..7f8a8255716 100644 --- a/test/modules/finance.spec.ts +++ b/test/modules/finance.spec.ts @@ -166,7 +166,7 @@ describe('finance', () => { }); it('should set a specified length', () => { - const expected = faker.number.int(20) || 4; + const expected = faker.number.int({ min: 1, max: 20 }); const mask = faker.finance.maskedNumber({ length: expected, From 3485e7eece540b63059e97bd8166d1ffd88334de Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sun, 24 Mar 2024 13:51:49 +0100 Subject: [PATCH 3/3] refactor(date)!: fail on invalid dates (#2757) --- docs/guide/upgrading_v9/2757.md | 9 ++ src/modules/date/index.ts | 117 +++++++------------ test/modules/__snapshots__/date.spec.ts.snap | 48 ++++---- test/modules/__snapshots__/git.spec.ts.snap | 36 +++--- test/modules/date.spec.ts | 46 +++++++- 5 files changed, 137 insertions(+), 119 deletions(-) create mode 100644 docs/guide/upgrading_v9/2757.md diff --git a/docs/guide/upgrading_v9/2757.md b/docs/guide/upgrading_v9/2757.md new file mode 100644 index 00000000000..c7a795776ab --- /dev/null +++ b/docs/guide/upgrading_v9/2757.md @@ -0,0 +1,9 @@ +### Fail on invalid dates + +Various methods in the `faker.date` module allow you to pass a `Date`-ish value: +that is, either a Javascript Date, or a timestamp number or string that can be converted to a `Date` via the `new Date()` constructor. + +Previously, if you passed something which could not be parsed to a `Date`, it would fall back to the current reference date. +Now, this throws an error raising awareness of that bad value. + +This affects the `refDate` parameter of the `anytime()`, `birthdate()`, `past()`, `future()`, `recent()` and `soon()`, methods as well as the `from` and `to` parameters of `between()` and `betweens()`. diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 2729c8496a3..cc8b2d0a90c 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -5,26 +5,21 @@ import { SimpleModuleBase } from '../../internal/module-base'; import { assertLocaleData } from '../../locale-proxy'; /** - * Converts date passed as a string, number or Date to a Date object. - * If nothing or a non-parsable value is passed, then it will take the value from the given fallback. + * Converts a date passed as a `string`, `number` or `Date` to a valid `Date` object. * * @param date The date to convert. - * @param fallback The fallback date to use if the passed date is not valid. + * @param name The reference name used for error messages. Defaults to `'refDate'`. + * + * @throws If the given date is invalid. */ -function toDate( - date: string | Date | number | undefined, - fallback: () => Date -): Date { - if (date == null) { - return fallback(); - } +function toDate(date: string | Date | number, name: string = 'refDate'): Date { + const converted = new Date(date); - date = new Date(date); - if (Number.isNaN(date.valueOf())) { - date = fallback(); + if (Number.isNaN(converted.valueOf())) { + throw new FakerError(`Invalid ${name} date: ${date.toString()}`); } - return date; + return converted; } /** @@ -56,13 +51,12 @@ export class SimpleDateModule extends SimpleModuleBase { refDate?: string | Date | number; } = {} ): Date { - const { refDate } = options; - - const date = toDate(refDate, this.faker.defaultRefDate); + const { refDate = this.faker.defaultRefDate() } = options; + const time = toDate(refDate).getTime(); return this.between({ - from: new Date(date.getTime() - 1000 * 60 * 60 * 24 * 365), - to: new Date(date.getTime() + 1000 * 60 * 60 * 24 * 365), + from: time - 1000 * 60 * 60 * 24 * 365, + to: time + 1000 * 60 * 60 * 24 * 365, }); } @@ -98,23 +92,18 @@ export class SimpleDateModule extends SimpleModuleBase { refDate?: string | Date | number; } = {} ): Date { - const { years = 1, refDate } = options; + const { years = 1, refDate = this.faker.defaultRefDate() } = options; if (years <= 0) { throw new FakerError('Years must be greater than 0.'); } - const date = toDate(refDate, this.faker.defaultRefDate); - const range = { - min: 1000, - max: years * 365 * 24 * 3600 * 1000, - }; - - let past = date.getTime(); - past -= this.faker.number.int(range); // some time from now to N years ago, in milliseconds - date.setTime(past); + const time = toDate(refDate).getTime(); - return date; + return this.between({ + from: time - years * 365 * 24 * 3600 * 1000, + to: time - 1000, + }); } /** @@ -149,23 +138,18 @@ export class SimpleDateModule extends SimpleModuleBase { refDate?: string | Date | number; } = {} ): Date { - const { years = 1, refDate } = options; + const { years = 1, refDate = this.faker.defaultRefDate() } = options; if (years <= 0) { throw new FakerError('Years must be greater than 0.'); } - const date = toDate(refDate, this.faker.defaultRefDate); - const range = { - min: 1000, - max: years * 365 * 24 * 3600 * 1000, - }; + const time = toDate(refDate).getTime(); - let future = date.getTime(); - future += this.faker.number.int(range); // some time from now to N years later, in milliseconds - date.setTime(future); - - return date; + return this.between({ + from: time + 1000, + to: time + years * 365 * 24 * 3600 * 1000, + }); } /** @@ -192,11 +176,10 @@ export class SimpleDateModule extends SimpleModuleBase { }): Date { const { from, to } = options; - const fromMs = toDate(from, this.faker.defaultRefDate).getTime(); - const toMs = toDate(to, this.faker.defaultRefDate).getTime(); - const dateOffset = this.faker.number.int(toMs - fromMs); + const fromMs = toDate(from, 'from').getTime(); + const toMs = toDate(to, 'to').getTime(); - return new Date(fromMs + dateOffset); + return new Date(this.faker.number.int({ min: fromMs, max: toMs })); } /** @@ -291,23 +274,18 @@ export class SimpleDateModule extends SimpleModuleBase { refDate?: string | Date | number; } = {} ): Date { - const { days = 1, refDate } = options; + const { days = 1, refDate = this.faker.defaultRefDate() } = options; if (days <= 0) { throw new FakerError('Days must be greater than 0.'); } - const date = toDate(refDate, this.faker.defaultRefDate); - const range = { - min: 1000, - max: days * 24 * 3600 * 1000, - }; - - let future = date.getTime(); - future -= this.faker.number.int(range); // some time from now to N days ago, in milliseconds - date.setTime(future); + const time = toDate(refDate).getTime(); - return date; + return this.between({ + from: time - days * 24 * 3600 * 1000, + to: time - 1000, + }); } /** @@ -342,23 +320,18 @@ export class SimpleDateModule extends SimpleModuleBase { refDate?: string | Date | number; } = {} ): Date { - const { days = 1, refDate } = options; + const { days = 1, refDate = this.faker.defaultRefDate() } = options; if (days <= 0) { throw new FakerError('Days must be greater than 0.'); } - const date = toDate(refDate, this.faker.defaultRefDate); - const range = { - min: 1000, - max: days * 24 * 3600 * 1000, - }; + const time = toDate(refDate).getTime(); - let future = date.getTime(); - future += this.faker.number.int(range); // some time from now to N days later, in milliseconds - date.setTime(future); - - return date; + return this.between({ + from: time + 1000, + to: time + days * 24 * 3600 * 1000, + }); } /** @@ -415,9 +388,9 @@ export class SimpleDateModule extends SimpleModuleBase { refDate?: string | Date | number; } = {} ): Date { - const mode = options.mode === 'age' ? 'age' : 'year'; - const refDate = toDate(options.refDate, this.faker.defaultRefDate); - const refYear = refDate.getUTCFullYear(); + const { mode = 'year', refDate = this.faker.defaultRefDate() } = options; + const date = toDate(refDate); + const refYear = date.getUTCFullYear(); // If no min or max is specified, generate a random date between (now - 80) years and (now - 18) years respectively // So that people can still be considered as adults in most cases @@ -426,8 +399,8 @@ export class SimpleDateModule extends SimpleModuleBase { let min: number; let max: number; if (mode === 'age') { - min = new Date(refDate).setUTCFullYear(refYear - (options.max ?? 80) - 1); - max = new Date(refDate).setUTCFullYear(refYear - (options.min ?? 18)); + min = new Date(date).setUTCFullYear(refYear - (options.max ?? 80) - 1); + max = new Date(date).setUTCFullYear(refYear - (options.min ?? 18)); } else { // Avoid generating dates the first and last date of the year // to avoid running into other years depending on the timezone. diff --git a/test/modules/__snapshots__/date.spec.ts.snap b/test/modules/__snapshots__/date.spec.ts.snap index 9b3c3d7659b..f67a339dfd3 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 3c8df2e21a3..8746903c83d 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 -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 -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 -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 -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 -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 -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 -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 -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 -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 303dc407524..bef0161038f 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(); }); }); });