Add mockRandom utility for testing

This commit is contained in:
MareStare 2025-03-04 02:42:14 +00:00
parent a02a9cd8c4
commit b4ab1ed42c
2 changed files with 18 additions and 9 deletions

View file

@ -1,9 +0,0 @@
export function mockDateNow(initialDateNow: number): void {
beforeAll(() => {
vi.useFakeTimers().setSystemTime(initialDateNow);
});
afterAll(() => {
vi.useRealTimers();
});
}

18
assets/test/mock.ts Normal file
View file

@ -0,0 +1,18 @@
export function mockDateNow(initialDateNow: number): void {
beforeAll(() => {
vi.useFakeTimers().setSystemTime(initialDateNow);
});
afterAll(() => {
vi.useRealTimers();
});
}
/**
* Mocks `Math.random` to return a static value.
*/
export function mockRandom(staticValue = 0.5) {
const realRandom = Math.random;
beforeEach(() => (Math.random = () => staticValue));
afterEach(() => (Math.random = realRandom));
}