mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-03-14 15:40:03 +01:00
Add mockRandom
utility for testing
This commit is contained in:
parent
a02a9cd8c4
commit
b4ab1ed42c
2 changed files with 18 additions and 9 deletions
|
@ -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
18
assets/test/mock.ts
Normal 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));
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue