mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-03 05:55:03 +01:00
test(utils): use mocked timers everywhere
This commit is contained in:
@@ -6,8 +6,7 @@ const returnDelayed =
|
||||
new Promise(resolve => setTimeout(() => resolve(value), ms));
|
||||
|
||||
describe('createLazy', () => {
|
||||
// setImmediate hacks below works only with legacyFakeTimers
|
||||
jest.useFakeTimers({ legacyFakeTimers: true });
|
||||
jest.useFakeTimers();
|
||||
|
||||
it('basic', async () => {
|
||||
const initFn = jest.fn(returnDelayed(500));
|
||||
@@ -20,7 +19,7 @@ describe('createLazy', () => {
|
||||
expect(lazy.get()).toEqual(undefined);
|
||||
expect(lazy.getPending()).not.toEqual(undefined);
|
||||
|
||||
setImmediate(() => jest.advanceTimersToNextTimer());
|
||||
jest.advanceTimersToNextTimerAsync();
|
||||
const res = await initPromise;
|
||||
|
||||
expect(res).toEqual('taxation');
|
||||
@@ -34,7 +33,7 @@ describe('createLazy', () => {
|
||||
const disposeFn = jest.fn();
|
||||
const lazy = createLazy(initFn, disposeFn);
|
||||
|
||||
setImmediate(() => jest.advanceTimersToNextTimer());
|
||||
jest.advanceTimersToNextTimerAsync();
|
||||
await lazy.getOrInit([42]);
|
||||
|
||||
expect(lazy.get()).toEqual([42]);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { getSynchronize } from '../src/getSynchronize';
|
||||
import { mockTime, unmockTime } from './utils/mockTime';
|
||||
|
||||
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
@@ -24,6 +25,12 @@ describe('getSynchronize', () => {
|
||||
beforeEach(() => {
|
||||
state = 'init';
|
||||
synchronize = getSynchronize();
|
||||
|
||||
mockTime();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
unmockTime();
|
||||
});
|
||||
|
||||
it('basic', async () => {
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
import { promiseAllSequence } from '../src/promiseAllSequence';
|
||||
import { mockTime, unmockTime } from './utils/mockTime';
|
||||
|
||||
describe('promiseAllSequence', () => {
|
||||
beforeEach(() => {
|
||||
mockTime();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
unmockTime();
|
||||
});
|
||||
|
||||
it('all passed', async () => {
|
||||
const actionInnerProcess = jest.fn();
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { scheduleAction } from '../src/scheduleAction';
|
||||
import { mockTime, unmockTime } from './utils/mockTime';
|
||||
|
||||
const ERR_SIGNAL = 'Aborted by signal';
|
||||
const ERR_DEADLINE = 'Aborted by deadline';
|
||||
@@ -26,7 +27,12 @@ describe('scheduleAction', () => {
|
||||
return `${addings.length - removals.length} listeners not cleaned`;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockTime();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
unmockTime();
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
@@ -60,7 +66,6 @@ describe('scheduleAction', () => {
|
||||
throw new Error('Runtime error');
|
||||
});
|
||||
|
||||
// note: allow certain errors?
|
||||
await expect(() => scheduleAction(spy, { deadline: Date.now() + 1000 })).rejects.toThrow(
|
||||
ERR_DEADLINE,
|
||||
);
|
||||
@@ -82,7 +87,6 @@ describe('scheduleAction', () => {
|
||||
throw new Error('Runtime error');
|
||||
});
|
||||
|
||||
// note: allow certain errors?
|
||||
await expect(() =>
|
||||
scheduleAction(spy, { deadline: Date.now() + 1000, signal: aborter.signal }),
|
||||
).rejects.toThrow(ERR_SIGNAL);
|
||||
@@ -102,7 +106,6 @@ describe('scheduleAction', () => {
|
||||
throw new Error('Runtime error');
|
||||
});
|
||||
|
||||
// note: allow certain errors?
|
||||
const result = await scheduleAction(spy, { deadline: Date.now() + 1000 });
|
||||
|
||||
expect(result).toEqual('Foo');
|
||||
|
||||
16
packages/utils/tests/utils/mockTime.ts
Normal file
16
packages/utils/tests/utils/mockTime.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
let timeMocked = false;
|
||||
|
||||
export const mockTime = async () => {
|
||||
jest.useFakeTimers();
|
||||
timeMocked = true;
|
||||
|
||||
await jest.advanceTimersToNextTimerAsync();
|
||||
while (timeMocked && jest.getTimerCount()) {
|
||||
await jest.advanceTimersToNextTimerAsync();
|
||||
}
|
||||
};
|
||||
|
||||
export const unmockTime = () => {
|
||||
timeMocked = false;
|
||||
jest.useRealTimers();
|
||||
};
|
||||
Reference in New Issue
Block a user