Skip to content

Commit cca9b13

Browse files
committed
jest [nfc]: Take Jest's new defaulting of modern fake-timer implementation
Jest 27 flipped the default for fake timers to the "modern" implementation, from the "legacy" implementation: https://jestjs.io/blog/2021/05/25/jest-27 Now, in the config, we can just say "fake" when we mean "modern fake". Since the goal is to eventually forget about the "legacy" implementation, apply that simpler name across our test code.
1 parent 6ebc7f4 commit cca9b13

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const projectForPlatform = platform => {
7979
globals: {
8080
__TEST__: true,
8181
},
82-
timers: 'modern',
82+
timers: 'fake',
8383
globalSetup: './jest/globalSetup.js',
8484
setupFiles: [
8585
'./jest/globalFetch.js',

jest/jestSetup.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { URL, URLSearchParams } from 'react-native-url-polyfill';
66
// $FlowIgnore[untyped-import] - this is not anywhere near critical
77
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';
88

9-
import { assertUsingModernFakeTimers } from '../src/__tests__/lib/fakeTimers';
9+
import { assertUsingFakeTimers } from '../src/__tests__/lib/fakeTimers';
1010

1111
// Use the same `URL` polyfill we do in the app.
1212
//
@@ -21,8 +21,8 @@ import { assertUsingModernFakeTimers } from '../src/__tests__/lib/fakeTimers';
2121
polyfillGlobal('URL', () => URL);
2222
polyfillGlobal('URLSearchParams', () => URLSearchParams);
2323

24-
// Default should be set with `timers: 'modern'` in Jest config.
25-
assertUsingModernFakeTimers();
24+
// Default should be set with `timers: 'fake'` in Jest config.
25+
assertUsingFakeTimers();
2626

2727
// Mock `react-native` ourselves, following upstream advice [1] [2].
2828
//

src/__tests__/lib/fakeTimers.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
import { sleep } from '../../utils/async';
33

44
/**
5-
* Ensure the "modern" fake-timer implementation is used.
5+
* Ensure the fake-timer implementation is used.
66
*
7-
* By setting `timers: 'modern'` in our Jest config, the modern
7+
* By setting `timers: 'fake'` in our Jest config, the fake-timer
88
* implementation is the default. May be used to double-check that
99
* this default is in fact set, in our Jest setup file.
1010
*
1111
* Also, in one or two files, we switch over to using real timers,
1212
* with `jest.useRealTimers()`. May be used in those files to make
1313
* sure this setting doesn't linger where we don't want it to.
14+
*
15+
* Note: As of Jest 27, there are "modern" and "legacy" fake-timer
16+
* implementations. This checks for the "modern" one, which is the only one
17+
* we should use.
1418
*/
15-
export const assertUsingModernFakeTimers = () => {
19+
export const assertUsingFakeTimers = () => {
1620
// "Note: This function is only available when using modern fake
1721
// timers implementation"
1822
//
@@ -25,11 +29,9 @@ export const assertUsingModernFakeTimers = () => {
2529
*/
2630
export const fakeSleep = async (ms: number): Promise<void> => {
2731
try {
28-
assertUsingModernFakeTimers();
32+
assertUsingFakeTimers();
2933
} catch {
30-
throw new Error(
31-
'Tried to call `fakeSleep` without "modern" fake-timer implementation enabled.',
32-
);
34+
throw new Error('Tried to call `fakeSleep` without fake-timer implementation enabled.');
3335
}
3436

3537
const sleepPromise = sleep(ms);

src/__tests__/metatests.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @jest-environment jest-environment-jsdom-global */
22
// @flow strict-local
33

4-
import { assertUsingModernFakeTimers } from './lib/fakeTimers';
4+
import { assertUsingFakeTimers } from './lib/fakeTimers';
55

66
/**
77
* This file should not test any part of the application. It exists to test that
@@ -75,8 +75,8 @@ describe('jsdom-global', () => {
7575
});
7676
});
7777

78-
describe('Jest modern fake timers', () => {
79-
assertUsingModernFakeTimers();
78+
describe('Jest fake timers', () => {
79+
assertUsingFakeTimers();
8080

8181
afterEach(() => {
8282
// clear any unset timers

src/utils/__tests__/async-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* @flow strict-local */
22
import { sleep, BackoffMachine } from '../async';
3-
import { assertUsingModernFakeTimers } from '../../__tests__/lib/fakeTimers';
3+
import { assertUsingFakeTimers } from '../../__tests__/lib/fakeTimers';
44

55
describe('BackoffMachine', () => {
66
beforeAll(() => {
7-
assertUsingModernFakeTimers();
7+
assertUsingFakeTimers();
88
});
99

1010
afterEach(() => {
@@ -60,7 +60,7 @@ const sleepMeasure = async (expectedMs: number) => {
6060

6161
describe('sleep (ideal)', () => {
6262
beforeAll(() => {
63-
assertUsingModernFakeTimers();
63+
assertUsingFakeTimers();
6464
});
6565

6666
afterEach(() => {

0 commit comments

Comments
 (0)