-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
47 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |