Use modern timers in monorepo Jest tests (#38955)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38955

Jest introduced "modern" timers based on `sinon/fake-timers` in Jest 26 ([release announcement](https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers)), and they became the default in Jest 27, in May 2021.

Modern timers have more capabilities - they were introduced with support for `queueMicrotask`, mocking `Date`, etc., and they've continued to receive more attention from the Jest team since - they're now much more comprehensive and more configurable than legacy timers.

Importantly, because they're not based on Jest mocks, they're not affected in surprising ways by eg `jest.resetAllMocks()` (a particularly confusing side-effect when fake timers are enabled globally, as in our setup).

This migrates RN's own tests and config to modern fake timers, or real timers where that's more appropriate.

NOTE: In cases where non-trivial changes to the tests are required, four test files are individually opted-in to `legacyFakeTimers` with a `TODO(legacy-fake-timers)`. I'll open these up for community contributions to fix.

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D48189907

fbshipit-source-id: 2e7ce74cc60e80679d81d7c16d599ad1bbe2c921
This commit is contained in:
Rob Hogan 2023-08-11 11:01:06 -07:00 коммит произвёл Facebook GitHub Bot
Родитель dea9c35016
Коммит 86968c1104
7 изменённых файлов: 14 добавлений и 10 удалений

Просмотреть файл

@ -20,7 +20,7 @@ module.exports = {
setupFiles: ['./packages/react-native/jest/local-setup.js'],
fakeTimers: {
enableGlobally: true,
legacyFakeTimers: true,
legacyFakeTimers: false,
},
snapshotFormat: {
escapeString: true,

Просмотреть файл

@ -11,6 +11,9 @@
'use strict';
// TODO(legacy-fake-timers): Fix these tests to work with modern timers.
jest.useFakeTimers({legacyFakeTimers: true});
jest.mock('react-native/Libraries/Utilities/HMRClient');
jest.mock('react-native/Libraries/Core/Devtools/getDevServer', () =>

Просмотреть файл

@ -166,15 +166,10 @@ describe('promise tasks', () => {
}
beforeEach(() => {
jest.resetModules();
jest.useFakeTimers({legacyFakeTimers: true});
InteractionManager = require('../InteractionManager');
sequenceId = 0;
});
afterEach(() => {
jest.useRealTimers();
});
it('should run a basic promise task', () => {
const task1 = jest.fn(() => {
expect(++sequenceId).toBe(1);

Просмотреть файл

@ -10,6 +10,10 @@
*/
'use strict';
// TODO(legacy-fake-timers): Fix these tests to work with modern timers.
jest.useFakeTimers({legacyFakeTimers: true});
jest.mock('../../../Core/Devtools/parseErrorStack', () => {
return {__esModule: true, default: jest.fn(() => [])};
});

Просмотреть файл

@ -9,6 +9,9 @@
* @oncall react_native
*/
// TODO(legacy-fake-timers): Fix these tests to work with modern timers.
jest.useFakeTimers({legacyFakeTimers: true});
import type {PressEvent} from '../../Types/CoreEventTypes';
const UIManager = require('../../ReactNative/UIManager');

Просмотреть файл

@ -9,6 +9,9 @@
* @oncall react_native
*/
// TODO(legacy-fake-timers): Fix these tests to work with modern timers.
jest.useFakeTimers({legacyFakeTimers: true});
import type {HostComponent} from '../../../Renderer/shims/ReactNativeTypes';
import * as React from 'react';

Просмотреть файл

@ -45,12 +45,8 @@ let consoleError;
describe('publish-npm', () => {
beforeAll(() => {
jest.useFakeTimers({legacyFakeTimers: false});
jest.setSystemTime(date);
});
afterAll(() => {
jest.useRealTimers();
});
beforeEach(() => {
consoleError = console.error;
console.error = consoleErrorMock;