Skip to content

Commit dbbd9e5

Browse files
authored
fix: handle invalid Date objects (#605) (#606)
calling toISOString() on an invalid Date object will throw a Range Error
1 parent d7465fa commit dbbd9e5

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

Diff for: src/formatter/formatPropValue.js

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ const formatPropValue = (
5454
}
5555

5656
if (propValue instanceof Date) {
57+
if (isNaN(propValue.valueOf())) {
58+
return `{new Date(NaN)}`;
59+
}
5760
return `{new Date("${propValue.toISOString()}")}`;
5861
}
5962

Diff for: src/formatter/formatPropValue.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ describe('formatPropValue', () => {
9393
).toBe('{new Date("2017-01-01T11:00:00.000Z")}');
9494
});
9595

96+
it('should format an invalid date prop value', () => {
97+
expect(formatPropValue(new Date(NaN), false, 0, {})).toBe(
98+
'{new Date(NaN)}'
99+
);
100+
});
101+
96102
it('should format an object prop value', () => {
97103
expect(formatPropValue({ foo: 42 }, false, 0, {})).toBe(
98104
'{*Mocked formatComplexDataStructure result*}'

0 commit comments

Comments
 (0)