-
-
Notifications
You must be signed in to change notification settings - Fork 267
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(vanilla): Improve Snapshot type so that it respects builtin objects #672
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 092bec4:
|
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.
The change looks reasonable!
Thanks for your suggestions!
Would you be able to help on it?
That's true, but I think you can run normal tests with
As you might guess, the first one can be extracted fairly easily, and the second one is intentional.
If they rely on internal types, I'd suggest to pin the valtio version and use it. We may really change them in the future without notice. |
Yup, I think I can write bunch of tests for Snapshot type using something like
Nope, it doesn't work and I have to install Yarn. And maybe I will, it's just that I have to do this for such small thing. Not a big problem, I think. But having scripts running using npm might be more convenient, as it is the default (and I personally could've run tests via pnpm).
And so I did, yes, but it would be just much more handful to have useSnapshot
Alright then :) |
We can simply drop such tests for very old TS. Please make it a new test file.
Oops, my bad. My recent change requires yarn for test. |
Added some tests for Snapshot type, ended up using |
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.
So, this needs to be changed to support custom classes?
Feels like we never reach an ideal solution. false positives are better than false negatives.
Can't agree with you more. |
Out of curiosity: Why GitHub does not let me to request review from both of you at the same time? x) |
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.
I don't know if there are any edge cases uncovered. Seems reasonable. (And makes me feel Snapshot type will be INTERNAL forever.)
I'll wait for @stephenh 's review.
Wellp, looks like I need yarn after all :) |
Fixed Snapshot type tests for TS 3.7. It appears that |
Nice catch! |
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.
Are you hesitant renaming the new file to tests/typings/snapshot.test.ts
?
No, but this will require configuration for jest, I think. It should be ignored by it. |
Didn't catch this one. For some reason VSCodium decided not to show ESLint errors for me :D |
Oh, I wasn't aware that. Which part is bad? We have |
The part where jest will likely fail trying to run through this file :) I can add configuration for jest and rename the file as you ask. If you insist :) |
Ah, I thought the other way around. |
If jest will ignore test file without any test in it, then it should be fine. I'm not familiar with jest that much, but, for instance, AVA will fail in that case. So I just assumed it might have same behaviour, because it seem reasonable. |
Google says it will fail. So in that case we need to either ignore these files (as I suggested - the whole |
This is how it's done in jotai. |
It does seem like it. I don't see any use of jest's |
Not sure though why do use have |
I think some of eslint rules complain otherwise. |
Yeah, please try it for now. We'll revisit later with other repos. Or maybe with #646. I may consider including typing tests in |
…hese tests as noot jest tests.
I've merged typings tests file into |
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.
Thanks for your contribution!!
Summary
This PR improves Snapshot type so that it does not affect builtin object, like Date.
Examples for old and new behaviours can be found in TS playground: for TS 3.5.1 and for TS 4.9.5. As you can see from the
OldSnapshot
example, it iterates through every member ofDate
,Map
andSet
object and converts these objects to readonly POJOs for some reason.The solution might not be perfect though, so I would suggest adding some tests for typings (or maybe you already have and I just won't be able to find any, so please let me know).
Also, I wouldn't be able to run tests, because this project is hardly relying on Yarn, so will see if the tests are passing.
And lastly, I would like to ask if you can expose both
Options
(for useSnapshot hook) andSnapshot<T>
types, so that people might implement extensions upon this hook and rely on public API.Check List
yarn run prettier
for formatting code and docs