-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix pretty format failing when printing invalid dates #6635
Fix pretty format failing when printing invalid dates #6635
Conversation
Not sure if I should add this, but for GitHub to auto close the issue: this PR will close #6631 |
Codecov Report
@@ Coverage Diff @@
## master #6635 +/- ##
=======================================
Coverage 63.57% 63.57%
=======================================
Files 235 235
Lines 8992 8992
Branches 3 4 +1
=======================================
Hits 5717 5717
Misses 3274 3274
Partials 1 1
Continue to review full report at Codecov.
|
All the checks were successful with the previous commit, I've just moved around something in the changelog (I did place it at the wrong position in the first place) and now Travis check is failing. Can someone re-start the job in TravisCI please? or tell me what in 8cf31da is making travis to fail :-D |
...ah yeah, GitHub status saying |
Don't mind Travis 🙂 |
Good stuff, is there something better we can use than Some ideas:
|
@SimenB thanks for reviewing.
I actually like the second one, since an invalid date's primitive value is |
...one thing tho, we have to remember that valid dates do not have
|
My thinking is that "invalid-date" looks like a string and we pretty-print things other types as:
So it would make sense that we include the type for the invalid date:
Check out the way we pretty print other types and try to stay consistent. One issue with the current implementation would be the diff for a test like: test('example', () => {
expect(Date(NaN)).toEqual('invalid-date');
}); Would fail and have a visually indistinguishable diff |
@rickhanlonii did you miss my first reply to your message? ;-)
I thought that too at first, but strings are surrounded with double quotes. Anyway, as I said in my first reply, what about So you confirm, I guess it's either:
I'm ready to change it now as soon as I got your choice ;-) |
Oh shoot I missed that, good call out I like |
Any ETA on when this will be merged @SimenB ? (or should I not worry about resolving conflicts in |
We can take care of a merge conflict if needed when we merge, no need for you to keep updating it 🙂 |
@rickhanlonii does this LGTY? If so, we can probably merge it 🙂 |
@rickhanlonii pingeling 😀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@@ -146,7 +146,7 @@ function printBasicValue( | |||
return printSymbol(val); | |||
} | |||
if (toStringed === '[object Date]') { | |||
return toISOString.call(val); | |||
return isNaN(+val) ? 'Date { NaN }' : toISOString.call(val); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also keep around Date.prototype.toString
and fall back to that one. It prints Invalid Date
.
const toStringValue = toString.call(val);
return toStringValue === 'Invalid Date' ? toStringValue : toISOString.call(val);
console.log(new Date(Infinity)) // 'Invalid Date'
console.log(new Date(NaN)) // 'Invalid Date'
EDIT: Or we can do Date { Invalid }
.
return toString.call(val) === 'Invalid Date'
? 'Date { Invalid }'
: toISOString.call(val);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
too late :-p
anyway, I do not like testing over a "human" string. While an invalid date will always cast to the wonderful JavaScript number... not a number 🤣
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah, too late 😀
Feel free to send a new PR with this change if you think it makes sense
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
As suggested by @SimenB in #6631, this allow pretty format (snapshot serializer) to print invalid date objects instead of failing.
before:
after:
The snapshot will contain
Date { NaN }
as of the date value.Test plan
A test has been added.