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

add failing test for Invalid Date #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kevlened
Copy link

@kevlened kevlened commented Dec 6, 2024

Invalid dates currently throw a SyntaxError during decoding, because new Date('invalid').getTime() returns NaN, which is invalid JSON.

I wasn't sure how you'd like to encode this, so I just created the test.

@kevlened
Copy link
Author

kevlened commented Dec 6, 2024

devalue encodes it as a Date with an empty string

https://github.com/Rich-Harris/devalue/blob/main/test/test.js#L116

Comment on lines +55 to +57
const output = await quickDecode(encode(input)) as Date;
expect(output?.toString()).toEqual('Invalid Date');
expect(output?.getTime()).toBe(NaN);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unusual, because jest's expect matchers don't work properly with Invalid Dates.

@kevlened
Copy link
Author

kevlened commented Dec 7, 2024

In retrospect, I'm unsure if using a string instead of an int in the array shape causes a performance hit.

Invalid Dates may deserve their own type, but I'll leave this PR as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant