From 11ad01110c57d64ba9cd1cb0c2abb9c38852204d Mon Sep 17 00:00:00 2001 From: Alen Ajam Date: Tue, 14 Nov 2023 18:41:59 +0100 Subject: [PATCH] fix: DateTime throws when year is invalid and throwOnInvalid is true (#1539) --- src/datetime.js | 4 ++++ test/datetime/invalid.test.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/datetime.js b/src/datetime.js index f98d183f5..9472cb531 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -810,6 +810,10 @@ export default class DateTime { ); } + if (!inst.isValid) { + return DateTime.invalid(inst.invalid); + } + return inst; } diff --git a/test/datetime/invalid.test.js b/test/datetime/invalid.test.js index a776e1854..d13fb8643 100644 --- a/test/datetime/invalid.test.js +++ b/test/datetime/invalid.test.js @@ -72,3 +72,18 @@ test("throwOnInvalid throws", () => { test("DateTime.invalid throws if you don't provide a reason", () => { expect(() => DateTime.invalid()).toThrow(); }); + +test("throwOnInvalid throws if year is too big", () => { + try { + Settings.throwOnInvalid = true; + expect(() => + DateTime.fromObject({ + year: 9999999, + month: 5, + day: 25, + }) + ).toThrow(); + } finally { + Settings.throwOnInvalid = false; + } +});