let randomMock: jest.SpyInstance | null = null let rmdSeed = 0 let customSeed = 0 export function restoreMathRandom() { if (randomMock) { randomMock.mockRestore() randomMock = null rmdSeed = 0 customSeed = 0 } } export function clearMathRandom() { rmdSeed = Math.max(0, customSeed) } export function mockMathRandom(seed = 0) { customSeed = seed randomMock = jest.spyOn(Math, 'random').mockImplementation(() => { const next = ++rmdSeed return Number(`0.${next}`) }) clearMathRandom() } mockMathRandom.restore = restoreMathRandom mockMathRandom.clear = clearMathRandom