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
ref(utils): Improve uuid generation #5426
Conversation
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.
Looks like a good change to me. Took me a few minutes to figure out what's going on but I think in terms of readability, it's similar (maybe a little better) than before. Less bytes is always great and using crypto.randomUUID()
whenever possible is a good change for sure.
Would you mind adding a test or two to make sure this function is covered?
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.
Awesome, thanks!
My only minor concern with the test is that we're not testing a version of node that hits
|
Right. Can we mock |
A few things worth noting... although node supports
A further spanner in the works of the tests is that Jest doesn't have the same globals as node.js or even the browser. This means that the crypto global is missing even in node v18 😭. I remember this same thing being an issue with global So for each uuid test, I construct the global This does make me wonder whether Jest + node version test matrix gives us the coverage we'd be hoping for when globals are involved 🤔 |
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 updating the tests!
You're making a good point about the global object and Node versions. I'll bring it up with the team so that we're all aware of it.
I know this has already been merged, but just for the sake of context-sharing, since I did a bunch of work with our Jest setup earlier this year... As for as Jest x Node versions goes, this has come up before in various ways. The problem is that we're at the mercy of a few intersecting issues:
In short, it's kind of a mess (albeit one which works decently well, even if not perfectly). As far as amending what's available, in the future what you can do is create your own environment. Check out |
Thanks for this! |
I'm having issues with this and Jest. I think the issue is with the UUID usage in jest as explained above. Have you had any luck with running tests in Jest? |
We run all of our tests in Jest without any issue. What version of |
Since modern browsers support
crypto.randomUUID()
it's probably a good idea to use that when available.Rather than let this impact bundle size, I also took some of the recent updates from the linked stackoverflow question.
crypto.randomUUID()
crypto.getRandomValues()
< v15
usesMath.random()
v15 > v16.7 uses
crypto.getRandomValues()
>= v16.7 uses
crypto.randomUUID()
Downsides vs previous code:
crypto.randomUUID()
, this may be less of a concern. I added a comment for the most confusing number+string concat part.crypto.getRandomValues()
, it's called multiple times rather than a single time withnew Uint16Array(8)
which could marginally affect performance.