aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMatt Mayer <[email protected]>2024-03-28 19:02:38 +0700
committerGitHub <[email protected]>2024-03-28 13:02:38 +0100
commitda35c51d16eccd99a7001a5b055a24806168435d (patch)
tree611aeaf1e12f27e5de60ba46864896c2e311374b /test
parent47f008aff5aee08057ad5445d5b3dfbd1b196934 (diff)
downloadfaker-da35c51d16eccd99a7001a5b055a24806168435d.tar.xz
faker-da35c51d16eccd99a7001a5b055a24806168435d.zip
refactor(date)!: stricter error handling of between (#2719)
Diffstat (limited to 'test')
-rw-r--r--test/modules/date.spec.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/test/modules/date.spec.ts b/test/modules/date.spec.ts
index bef01610..432573c7 100644
--- a/test/modules/date.spec.ts
+++ b/test/modules/date.spec.ts
@@ -256,6 +256,32 @@ describe('date', () => {
expect(date).lessThan(to);
}
);
+
+ it('should throw an error when from is after to', () => {
+ expect(() =>
+ faker.date.between({
+ from: '2000-01-01',
+ to: '1990-01-01',
+ })
+ ).toThrow(new FakerError('`from` date must be before `to` date.'));
+ });
+
+ it('should allow date 0 (start of UNIX epoch)', () => {
+ const date = faker.date.between({
+ from: 0,
+ to: '1970-12-31',
+ });
+ expect(date).greaterThan(new Date(0));
+ });
+
+ it('should throw an error if to is invalid', () => {
+ expect(() =>
+ faker.date.between({
+ from: '1990-01-01',
+ to: 'not-a-date',
+ })
+ ).toThrow(new FakerError('Invalid to date: not-a-date'));
+ });
});
describe('betweens()', () => {
@@ -325,6 +351,26 @@ describe('date', () => {
expect(dates.at(-1)).lessThan(to);
}
);
+
+ it('should throw an error when from is after to', () => {
+ expect(() =>
+ faker.date.betweens({
+ from: '2000-01-01',
+ to: '1990-01-01',
+ count: 3,
+ })
+ ).toThrow(new FakerError('`from` date must be before `to` date.'));
+ });
+
+ it('should throw an error if to is invalid', () => {
+ expect(() =>
+ faker.date.betweens({
+ from: '1990-01-01',
+ to: 'not-a-date',
+ count: 3,
+ })
+ ).toThrow(new FakerError('Invalid to date: not-a-date'));
+ });
});
describe('recent()', () => {