Skip to content

Commit 7ce8c28

Browse files
authored
fix(date): ensures correct range for birthdate (#2535)
1 parent ef965da commit 7ce8c28

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

src/modules/date/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ export class SimpleDateModule extends SimpleModuleBase {
890890
options.min ?? refYear - 80
891891
);
892892
max = new Date(Date.UTC(0, 11, 30)).setUTCFullYear(
893-
options.max ?? refYear - 18
893+
options.max ?? refYear - 19
894894
);
895895
}
896896

test/modules/__snapshots__/date.spec.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ exports[`date > 42 > birthdate > with age mode and refDate 1`] = `1963-09-27T06:
7171

7272
exports[`date > 42 > birthdate > with age range and refDate 1`] = `1962-12-27T20:13:59.702Z`;
7373

74-
exports[`date > 42 > birthdate > with only refDate 1`] = `1964-08-06T01:03:57.017Z`;
74+
exports[`date > 42 > birthdate > with only refDate 1`] = `1964-03-22T08:05:39.972Z`;
7575

7676
exports[`date > 42 > birthdate > with year and refDate 1`] = `0020-07-07T19:06:53.022Z`;
7777

78-
exports[`date > 42 > birthdate > with year mode and refDate 1`] = `1964-08-06T01:03:57.017Z`;
78+
exports[`date > 42 > birthdate > with year mode and refDate 1`] = `1964-03-22T08:05:39.972Z`;
7979

8080
exports[`date > 42 > birthdate > with year range and refDate 1`] = `0057-12-20T11:59:23.890Z`;
8181

@@ -199,11 +199,11 @@ exports[`date > 1211 > birthdate > with age mode and refDate 1`] = `1998-08-21T2
199199

200200
exports[`date > 1211 > birthdate > with age range and refDate 1`] = `1996-10-13T01:44:07.645Z`;
201201

202-
exports[`date > 1211 > birthdate > with only refDate 1`] = `1999-06-29T11:06:58.506Z`;
202+
exports[`date > 1211 > birthdate > with only refDate 1`] = `1998-07-25T13:16:46.938Z`;
203203

204204
exports[`date > 1211 > birthdate > with year and refDate 1`] = `0021-01-26T13:16:31.421Z`;
205205

206-
exports[`date > 1211 > birthdate > with year mode and refDate 1`] = `1999-06-29T11:06:58.506Z`;
206+
exports[`date > 1211 > birthdate > with year mode and refDate 1`] = `1998-07-25T13:16:46.938Z`;
207207

208208
exports[`date > 1211 > birthdate > with year range and refDate 1`] = `0113-12-03T19:45:27.654Z`;
209209

@@ -325,11 +325,11 @@ exports[`date > 1337 > birthdate > with age mode and refDate 1`] = `1956-08-25T0
325325

326326
exports[`date > 1337 > birthdate > with age range and refDate 1`] = `1956-02-15T21:16:40.120Z`;
327327

328-
exports[`date > 1337 > birthdate > with only refDate 1`] = `1957-07-05T09:38:29.057Z`;
328+
exports[`date > 1337 > birthdate > with only refDate 1`] = `1957-03-31T18:18:18.869Z`;
329329

330330
exports[`date > 1337 > birthdate > with year and refDate 1`] = `0020-05-27T14:46:44.831Z`;
331331

332-
exports[`date > 1337 > birthdate > with year mode and refDate 1`] = `1957-07-05T09:38:29.057Z`;
332+
exports[`date > 1337 > birthdate > with year mode and refDate 1`] = `1957-03-31T18:18:18.869Z`;
333333

334334
exports[`date > 1337 > birthdate > with year range and refDate 1`] = `0046-08-09T19:19:18.047Z`;
335335

test/modules/date.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,35 @@ describe('date', () => {
587587
expect(birthdate.getUTCFullYear()).toBeLessThanOrEqual(max);
588588
});
589589

590+
it('returns a random birthdate that is 18+ by default', () => {
591+
// Generate the latest possible value => youngest
592+
faker.seed(2855577693);
593+
594+
const refDate = new Date();
595+
const birthdate = faker.date.birthdate({ refDate });
596+
expect(birthdate).toBeInstanceOf(Date);
597+
const value = birthdate.valueOf();
598+
const refDateValue = refDate.valueOf();
599+
expect(value).toBeLessThanOrEqual(refDateValue);
600+
const deltaDate = new Date(refDateValue - value);
601+
expect(deltaDate.getUTCFullYear() - 1970).toBeGreaterThanOrEqual(18);
602+
});
603+
604+
it('returns a random birthdate in one year', () => {
605+
const min = 1990;
606+
const max = 1990;
607+
608+
const birthdate = faker.date.birthdate({ min, max, mode: 'year' });
609+
610+
// birthdate is a date object
611+
expect(birthdate).toBeInstanceOf(Date);
612+
expect(birthdate.toISOString()).not.toMatch(/T00:00:00.000Z/);
613+
614+
// Generated date is between min and max
615+
expect(birthdate.getUTCFullYear()).toBeGreaterThanOrEqual(min);
616+
expect(birthdate.getUTCFullYear()).toBeLessThanOrEqual(max);
617+
});
618+
590619
it('returns a random birthdate between two ages', () => {
591620
const min = 4;
592621
const max = 5;

0 commit comments

Comments
 (0)