File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed
Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -301,7 +301,29 @@ describe('checkOrSetAlreadyCaught()', () => {
301301} ) ;
302302
303303describe ( 'uuid4 generation' , ( ) => {
304- it ( 'returns valid uuid v4 ids' , ( ) => {
304+ // Jest messes with the global object, so there is no global crypto object in any node version
305+ // For this reason we need to create our own crypto object for each test to cover all the code paths
306+ it ( 'returns valid uuid v4 ids via Math.random' , ( ) => {
307+ for ( let index = 0 ; index < 1_000 ; index ++ ) {
308+ expect ( uuid4 ( ) ) . toMatch ( / ^ [ 0 - 9 A - F ] { 12 } [ 4 ] [ 0 - 9 A - F ] { 3 } [ 8 9 A B ] [ 0 - 9 A - F ] { 15 } $ / i) ;
309+ }
310+ } ) ;
311+
312+ it ( 'returns valid uuid v4 ids via crypto.getRandomValues' , ( ) => {
313+ const cryptoMod = require ( 'crypto' ) ;
314+
315+ ( global as any ) . crypto = { getRandomValues : cryptoMod . getRandomValues } ;
316+
317+ for ( let index = 0 ; index < 1_000 ; index ++ ) {
318+ expect ( uuid4 ( ) ) . toMatch ( / ^ [ 0 - 9 A - F ] { 12 } [ 4 ] [ 0 - 9 A - F ] { 3 } [ 8 9 A B ] [ 0 - 9 A - F ] { 15 } $ / i) ;
319+ }
320+ } ) ;
321+
322+ it ( 'returns valid uuid v4 ids via crypto.randomUUID' , ( ) => {
323+ const cryptoMod = require ( 'crypto' ) ;
324+
325+ ( global as any ) . crypto = { randomUUID : cryptoMod . randomUUID } ;
326+
305327 for ( let index = 0 ; index < 1_000 ; index ++ ) {
306328 expect ( uuid4 ( ) ) . toMatch ( / ^ [ 0 - 9 A - F ] { 12 } [ 4 ] [ 0 - 9 A - F ] { 3 } [ 8 9 A B ] [ 0 - 9 A - F ] { 15 } $ / i) ;
307329 }
You can’t perform that action at this time.
0 commit comments