Skip to content

Commit

Permalink
test: using fake timers globally
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienIzz committed Feb 5, 2024
1 parent 4ea46c5 commit f6101ec
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ describe('SpatialNavigationVirtualizedGrid', () => {
);

it('renders the correct number of item', () => {
jest.useFakeTimers();
const component = renderGrid();
act(() => jest.runAllTimers());

Expand All @@ -74,7 +73,6 @@ describe('SpatialNavigationVirtualizedGrid', () => {
});

it('renders the correct number of item', () => {
jest.useFakeTimers();
const component = renderGrid();
act(() => jest.runAllTimers());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ describe('SpatialNavigationVirtualizedList', () => {
);

it('renders the correct number of item', () => {
jest.useFakeTimers();
const component = renderList();
act(() => jest.runAllTimers());

Expand All @@ -75,7 +74,6 @@ describe('SpatialNavigationVirtualizedList', () => {
});

it('handles correctly RIGHT and RENDERS new elements accordingly while deleting elements that are too far from scroll', async () => {
jest.useFakeTimers();
const component = renderList();
act(() => jest.runAllTimers());

Expand Down
11 changes: 11 additions & 0 deletions packages/lib/src/testing/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* UTC+1 in Winter (the default date is in Winter)
*/
export const TEST_DEFAULT_TZ = 'Europe/Paris';

/**
* 15:23 UTC -> 16:23 In Paris
*/
export const TEST_DEFAULT_DATE = '2023-03-05T15:23:49.294Z';

export const TEST_DEFAULT_MATH_RANDOM = 0.372294134538401;
37 changes: 36 additions & 1 deletion packages/lib/src/testing/jest-setupAfterEnv.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
import '@testing-library/jest-native/extend-expect';

export {};
import { TEST_DEFAULT_DATE, TEST_DEFAULT_MATH_RANDOM } from './constants';

/**
* Some globals have no reason to not ever be mocked if we want to have reproducible tests.
* Put those things here: Date, Math.random, etc.
*
* You can still customize the mock in an isolated way for a given test or test suite
* BEWARE that your customizations in tests and test suites don't apply to top-level module code (it runs before the `beforeEach`)
*/
const setupPermanentMocks = () => {
// Note: Timezone is set in src/testing/jest-globalSetup.ts (it wouldn't work to set it here)
jest.useFakeTimers({
// We're not really interested in stopping the microtasks queue, what we want to mock is "timers"
doNotFake: [
'setImmediate', // see https://github.com/callstack/react-native-testing-library/issues/1347
'clearImmediate',
'nextTick',
'queueMicrotask',
'requestIdleCallback',
'cancelIdleCallback',
'requestAnimationFrame',
'cancelAnimationFrame',
'hrtime',
'performance',
],
now: new Date(TEST_DEFAULT_DATE), // To customize in a test, use `jest.setSystemTime`
});

Math.random = () => TEST_DEFAULT_MATH_RANDOM; // To customize in a given test, use `jest.spyOn(Math, "random").mockReturnValue(xx)`
};

// Some code runs before `beforeEach` (top-level code from imported modules), so this line is needed
setupPermanentMocks();

// And then this one is needed to re-set the mocks after the automatic `clearMocks`
beforeEach(setupPermanentMocks);

0 comments on commit f6101ec

Please sign in to comment.