@@ -290,11 +290,12 @@ describe('checkOrSetAlreadyCaught()', () => {
290290} ) ;
291291
292292describe ( 'uuid4 generation' , ( ) => {
293+ const uuid4Regex = / ^ [ 0 - 9 A - F ] { 12 } [ 4 ] [ 0 - 9 A - F ] { 3 } [ 8 9 A B ] [ 0 - 9 A - F ] { 15 } $ / i;
293294 // Jest messes with the global object, so there is no global crypto object in any node version
294295 // For this reason we need to create our own crypto object for each test to cover all the code paths
295296 it ( 'returns valid uuid v4 ids via Math.random' , ( ) => {
296297 for ( let index = 0 ; index < 1_000 ; index ++ ) {
297- expect ( uuid4 ( ) ) . toMatch ( / ^ [ 0 - 9 A - F ] { 12 } [ 4 ] [ 0 - 9 A - F ] { 3 } [ 8 9 A B ] [ 0 - 9 A - F ] { 15 } $ / i ) ;
298+ expect ( uuid4 ( ) ) . toMatch ( uuid4Regex ) ;
298299 }
299300 } ) ;
300301
@@ -305,7 +306,7 @@ describe('uuid4 generation', () => {
305306 ( global as any ) . crypto = { getRandomValues : cryptoMod . getRandomValues } ;
306307
307308 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+ expect ( uuid4 ( ) ) . toMatch ( uuid4Regex ) ;
309310 }
310311 } ) ;
311312
@@ -316,7 +317,30 @@ describe('uuid4 generation', () => {
316317 ( global as any ) . crypto = { randomUUID : cryptoMod . randomUUID } ;
317318
318319 for ( let index = 0 ; index < 1_000 ; index ++ ) {
319- expect ( uuid4 ( ) ) . toMatch ( / ^ [ 0 - 9 A - F ] { 12 } [ 4 ] [ 0 - 9 A - F ] { 3 } [ 8 9 A B ] [ 0 - 9 A - F ] { 15 } $ / i) ;
320+ expect ( uuid4 ( ) ) . toMatch ( uuid4Regex ) ;
321+ }
322+ } ) ;
323+
324+ it ( "return valid uuid v4 even if crypto doesn't exists" , ( ) => {
325+ ( global as any ) . crypto = { getRandomValues : undefined , randomUUID : undefined } ;
326+
327+ for ( let index = 0 ; index < 1_000 ; index ++ ) {
328+ expect ( uuid4 ( ) ) . toMatch ( uuid4Regex ) ;
329+ }
330+ } ) ;
331+
332+ it ( 'return valid uuid v4 even if crypto invoked causes an error' , ( ) => {
333+ ( global as any ) . crypto = {
334+ getRandomValues : ( ) => {
335+ throw new Error ( 'yo' ) ;
336+ } ,
337+ randomUUID : ( ) => {
338+ throw new Error ( 'yo' ) ;
339+ } ,
340+ } ;
341+
342+ for ( let index = 0 ; index < 1_000 ; index ++ ) {
343+ expect ( uuid4 ( ) ) . toMatch ( uuid4Regex ) ;
320344 }
321345 } ) ;
322346} ) ;
0 commit comments