File tree Expand file tree Collapse file tree 1 file changed +24
-2
lines changed Expand file tree Collapse file tree 1 file changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -300,8 +300,30 @@ describe('checkOrSetAlreadyCaught()', () => {
300
300
} ) ;
301
301
} ) ;
302
302
303
- describe ( 'uuid4 generation' , ( ) => {
304
- it ( 'returns valid uuid v4 ids' , ( ) => {
303
+ describe . only ( 'uuid4 generation' , ( ) => {
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
+
305
327
for ( let index = 0 ; index < 1_000 ; index ++ ) {
306
328
expect ( uuid4 ( ) ) . toMatch ( / ^ [ 0 - 9 A - F ] { 12 } [ 4 ] [ 0 - 9 A - F ] { 3 } [ 8 9 A B ] [ 0 - 9 A - F ] { 15 } $ / i) ;
307
329
}
You can’t perform that action at this time.
0 commit comments