Randomized IDs cause snapshot test failures in Jest, Vitest, and Playwright. The mocking module allows you to seed the internal Web Crypto generator with a deterministic Mulberry32 PRNG during tests.
Usage in Test Suites
js
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { seed, resetMocks, uuidv4, nanoId } from '@readytools/id';
describe('User Snapshot Testing', () => {
beforeEach(() => {
// Seed with a constant number for deterministic output
seed(42);
});
afterEach(() => {
// Always restore secure native Web Crypto
resetMocks();
});
it('generates predictable IDs in snapshots', () => {
const id1 = uuidv4();
const id2 = nanoId();
// Will always produce the exact same values on every run
expect(id1).toBe('51336c53-b09b-449e-ba63-005fa95781a7');
});
});Important Security Notice
Warning: Testing Only
Never call seed() in production code. Seeding replaces cryptographically secure randomness with a 32-bit pseudo-random algorithm. Always invoke resetMocks() in test teardown hooks.