diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 111fc4bae6f..ea9d15d7768 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -47,10 +47,14 @@ export class DateModule { * @since 2.0.1 */ past(years?: number, refDate?: string | Date | number): Date { + if (years <= 0) { + throw new FakerError('Years must be greater than 0.'); + } + const date = toDate(refDate); const range = { min: 1000, - max: (years || 1) * 365 * 24 * 3600 * 1000, + max: (years ?? 1) * 365 * 24 * 3600 * 1000, }; let past = date.getTime(); @@ -76,10 +80,14 @@ export class DateModule { * @since 2.0.1 */ future(years?: number, refDate?: string | Date | number): Date { + if (years <= 0) { + throw new FakerError('Years must be greater than 0.'); + } + const date = toDate(refDate); const range = { min: 1000, - max: (years || 1) * 365 * 24 * 3600 * 1000, + max: (years ?? 1) * 365 * 24 * 3600 * 1000, }; let future = date.getTime(); @@ -157,10 +165,14 @@ export class DateModule { * @since 2.0.1 */ recent(days?: number, refDate?: string | Date | number): Date { + if (days <= 0) { + throw new FakerError('Days must be greater than 0.'); + } + const date = toDate(refDate); const range = { min: 1000, - max: (days || 1) * 24 * 3600 * 1000, + max: (days ?? 1) * 24 * 3600 * 1000, }; let future = date.getTime(); @@ -186,10 +198,14 @@ export class DateModule { * @since 5.0.0 */ soon(days?: number, refDate?: string | Date | number): Date { + if (days <= 0) { + throw new FakerError('Days must be greater than 0.'); + } + const date = toDate(refDate); const range = { min: 1000, - max: (days || 1) * 24 * 3600 * 1000, + max: (days ?? 1) * 24 * 3600 * 1000, }; let future = date.getTime(); diff --git a/test/date.spec.ts b/test/date.spec.ts index d5016e2dc7a..365e2cab7f5 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it } from 'vitest'; -import { faker } from '../src'; +import { faker, FakerError } from '../src'; import { seededTests } from './support/seededRuns'; const converterMap = [ @@ -130,11 +130,11 @@ describe('date', () => { expect(date).greaterThanOrEqual(yearsAgo); }); - it('should return a past date when years 0', () => { + it('should throw an error when years = 0', () => { const refDate = new Date(); - const date = faker.date.past(0, refDate.toISOString()); - - expect(date).lessThan(refDate); + expect(() => faker.date.past(0, refDate.toISOString())).toThrow( + new FakerError('Years must be greater than 0.') + ); }); it.each(converterMap)( @@ -158,11 +158,11 @@ describe('date', () => { expect(date).greaterThan(new Date()); }); - it('should return a future date when years 0', () => { + it('should throw an error when years = 0', () => { const refDate = new Date(); - const date = faker.date.future(0, refDate.toISOString()); - - expect(date).greaterThan(refDate); // date should be after the date given + expect(() => faker.date.future(0, refDate.toISOString())).toThrow( + new FakerError('Years must be greater than 0.') + ); }); it.each(converterMap)( @@ -218,6 +218,13 @@ describe('date', () => { expect(date).lessThanOrEqual(new Date()); }); + it('should throw an error when days = 0', () => { + const refDate = new Date(); + expect(() => faker.date.recent(0, refDate.toISOString())).toThrow( + new FakerError('Days must be greater than 0.') + ); + }); + it.each(converterMap)( 'should return a date N days from the recent past, starting from refDate', (converter) => { @@ -249,6 +256,13 @@ describe('date', () => { expect(date).greaterThanOrEqual(new Date()); }); + it('should throw an error when days = 0', () => { + const refDate = new Date(); + expect(() => faker.date.soon(0, refDate.toISOString())).toThrow( + new FakerError('Days must be greater than 0.') + ); + }); + it.each(converterMap)( 'should return a date N days from the recent future, starting from refDate', (converter) => {