@@ -290,11 +290,12 @@ describe('checkOrSetAlreadyCaught()', () => {
290
290
} ) ;
291
291
292
292
describe ( '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;
293
294
// Jest messes with the global object, so there is no global crypto object in any node version
294
295
// For this reason we need to create our own crypto object for each test to cover all the code paths
295
296
it ( 'returns valid uuid v4 ids via Math.random' , ( ) => {
296
297
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 ) ;
298
299
}
299
300
} ) ;
300
301
@@ -305,7 +306,7 @@ describe('uuid4 generation', () => {
305
306
( global as any ) . crypto = { getRandomValues : cryptoMod . getRandomValues } ;
306
307
307
308
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 ) ;
309
310
}
310
311
} ) ;
311
312
@@ -316,7 +317,30 @@ describe('uuid4 generation', () => {
316
317
( global as any ) . crypto = { randomUUID : cryptoMod . randomUUID } ;
317
318
318
319
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 ) ;
320
344
}
321
345
} ) ;
322
346
} ) ;
0 commit comments