Skip to content

Commit 08657e8

Browse files
committed
Improve tests
1 parent d1de114 commit 08657e8

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

packages/utils/test/misc.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,29 @@ describe('checkOrSetAlreadyCaught()', () => {
301301
});
302302

303303
describe('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-9A-F]{12}[4][0-9A-F]{3}[89AB][0-9A-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-9A-F]{12}[4][0-9A-F]{3}[89AB][0-9A-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-9A-F]{12}[4][0-9A-F]{3}[89AB][0-9A-F]{15}$/i);
307329
}

0 commit comments

Comments
 (0)