Skip to content
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

Merged
merged 7 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master

### Fixes

- `[pretty-format]` Fix formatting of invalid Date objects ([#6635](https://github.com/facebook/jest/pull/6635))

## 23.3.0

### Features
Expand Down
5 changes: 5 additions & 0 deletions packages/pretty-format/src/__tests__/pretty_format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ describe('prettyFormat()', () => {
expect(prettyFormat(val)).toEqual('2001-09-09T01:46:40.000Z');
});

it('prints an invalid date', () => {
const val = new Date(Infinity);
expect(prettyFormat(val)).toEqual('Date { NaN }');
});

it('prints an empty object', () => {
const val = {};
expect(prettyFormat(val)).toEqual('Object {}');
Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

@SimenB SimenB Aug 15, 2018

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);

Copy link
Contributor Author

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 🤣

Copy link
Member

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

}
if (toStringed === '[object Error]') {
return printError(val);
Expand Down